I went through the data sheet but it does not tell anything about starting address.
Is your starting address the device address? Or do you mean the starting register where you write your data to?
Slave address:
0 1 0 0 A2 A1 A0 R/W
fixed programmable
What does it mean?
That means that 0100 is fixed while the lower 3 bits are specified by pulling the corresponding pins to GND or Vcc. If you connect A0, A1 and A2 (of the chip, not the Arduino) to GND, the (7bit) I2C slave address is 0b0100000 or 0x20.
I know that the SDA and SCA lines need to be connected and also pull-up resistors need to be connected from these lines to +5V. And finally connect the ground.
And to communicate to the chip I need to use wire library. And mostly I'll have to use the write() function to change the Output pins' state.
But I couldn't figure out the last part i.e. sending data so that the pin state is changed.
You also need to wire the address pins, either to Vcc or GND. Based on how you've done that the I2C address of the device changes. Depending on your interrupt needs you also need to connect the INT pin and maybe the OE (Output Enable) pin.
And mostly I'll have to use the write() function to change the Output pins' state.
If you just want to use them as outputs, your code looks something like this:
Thanks pylon, code looks pretty systematic. You're getting the previous value of the port and altering only one bit.
But I'm finding it hard to understand the following lines
#define I2C_ADDR 0x20
PCA9505 has a 7bit slave address of 0100 PPP R/W
0100 is fixed, PPP programmable bits and R/W to select mode.
So if PPP = 000 and R/W = 0, the address must probably be #define I2C_ADDR 0x40 ?
Wire.write(0x88+pin/8);
I believe "pin" is a number 0 to 7 (or 1 to 8)..? what is the significance of "0x88" and "pin/8"?
What is the need to endTransmission() before requestFrom() ?
pin / 8 will give you what port any one pin is on because there are eight pins per port.
0x88 is the offset address of the first port, so this expression gets the right port address for any given pin.
But I don't find it mentioned anywhere in the datasheet that specifies 0x88 as offset for first port.
This is what I found in the datasheet:
0x00 - 0x04 Input Port Register Bank 0 - 4
0x08 - 0x0C Output Port Register Bank 0 - 4
0x18 - 0x1C I/O Configuration (hence for my application I need to make all of them 0 for output)
and the datasheet tells that Output Port registers are R/W.
So I'm assuming that I need to use 0x08 as my offset address.
(Sorry if I'm sounding silly, this is my first time playing with I2C)
But I don't find it mentioned anywhere in the datasheet that specifies 0x88 as offset for first port.
Yes, the highest bit is the AI (Auto Increment) bit and, theoretically, can be also set to 0 in this code. But because the auto increment is the default in most devices I set that bit to 1 to have the default behavior.