Home ESP32 ESP32 and PCF8574 expander example in micropython

ESP32 and PCF8574 expander example in micropython

by shedboy71

In this example we connect a PCF8574 expander to an ESP32 board, in this case we I used a Lolin32

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

pcf8574 pinout

pcf8574 pinout

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
Lolin32 WeMos Mini D1 LOLIN32 ESP32
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 PCF8574 module

esp32 and pcf8574 layout

esp32 and pcf8574 layout

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 device using the Thonny file

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(scl=Pin(22), sda=Pin(21))
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

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

255
0
170
85
255
0
170
85

 

You may also like

1 comment

Python Basics : While loop | About micros 8th October 2020 - 7:31 pm

[…] If you want to see the circuit, parts, and more this example is located at ESP32 and PCF8574 […]

Reply

Leave a Comment