port-expander
I bought an 8-port general purpose IO expander for adding ports to my MSP430 projects via I2C, but I couldn’t find any library for accessing it. Fortunately the I2C communication with the on-board Texas Instruments PCF8574 chip is pretty straightforward, so after studying the LiquidCrystal_I2C library, which uses the PCF8574 for communicating with the popular 16×2 LCDs, I made my own library for this port expander. You are welcome to it. I release it as Public Domain code.

Download PortExpander_I2C

Example based on “Blink”:
[c]
#include <PortExpander_I2C.h>

PortExpander_I2C pe(0x20);
int ledPin = 7; // 8th port

void setup() {
pe.init();
}

void loop() {
pe.digitalWrite(ledPin, HIGH);
delay(1000);
pe.digitalWrite(ledPin, LOW);
delay(1000);
}

[/c]

Reading from the port expander works by setting the pin high and then waiting for the pin to be grounded. If you want to detect a signal going high, you will need a transistor or something clever for flipping the signal.

[c]
#include <PortExpander_I2C.h>

PortExpander_I2C pe(0x20);

void setup() {
pe.init();
Serial.begin(9600);
for( int i = 0; i < 8; i++ ){
pe.pinMode(i,INPUT);
}
}

void loop() {
for( int i = 0; i < 8; i++ ){
if( pe.digitalRead(i) == 0 ){
Serial.print(“Pin ” );
Serial.print(i);
Serial.println(” is low.”);
}
}
}
[/c]