A PCF8574 expander example in micropython using a Raspberry Pi Pico

In this example we connect a PCF8574 expander to a Raspberry Pi Pico

Description

This 8-bit input/output (I/O) expander for the two-line bidirectional bus (I2C) is designed for 2.5-V to 6-V VCC operation.

The PCF8574 device provides general-purpose remote I/O expansion for most microcontroller families by way of the I2C interface [serial clock (SCL), serial data (SDA)].

The device features an 8-bit quasi-bidirectional I/O port (P0–P7), including latched outputs with high-current drive capability for directly driving LEDs. Each quasi-bidirectional I/O can be used as an input or output without the use of a data-direction control signal. At power on, the I/Os are high. In this mode, only a current source to VCC is active.

A0,A1,A2 are address pins
P0,P1,P2,P3,P4,P5,P6,P7 are digital I/O ports
SDA,SCL are the I2C pins

If we set pins A0 to A2 to GND, our device address in binary will be 0x20, thats exactly what I did in my example. To enable read and write there are different values required you can see these in the image below

 

Features

  • Low Standby-Current Consumption of 10 µA Max
  • I2C to Parallel-Port Expander
  • Open-Drain Interrupt Output
  • Compatible With Most Microcontrollers
  • Latched Outputs With High-Current Drive Capability for Directly Driving LEDs
  • Latch-Up Performance Exceeds 100 mA Per JESD 78, Class II

Parts Required

Various parts used in this example

Name Link
PCF8574 module PCF8574 IO Expansion Board I/O Expander
LED bar module 8 Bit LED Bar Marquee LED Display Module
Raspberry Pi Pico
Connecting cables Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

Schematic/Connection

Note that the PCF8574 is a current sink device so you do not require the current limiting resistors

I connected the LED board to the Raspberry Pi Pico module

I used GP8 – SDA, GP9 – SCL for the I2C connection

 

raspberry pi pico pinout

 

 

Code Example

I used Thonny for development and the library from https://github.com/mcauser/micropython-pcf8574

I copied the pcf8574.py from my PC to the Raspberry Pi Pico using the Thonny IDE

In this test example we flash various sequences on the LED's and then read in the port value

[codesyntax lang=”python”]

import pcf8574
from machine import I2C, Pin
import time

i2c = I2C(0)
pcf = pcf8574.PCF8574(i2c, 0x20)

while True:
    pcf.port = 0xff
    time.sleep(0.5)
    print(pcf.port)
    pcf.port = 0x00
    time.sleep(0.5)
    print(pcf.port)
    pcf.port = 0xAA
    time.sleep(0.5)
    print(pcf.port)
    pcf.port = 0x55
    time.sleep(0.5)
    print(pcf.port)

[/codesyntax]

Output

Run this example

Using the shell you will see the following values read in as well the LEDs flashing on and off

>>> %Run -c $EDITOR_CONTENT
255
0
170
85
255
0
170

 

Related posts

A Raspberry Pi Pico and KY-001 Temperature Sensor

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More