Αρχική Blog PCF8574 chip, Python και Raspberry Pi 5

PCF8574 chip, Python και Raspberry Pi 5

PCF8574, Python και Raspberry Pi 5
PCF8574, Python και Raspberry Pi 5

Επικοινωνία I2C στο Raspberry Pi 5 με PCF8574 και 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.

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()
Το καλάθι είναι άδειο
  • Το καλάθι είναι άδειο
Σύνολο
0,00 EUR
https://www.isopensource.com/inner.php/el/shop/

Εργασίες

Έργα σε εξέλιξη

  • Open Form Manager
    A free open source component for managing forms
    92%
    22 Σεπτεμβρίου 2024

Ολοκληρωμένα έργα

  • e-Code module
    Display a block of code with optional highlight and copy to clipboard functionality.
    17 Αυγούστου 2024
  • FAQ v2.0
    Επανασχεδίαση του component FAQ, συμβατότητα με το Elxis 5
    17 Ιουνίου 2024
  • Open Shop 3.7
    Διασύνδεση με το ERP της SoftOne και το BoxNow
    08 Ιουνίου 2024
  • Sticky notes 2.0
    Module για συγγραφή σημειώσεων στην κονσόλα διαχείριση του Elxis
    26 Μαϊου 2024
  • Watch My Site 2.0
    Module επιτήρησης κατάστασης HTTP διακομιστή για εώς και 10 ιστότοπους
    23 Μαϊου 2024
  • IOS AERO v1.7
    Συμβατότητα με το Elxis 5.5
    24 Απριλίου 2024
  • IOS Hotels 3.4
    Συμβατότητα με το Elxis 5.5
    24 Απριλίου 2024
  • IOS Rentals v3.3
    Συμβατότητα με το Elxis 5.5 και δυνατότητα υποβολής κριτικών από τον πίνακα ελέγχου του IOS Rentals.
    23 Απριλίου 2024
  • IOS Rentals v3.2
    Βαθμολογία, κριτικές και σύστημα template
    25 Φεβρουαρίου 2024
  • IOS Rentals v3.1
    Νέα έκδοση για το λογισμικό ενοικίασης αυτοκινήτων. Η τιμή του οχήματος μπορεί αν διαφέρει αναλόγως της ηλικίας του οδηγού.
    25 Ιανουαρίου 2024
Is Open Source
Αυτός ο ιστότοπος χρησιμοποιεί cookies για να βελτιώσει την εμπειρία χρήσης.
https://www.isopensource.com/inner.php/el/ajax