Home Blog PCF8574 chip, Python and Raspberry Pi 5

PCF8574 chip, Python and Raspberry Pi 5

PCF8574, Python and Raspberry Pi 5
PCF8574, Python and Raspberry Pi 5

I2C communication in Raspberry Pi 5 with PCF8574 and Python.

On Raspberry Pi 4 I was using the Python library pcf8574-io for I2C communication using the PCF8574 chip. However this library does not work on the newer Raspberry Pi 5. To be accurate, you can install it in RPi 5 using pip3 in a virtual environment (venv), however in my case I wanted my script to run as a service in global scope. So a virtual environment was not a choice. Most of the PCF8574 related code I found on the internet was outdated for Rpi4. After some search I found a Python library from jhaubrich on github which actually works. In this article I will give you a how-to guide for having I2C communication with PCF8574 chip and Python on Raspberry Pi 5. I access Raspberry from a terminal window using SSH (use PuTTY or other client).

Preparation

Make sure you have enabled I2C communication in Raspberry.

  • sudo raspi-config
  • Select Interfacing Options > I2C
  • Enable the I2C interface and reboot Raspberry sudo reboot

Install libffi-dev (for Debian OS) which is required by the Python PCF8574 library.

sudo apt install libffi-dev
sudo apt install libffi-dev

Now, we need to find the I2C address. Do an I2C scan by using the i2cdetect command.

i2cdetect -y 1
i2cdetect -y 1

The result will be something like the following. Keep that number, you will need it later on the coding part. In case you have 2 PCF8574 chips you will see 2 such numbers.

Detect I2C address on bus 1
Detect I2C address on bus 1

I will put everything in a folder named myi2c, off course you can have your own file structure. Create the folder and a test script.

cd /home/pi
sudo mkdir myi2c
sudo chown pi:pi myi2c
cd myi2c
sudo touch testpcf.py
sudo chown pi:pi testpcf.py
cd /home/pi
sudo mkdir myi2c
sudo chown pi:pi myi2c
cd myi2c
sudo touch testpcf.py
sudo chown pi:pi testpcf.py

Download the library from github and unzip it. If you don't find it I have made a copy here.

wget https://github.com/jhaubrich/pcf8574/archive/refs/heads/develop.zip
unzip develop.zip
wget https://github.com/jhaubrich/pcf8574/archive/refs/heads/develop.zip
unzip develop.zip

A folder named pcf8574-develop has been created. Rename that folder to pcf.

mv pcf8574-develop pcf
mv pcf8574-develop pcf

Delete downloded zip package and make sure all files are owned by the pi user.

rm develop.zip
sudo chown -R pi:pi *
rm develop.zip
sudo chown -R pi:pi *
Final file structure inside myi2c folder
Final file structure inside myi2c folder

I2C with 1 PCF8574 chip (8 bits)

Open file testpcf.py with nano testpcf.py, copy the code bellow and with right click inside the terminal paste it in the open editor area. Change the address for the I2C communication to whatever you have.

from pcf.pcf8574 import PCF8574

ADDR = 0x38# Change address if needed
mypcf = PCF8574(1, ADDR)

print("Bits: 0-7")
resp = ''
for i in range(8):
    val = 1 if mypcf.port[i] == True else 0
    resp += 'PIN'+str(i)+': '
    resp += str(val)+', '
print(resp)

mypcf.close()
from pcf.pcf8574 import PCF8574

ADDR = 0x38# Change address if needed
mypcf = PCF8574(1, ADDR)

print("Bits: 0-7")
resp = ''
for i in range(8):
    val = 1 if mypcf.port[i] == True else 0
    resp += 'PIN'+str(i)+': '
    resp += str(val)+', '
print(resp)

mypcf.close()

Save the file (Control + X, then type Y and click enter). Now its time to test our script. This script will echo the values of the 8 bits (1 for HIGH, 0 for LOW). Execute the script.

python testpcf.py
python testpcf.py

If you get an error mentioning get_pin_state or read_byte in the error message, then the address you have provided is wrong.

