Skip to content

Add pcf8574 example #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions i2c/pcf8574/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
= Using a PCF8574 remote 8-Bit I/O expander over I2C to get more IO pins
:xrefstyle: short

This example demonstrates how to use the PCF8574 Remote 8-Bit I/O Expander with the Raspberry Pi Pico over the I2C bus. The code toggles the pins of the PCF8574 sequentially, turning them on and off one by one.

== Wiring information

See <<pcf8574-wiring-diagram>> for wiring instructions.

[[pcf8574-wiring-diagram]]
[pdfwidth=75%]
.Wiring the PCF8574 to Pico using I2C
image::pico-and-io-expander.png[]

== List of Files

A list of files with descriptions of their function;

i2c_pcf8574_blink.py:: The example code.
i2c_get_address.py:: Some code to detect the I2C address of the expander board.

== Bill of Materials

.A list of materials required for the example
[[pcf8574-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | https://www.raspberrypi.com/products/raspberry-pi-pico/
| PCF8574 I/O expander board | 1 |
| LEDs or other things to control | 8 |
|===
8 changes: 8 additions & 0 deletions i2c/pcf8574/i2c_get_address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import machine

# Initialize I2C bus on pins 16 (SDA) and 17 (SCL) with a frequency of 400kHz
i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)

# print the I2C address
print('I2C address:')
print(hex(i2c.scan()[0]), ' (hex)')
32 changes: 32 additions & 0 deletions i2c/pcf8574/i2c_pcf8574_blink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Add more IO Pins via a PCF8574 Remote 8-Bit I/O Expander

from machine import Pin, I2C
import utime

# Initialize I2C bus on pins 16 (SDA) and 17 (SCL) with a frequency of 400kHz
i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)

# Address of the PCF8574 I/O expander on the I2C bus
address = 0x20

def toggle_pins():
try:
for i in range(8):
# Create a bitmask to set each pin high one at a time
pin_state = 1 << i

# Write the bitmask to the PCF8574
i2c.writeto(address, bytearray([pin_state]))

# Sleep for 200ms to keep the pin high
utime.sleep(0.2)

# Reset all pins to low
i2c.writeto(address, bytearray([0x00]))
except OSError as e:
# Print an error message if there is an issue accessing the I2C device
print("Error accessing the I2C device:", e)

# Continuously toggle the pins
while True:
toggle_pins()
Binary file added i2c/pcf8574/pico-and-io-expander.fzz
Binary file not shown.
Binary file added i2c/pcf8574/pico-and-io-expander.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.