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
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. :)
Hi Arien,
It may have been my mistake in the sample code. The function setOutputStates takes a word (16bit), the high byte is P17-P10 and the low byte is P7-P0. so the mask I gave in the sample code (1<<8|3) 00000010000011 should map to port 0,1 and 10 not 17.
Hi,
Thanks for your reply now I know how bitmask works :)
Thank you for this library :)
Great work and it help me a lot :)
Thanks. Glad I could help.
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?
Did you place the library into the libraries folder under your arduino installation? It should work with 1.05.
yes its in the libraries folder
This is the error as soon as I try and compile the example: (changed address to suit my chip)
at line:
TCA9555 tca9555(0,1,0);
“no matching function call to “TCA9555::TCA9555(int,int,int)'”
Spelling error. s/Taxes/Texas/
Corrected. Thanks!
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” ?
Unfortunately, I don’t have a TCA9555 on hand right now (they were used in projects) so I couldn’t test it out…
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()
{
}