Raspberry Pi and a PCF8574

The PCF8574 is an 8 bits I/O port expander that uses the I2C protocol. Using this IC, you can use only the SDA and SCL pins of your Arduino board to control up to 8 digital I/O ports.

A0,A1,A2 are address pins
P0,P1,P2,P3,P4,P5,P6,P7 are digital I/O ports
SDA,SCL are the I2C pins

pcf8574n_pinout

If we set pins A0 to A2 to GND, our device address in binary will be 0x20, thats exactly what I did in my example. To enable read and write there are different values required you can see these in the image below

pcf8574-address-map

Schematic

Note that the PCF8574 is a current sink device so you do not require the current limiting resistors

I have only shown 2 LEDs, also note that I have shown A0, A1 and A2 tied to GND, this is what my test module had selected.

pi-and-pcf8574_schem

 

Testing

Open a terminal on your Raspberry PI and type the following to display the I2C address of the device

sudo i2cdetect -y 1

Now you can flash an LED connected to the I2C device by sending data to it using i2cset

sudo i2cset -y 1 0x20 0x55

You can see this below, notice I deliberately put in the incorrect address as well so you can see the error message when you do this

pcf8574-commands

 

Code

 

Type in the following and save as pcd8574.py and run from the command line with sudo python pcd8574.py

from smbus import SMBus
from time import sleep
 
 
def main():
	bus = SMBus(1)
	bus.write_byte(0x20, 0x55)
	sleep(1.0)
	bus.write_byte(0x20, 0xAA)
	sleep(1.0)
	return 0
 
if __name__ == '__main__':
	main()

 

 

Link
5PCS PCF8574P DIP-16 Remote 8-bit I/O Expander IC