Pico LED chaser through an I2C expander
Build it step by step
0 of 7 doneWire the expander
VCC to 3V3(OUT), GND and address pins to GND, SDA to GP0, SCL to GP1.
Wire the LEDs
Anodes through 330 ohms to 3V3(OUT), cathodes to P0 to P7.
Wire the button
GP14 to GND.
Scan the bus
i2c.scan() shows 32, which is 0x20.
Save the program as main.py
On the Pico.
Run it
A light chases along the row.
Press the button
The pattern changes.
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
- Development BoardsBuy
- Integrated CircuitsBuy
- SemiconductorsBuyLED 5 mm red × 8
- PassivesBuyResistor 330 Ω × 8
- SwitchesBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| PCF8574 VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V |
| PCF8574 GND and A0, A1, A2 | Pico GND | Address 0x20 |
| PCF8574 SDA and SCL | Pico GP0 pin 1 and GP1 pin 2 | I2C data and clock |
| Eight LED anodes | 330 ohm resistor each to Pico 3V3(OUT) | LEDs are switched on by pulling the pin low |
| Eight LED cathodes | PCF8574 P0 to P7, one LED per pin | The chip sinks the current |
| Pattern button | Pico GP14 pin 19 and GND | Changes 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.
| Check | Expected |
|---|---|
| Start | One light moves along the eight LEDs |
| Button press | Bounce pattern, then blink |
| Order of lights | P0 to P7 left to right |
| Each LED | Lights fully with no dim glow |
| Scan | Address 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 →
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.
