added pcf8574 example
rodzic
712964b1d9
commit
2d62aed437
|
@ -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 |
|
||||
|===
|
|
@ -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)')
|
|
@ -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()
|
Plik binarny nie jest wyświetlany.
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 175 KiB |
Ładowanie…
Reference in New Issue