TCA9555 Library for Arduino

TCA9555 is a 16-bit I2C I/O expander from Texas Instruments. It allows the easy addition of 16 I/O ports on any device that supports the I2C bus. This makes it attractive for expanding the number of I/O pins on the standard Arduino platform using ATmega328. Up to eight TCA9555’s can be used on the same I2C bus and thus allowing up to an additional 128 digital pins to be added.

Keith Neufeld had provided a basic C library for PCA9555 in his blog post.TCA9555 is an improved version of PCA9555. Functionally speaking, TCA9555 is identical to PCA9555 and both versions have the same pin-out. So the code for PCA9555 can be used with TCA9555 without any change.

To make it even easier to use, I wrote a C++ Arduino library that can be used with either PCA9555 or TCA9555. The library exposes all the functionalities of the chip.

Here is some sample code demonstrating how this library can be used:

#include <Wire.h>
#include <TCA9555.h>

// intializaing TCA9555, A2 A1 A0 are set to 000.
TCA9555 tca9555(0,0,0);

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  
  //set the pin directions for both ports to 
  //output
  tca9555.setPortDirection(TCA9555::DIR_OUTPUT);
  
  //change P00 (pin 4), P01 (pin 5) and P17 (pin 20) to high.
  tca9555.setOutputStates(1<<8 | 3);
  
  //read back the pin states
  Serial.print(tca9555.getInputStates());
}

void loop()
{
}

Download TCA9555 Library: TCA9555.tar.gz

TCA9555

TCA9555

Be Sociable, Share!

13 Comments

  1. Adrien J. says:

    Hi, I know this is an old post but could you tell me how this command works please ?

    //change P00 (pin 4), P01 (pin 5) and P17 (pin 20) to high.
    tca9555.setOutputStates(1<<8 | 3);

    I would like to know why you set: 1<<8 | 3 for making P00, P01 and P17 high.
    Thanks you for your help. :)

  2. ZARh says:

    Thank you for this library :)
    Great work and it help me a lot :)

  3. Stocky says:

    I am getting compile errors on Arduino 1.0.5 IDE with this library.
    What version of the IDE is it designed to work with?

  4. BRIAN COCKBURN says:

    Spelling error. s/Taxes/Texas/

  5. Gennady says:

    Good afternoon!

    I have been working on reading ports of the TCA9555 for two days
    already. Your example shown in the library
    http://www.oss.io/p/kerrydwong/TCA9555
    works great!
    Everything is recorded, appears on the outputs of the chip and then
    reads back correctly.
    But if I just want to read the inputs without first writing (read
    dip-switches connectet to inputs), then TCA9555 :: getInputStates ()
    only gives all bits=”1″ regardless of the state-enabled dip switch ON
    (0) or OFF (1).
    PLS help!

    Thank You!
    Gennady

    PS
    Re:setPortDirection(TCA9555::DIR_OUTPUT);
    In my opinion, every input / output direction is installed bitwise

    so to set all 8 inputs (port) you need to write not “static const byte
    DIR_INPUT = 1;” but ” static const byte DIR_INPUT = 0xff” ?

  6. Gennady says:

    Thank you for your answer!
    Can you tell me how to properly use your library if you only need to read the status of the TCA9555 inputs (for scanning dip-switch keyboard)?
    That’s right?

    /*
    * TCA9555 Library, code sample
    * Kerry D. Wong
    * http://www.kerrywong.com
    * 3/2011
    */
    #include
    #include

    // intializaing TCA9555, A2 A1 A0 are set to 000.
    TCA9555 tca9555(0,0,0);

    void setup()
    {
    Serial.begin(9600);
    Wire.begin();

    //read back the pin states
    Serial.print(tca9555.getInputStates());
    }

    void loop()
    {
    }

Leave a Reply