Over the last three years I build several Raspberry Pi powered robots. With the GPIOs of the Raspberry Pi it is possible to control for example a relays or LEDs. The robots I build got very complex over the years and my Raspberry Pi run out of free GPIO pins. I searched for a cheap and easy to program GPIO expander which is well known in the Raspberry Pi community to get support is necessary. I found the PCF8574 chip soldered on a board which then offers up to 8 additional I/O-ports. The PCF8574 board is connected to the Raspberry Pi with the I2C bus. This makes it very easy to wire everything up with four female-to-female jumpers. You find several Python projects and libraries using the PCF8574 board together with a Raspberry Pi.
The picture below shows the setup I build with the relays and the I/O port expander in the front. The DC motors on the left side are used for another test.
It is possible to mount several I/O port expander as a chain one by one. You only have to set a unique I2C address for each board with the yellow jumpers.
The next picture shows the relays and the I/O port expander in the front with the yellow jumpers.
After connecting the port expander with the Raspberry Pi by using the I2C bus and connecting the relays to the PCF8574 port expander it is time to install the Python software and to write a small test program.
Raspberry Pi 3 Model B Kit
I2C Level Shifter
The next chapter explains the steps to install the software libraries you need to get the PCF8574 board working on your Raspberry Pi. I assume that you are using a Raspberry Pi and an up to date Raspbian as operation system.
Please install all the software packages with following commands on your Raspberry Pi one after the other:
Command: suto apt-get update
Command: sudo apt-get install libffi-dev
My program uses the following library from “flyte” to easily control the pcf8574 board which is hosted on GitHub:
GitHub page: https://github.com/flyte/pcf8574.
To install the program form the GitHub user “flyte” you could use the following command.
Command: sudo pip install pcf8574
Python test program
I developed a small test program to control the 4-way relays. The program only switches the I/O ports of the PCF8574 chip. The relays switches depending on the state of the I/O ports.
Download the program, unzip the program and start it in the command line.
Download: PCF8574 – Relais Control
The program is also available on my GitHub repository too:
GitHub page: https://github.com/custom-build-robots/.
After starting the program you see some information about the switch state of the I/O ports. The Python code should give you an idea how to program your own program.
If you ran out of free GPIO pins on Raspberry Pi the PCF8547 board is a cheap and easy to implement solution to get 8 additional I/O ports. You could not only set the state of the ports you could also read the state of the ports as well. That is perfect if you are using buttons or switches in your project. The I2C bus makes it easy to connect everything together with only for cables. Two for SDA/SCL and tow wires for Vcc/GND.