Pico: blink two LEDs with PIO, the CPU idle
Hand-assemble a six-instruction PIO program and run it on two state machines, which blink two LEDs at their own rates while the CPU does nothing for them.
Firmware · Advanced
In a real product: Smart LED and motor-step timing (Robotics): PIO state machines make precise pulse trains (LED protocols, stepper steps) without CPU time.
The RP2040 has something few microcontrollers have: PIO, two blocks of four tiny state machines that run their own programs. Each instruction takes one clock cycle, so a PIO program can bit-bang a protocol (WS2812 LEDs, VGA, an extra UART) with exact timing while both CPU cores do something else.
Here a six-instruction PIO program blinks an LED. It is loaded once into PIO0's instruction memory and run on two state machines: one drives GP15 and one GP14, each at its own clock divider. After starting them, the CPU only counts and prints.
What you'll learn
- The PIO instruction format: three bits of opcode, five of delay, and the operands, assembled by hand
set pins,set xandjmp x--with a delay, the building blocks of a timed loop- State machine configuration: SET pins, wrap, and the clock divider
- Why a timing loop in PIO costs the CPU nothing
How it works
The program, with its encoding:
| Address | Instruction | Word |
|---|---|---|
| 0 | set pins, 1 | E001 |
| 1 | set x, 31 | E03F |
| 2 | jmp x--, 2 [31] | 1F42 |
| 3 | set pins, 0 | E000 |
| 4 | set x, 31 | E03F |
| 5 | jmp x--, 5 [31] | 1F45 |
SET is 111 in the top three bits, then the delay (bits 12–8), the destination (bits 7–5: 000 pins, 001 X) and a 5-bit value. JMP is 000, then the delay, the condition (010: "X not zero, then decrement") and the address. jmp x--, 2 [31] jumps to itself 32 times and waits 31 extra cycles each time: 32 × 32 = 1024 cycles.
So each half period is about 1026 state machine cycles. Wrap sends the program from address 5 back to 0 for free, so no jump is needed at the end. The SDK's pio_add_program moves the jump addresses to wherever the program lands.
A state machine's clock is the system clock divided by up to 65536. The Arduino core runs the RP2040 at 200 MHz, so with 65535 one PIO cycle is about 0.33 ms, and GP15 changes every ~0.34 s. The second state machine runs the same code at half that divider, so GP14 blinks twice as fast. The two state machines share the instructions but not their pins, X registers or clocks.
Try this
- Change the delay in
jmp x--, 2 [31]to[15](word0F42): the on time halves, the off time stays. - Start a third state machine on GP13 with a divider of 16384.
- Comment out
pio_sm_set_enabledfor state machine 1 and see that only GP15 blinks.
sketch.ino
// Raspberry Pi Pico: a hand-assembled PIO program blinks GP15 and GP14 from two state machines, the CPU idle.
#include "hardware/pio.h"
#include "hardware/clocks.h"
// set pins,1 / set x,31 / jmp x--,2 [31] / set pins,0 / set x,31 / jmp x--,5 [31]
static const uint16_t blink_code[] = { 0xE001, 0xE03F, 0x1F42, 0xE000, 0xE03F, 0x1F45 };
static const struct pio_program blink_program = { .instructions = blink_code, .length = 6, .origin = -1 };
PIO pio = pio0;
uint offset;
void start(uint sm, uint pin, float divider) {
pio_gpio_init(pio, pin); // the pin's function: PIO0
pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
pio_sm_config c = pio_get_default_sm_config();
sm_config_set_set_pins(&c, pin, 1); // "set pins" drives this one pin
sm_config_set_wrap(&c, offset + 0, offset + 5); // after 5, back to 0
sm_config_set_clkdiv(&c, divider);
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}
void setup() {
Serial.begin(115200);
offset = pio_add_program(pio, &blink_program);
start(0, 15, 65535.0f);
start(1, 14, 32767.5f);
Serial.printf("PIO program at %u, system clock %lu Hz\n", offset, (unsigned long)clock_get_hz(clk_sys));
}
void loop() {
static uint32_t spins = 0, last = 0;
spins++; // the CPU is free: count how often loop() runs
if (millis() - last >= 250) {
last = millis();
Serial.printf("GP15=%d GP14=%d loop ran %lu times\n", gpio_get(15), gpio_get(14), (unsigned long)spins);
}
}