I2C with 2 PCF8574 chips (16 bits)

Open file testpcf.py with nano testpcf.py, copy the code bellow and with right click inside the terminal paste it in the open editor area. Change the addresses for the I2C communication to whatever you have.

from pcf.pcf8574 import PCF8574

ADDR1 = 0x38# Change address if needed
ADDR2 = 0x39# Change address if needed
mypcf1 = PCF8574(1, ADDR1)
mypcf2 = PCF8574(1, ADDR2)

print("Bits: 0-7")
resp = ''
for i in range(8):
    val = 1 if mypcf1.port[i] == True else 0
    resp += 'PIN'+str(i)+': '
    resp += str(val)+', '
print(resp)

print("Bits: 8-15")
resp = ''
for i in range(8):
    val = 1 if mypcf2.port[i] == True else 0
    resp += 'PIN'+str(i)+': '
    resp += str(val)+', '
print(resp)

mypcf1.close()
mypcf2.close()
from pcf.pcf8574 import PCF8574

ADDR1 = 0x38# Change address if needed
ADDR2 = 0x39# Change address if needed
mypcf1 = PCF8574(1, ADDR1)
mypcf2 = PCF8574(1, ADDR2)

print("Bits: 0-7")
resp = ''
for i in range(8):
    val = 1 if mypcf1.port[i] == True else 0
    resp += 'PIN'+str(i)+': '
    resp += str(val)+', '
print(resp)

print("Bits: 8-15")
resp = ''
for i in range(8):
    val = 1 if mypcf2.port[i] == True else 0
    resp += 'PIN'+str(i)+': '
    resp += str(val)+', '
print(resp)

mypcf1.close()
mypcf2.close()

Save the file (Control + X, then type Y and click enter). Now its time to test our script. This script will echo the values of the 8 bits (1 for HIGH, 0 for LOW). Execute the script.

python testpcf.py
python testpcf.py

If you get an error mentioning get_pin_state or read_byte in the error message, then the addresses you have provided are wrong.

Setting port values

If you want to set the value of a port it is as simple as this:

from pcf.pcf8574 import PCF8574

ADDR = 0x38# Change address if needed
mypcf = PCF8574(1, ADDR)

mypcf.port[0] = True
mypcf.port[1] = False
mypcf.port[2] = False

mypcf.close()
from pcf.pcf8574 import PCF8574

ADDR = 0x38# Change address if needed
mypcf = PCF8574(1, ADDR)

mypcf.port[0] = True
mypcf.port[1] = False
mypcf.port[2] = False

mypcf.close()
The cart is empty
  • The cart is empty
Total
EUR 0.00
https://www.isopensource.com/inner.php/shop/

Projects

Current projects

  • IOS Rentals v3.4
    Synchronize reservations with 3rd parties (IOS Sync)
    98%
    November 24, 2024

Projects completed

  • Open Form Manager
    A free open source component for managing forms
    September 20, 2024
  • OFM plugin
    Plugin for Open Form Manager component
    September 20, 2024
  • e-Code module
    Display a block of code with optional highlight and copy to clipboard functionality.
    August 17, 2024
  • FAQ v2.0
    Re-design of component FAQ, compatibility with Elxis 5
    June 17, 2024
  • Open Shop 3.7
    Integration with SoftOne ERP and BoxNow
    June 08, 2024
  • Sticky notes 2.0
    Module for placing notes on Elxis administration area
    May 26, 2024
  • Watch My Site 2.0
    Module to monitor the status of the HTTP web server for up to 10 web sites
    May 23, 2024
  • IOS AERO v1.7
    Compatibility with Elxis 5.5
    April 24, 2024
  • IOS Hotels 3.4
    Compatibility with Elxis 5.5
    April 24, 2024
  • IOS Rentals v3.3
    Compatibility with Elxis 5.5 and option to submit reviews from IOS Rentals control panel.
    April 23, 2024
Is Open Source
This website uses cookies to improve user experience.
https://www.isopensource.com/inner.php/ajax