The first sketch is about how to control PCF8575 PINs.
I connected a LED to each PIN to show its state. This IC can’t provide (source mode) enougth current to power on a LED, you need to connect the LED in sink mode, with current flowing from Vcc into the PIN:
With this connection, the LED will be on when IC PIN is connected to ground, that is logical level 0.
Let me show you how to control PIN states:
- write, starting from P7 to P0, the required logical stage (0 or 1), for example: 10110000;
- do the same for PINs from P17 to P10: 10110000 01011101;
- (for convenience) convert the two numbers in hexadecimal: B0 5D;
- send the two values to the expander.
I2C bus allows to connect more than two devices: when you send commands on the bus, you have to specify the target’s address. Every I2C device has a builtin address; for PCF8575 that address is 0x20 (decimal 32). If you connect to ground or Vcc PINs A0, A1 e A2 you can change its address:
For example, if you connect A0 to ground and A1, A2 to Vcc, the resulting address is 0100110 = 0x26. If you change the builtin address, you can connect more than one IC of the same type on the same I2C bus.
After having set the address, you can send commands and data with the following code:
#include <Wire.h> #define PCF8575_ADDRESS 0x20 ... Wire.beginTransmission(PCF8575_ADDRESS); Wire.write(0xB0); Wire.write(0x5D); Wire.endTransmission();
I wrote a simple sketch to light up 16 LEDs with a rolling animation:
In the next page, an Arduino working with both modules…