diff --git a/i2c/pcf8574/README.adoc b/i2c/pcf8574/README.adoc new file mode 100644 index 0000000..ddd70de --- /dev/null +++ b/i2c/pcf8574/README.adoc @@ -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 <> 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 | +|=== diff --git a/i2c/pcf8574/i2c_get_address.py b/i2c/pcf8574/i2c_get_address.py new file mode 100644 index 0000000..25dd75d --- /dev/null +++ b/i2c/pcf8574/i2c_get_address.py @@ -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)') diff --git a/i2c/pcf8574/i2c_pcf8574_blink.py b/i2c/pcf8574/i2c_pcf8574_blink.py new file mode 100644 index 0000000..74d72ee --- /dev/null +++ b/i2c/pcf8574/i2c_pcf8574_blink.py @@ -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() \ No newline at end of file diff --git a/i2c/pcf8574/pico-and-io-expander.fzz b/i2c/pcf8574/pico-and-io-expander.fzz new file mode 100644 index 0000000..f4dc9ef Binary files /dev/null and b/i2c/pcf8574/pico-and-io-expander.fzz differ diff --git a/i2c/pcf8574/pico-and-io-expander.png b/i2c/pcf8574/pico-and-io-expander.png new file mode 100644 index 0000000..8e914b2 Binary files /dev/null and b/i2c/pcf8574/pico-and-io-expander.png differ