WiringPi supports an extension module for the PCF8574 8-bit GPIO expander IC.
The Raspberry Pi has one I2C bus and the PCF8574 has a 3-bit address select port, so in-theory you can connect up 8 PCF8574’s to your Pi.
The PCF8574 is described as having “Quasi Bidirectional IO” ports, however wiringPi takes care of the procedure neccessary to turn a pin from output (the default) to input.
#include <wiringPi.h> #include <pcf8574.h>
pcf8574Setup (int pinBase, int i2cAddress) ;
The pinBase can be any number you like above 64 and the i2cAddress is the address of the device in the I2C bus – 0x38 is the default but they can change if you have multiple devices. Use the i2cdetect command (gpio i2cd) to probe your I2C bus to work out the right address to use.
You can call pcf8574Setup() as many times as needed for each PCF8754 you have in the system – just give it a different pin base and I2C bus address.
You don’t need to specify the number of pins here – the PCF8754 has 8 pins.
- You need to load the I2C kernel modules before you can use I2C devices. Use the gpio command: gpio load i2c
- Use the i2cdetect program to scan your I2C bus to make sure the Pi can see your devices.
- If you have a Rev 1 Pi, then the i2cdetect command is: i2cdetect -y 0 if you have a Rev. 2 Pi, then use i2cdetect -y 1
- The gpio command supports the i2cdetect command and automatically caters for board revision. Simply type: gpio i2cd
- The wiringPi PCF8574 driver knows which revision Pi you have, so you know need to take any special precautions – your code will work on either a Revision 1 or 2 Pi.
- The PCF8574 does not have programmable internal pull-up resistors, but the pins when in input mode effectively have an internal pull-up active.
- The maximum current sourced of sunk on any pin is 20mA, however the chip can only source or sink a maxmum on 100mA.