Home > Utility > MSP430 (or Arduino) Library for PCF8574 I2C Port Expander

MSP430 (or Arduino) Library for PCF8574 I2C Port Expander

port-expanderI 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”:

#include <PortExpander_I2C.h>

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

void setup() {

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

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.

#include <PortExpander_I2C.h>

PortExpander_I2C pe(0x20);

void setup() {
  for( int i = 0; i < 8; i++ ){

void loop() {
  for( int i = 0; i < 8; i++ ){
    if( pe.digitalRead(i) == 0 ){
      Serial.print("Pin " );
      Serial.println(" is low.");
  1. Martignene
    December 26th, 2014 at 07:37 | #1

    Merci beaucoup. Thanks a lot. Best regards.

  2. Headley
    January 24th, 2015 at 15:39 | #2

    Fantastic library.
    Spent hours trying to write the code out before finding this.


  3. humberto
    May 18th, 2015 at 09:03 | #3

    don’t forget to add #Wire.h this is required for I2C

  4. humberto
    May 18th, 2015 at 10:01 | #4

    got a $99 LCD to I2c card working it has the same IC as the $6+ I/O card been sold on ebay…
    pins are as follows…
    pin 1=gnd, 2=GND, 3=GND with var resistor, 4=P0, 5=P1, 6=P2 7,8,9,10 N/C
    11=P4, 12= P5, 13=P6, 14=P7 15=Vcc with jumper, 16= P3 with transistor 2N3904 (200 ma).

  5. humberto
    May 18th, 2015 at 10:14 | #5

    I2C addressing A0,A1,A2:
    High High High = 0x27, HHL=0x26, HLH=0x25, HLL=0x24, LHH=0x23, LHL=0x22, LLH=21
    Low Low Low= 0x20…
    The pings on the card come Hight by Default= 0x27 and can be changed by soldering the jumpers to low…

  6. Mazur
    January 31st, 2016 at 21:17 | #6

    How do I control 3 pcf8574 with arduino?
    I’ve done this to separate my ic’s:
    PortExpander_I2C pe1(0x20);
    PortExpander_I2C pe2(0x21);
    PortExpander_I2C pe3(0x24);

    Now, if I set pin like this one below:
    ” int ledPin = 7; // 8th port ”
    I’m setting pins for first 0x20 PCF. How do I choose pins for 2nd and 3rd chip?

  7. wilkist1
    February 16th, 2016 at 01:46 | #8

    Great library, thanks!

    Very useful for the ESP8266-1, just add Wire.begin(0,2) to set up to specify GPIO0 for SDA and GPIO2 for SCL.
    There’s a great Instructable on setting up the little ESP for I2C:

  8. July 17th, 2017 at 08:06 | #9

    Thank you.

  9. Pop
    February 27th, 2018 at 09:02 | #10

    I have the same LCD to I2c card as Humberto. Its address is 0x27. The card is working fine with LCD, but i would like to use it in another application. For test, I put a LED (with resistor, of course) from each output to ground. The setup is tested with Blink example program.
    The program is working as expected ONLY if ledPin = 7. For other values, no LED is ON.
    Thanks for possible help.

  1. No trackbacks yet.