Projects / Pico LED chaser through an I2C expander

Pico LED chaser through an I2C expander

Beginner2.2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. PICOPCF8574

    Wire the expander

    VCC to 3V3(OUT), GND and address pins to GND, SDA to GP0, SCL to GP1.

  2. PICO12.0 mA

    Wire the LEDs

    Anodes through 330 ohms to 3V3(OUT), cathodes to P0 to P7.

  3. PICO

    Wire the button

    GP14 to GND.

  4. PICO

    Scan the bus

    i2c.scan() shows 32, which is 0x20.

  5. PICO

    Save the program as main.py

    On the Pico.

  6. PICO

    Run it

    A light chases along the row.

  7. PICO

    Press the button

    The pattern changes.

In short

How do you build a pico LED chaser through an I2C expander?

A Pico drives eight LEDs through a PCF8574 expander using only two I2C wires, with chase patterns chosen by a button. You need Raspberry Pi Pico H, PCF8574 I/O expander, LED 5 mm red × 8, Resistor 330 Ω × 8, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 130 minutes.

Level
Beginner
Time
About 130 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
PCF8574 VCCPico 3V3(OUT) pin 36Power at 3.3 V
PCF8574 GND and A0, A1, A2Pico GNDAddress 0x20
PCF8574 SDA and SCLPico GP0 pin 1 and GP1 pin 2I2C data and clock
Eight LED anodes330 ohm resistor each to Pico 3V3(OUT)LEDs are switched on by pulling the pin low
Eight LED cathodesPCF8574 P0 to P7, one LED per pinThe chip sinks the current
Pattern buttonPico GP14 pin 19 and GNDChanges the pattern

Code

An I2C expander gives you extra pins using only two wires. The PCF8574 has eight pins that you set by writing one byte over I2C. Its outputs can pull a pin low with plenty of current but can only lift it high with a very small current, so LEDs are wired the other way round: the anode goes through a resistor to 3.3 V and the cathode to the pin, and the LED lights when the chip pulls the pin low. The program flips every bit before it writes, so in the program a one means on. Three pattern functions return the byte for each step, a single light moving along, a light bouncing back and forth and all lights blinking, and the button steps between them. The chip answers at address 0x20 when its three address pins are low, or at 0x38 for the A version of the chip. Each LED needs its own 330 ohm resistor.

import time
from machine import Pin, I2C

i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000)
ADDR = 0x20                                      # use 0x38 for a PCF8574A chip
button = Pin(14, Pin.IN, Pin.PULL_UP)

def show(pattern):
    # A 1 bit in pattern means LED on. The PCF8574 sinks current, so a 0 on the pin lights the LED.
    i2c.writeto(ADDR, bytes([(~pattern) & 0xFF]))

def chase(step):
    return 1 << (step % 8)

def bounce(step):
    pos = step % 14
    return 1 << (pos if pos < 8 else 14 - pos)

def blink(step):
    return 0xFF if step % 2 == 0 else 0x00

patterns = [chase, bounce, blink]
mode = 0
step = 0
prev = 1
while True:
    b = button.value()
    if prev == 1 and b == 0:
        mode = (mode + 1) % len(patterns)        # next pattern
        time.sleep_ms(40)
    prev = b
    show(patterns[mode](step))
    step += 1
    time.sleep_ms(120)

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
StartOne light moves along the eight LEDs
Button pressBounce pattern, then blink
Order of lightsP0 to P7 left to right
Each LEDLights fully with no dim glow
ScanAddress 32 appears

If it does not work

Nothing lights

Check the scan result and the address in the program.

LEDs inverted

Check the LED direction, the pin must be the cathode side.

Wrong order

Swap the LED wires to match P0 to P7.

Dim LEDs

Check each LED has its own resistor and the supply is 3.3 V.

Program does not start on power-up

Save it on the Pico as main.py.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pico LED chaser through an I2C expander?

Raspberry Pi Pico H, PCF8574 I/O expander, LED 5 mm red × 8, Resistor 330 Ω × 8, Tactile push button 6 mm and Breadboard 830 Tie Points.

How long does the pico LED chaser through an I2C expander take to build?

About 130 minutes for a beginner maker, in 7 steps.

How do I know the pico LED chaser through an I2C expander works?

Start: One light moves along the eight LEDs. Button press: Bounce pattern, then blink. Order of lights: P0 to P7 left to right.

Nothing lights: what should I check?

Check the scan result and the address in the program.

LEDs inverted: what should I check?

Check the LED direction, the pin must be the cathode side.

Wrong order: what should I check?

Swap the LED wires to match P0 to P7.

Dim LEDs: what should I check?

Check each LED has its own resistor and the supply is 3.3 V.