Programming the RP2040/RP2350 PIO Peripheral: Writing Custom Hardware Protocols in PIO Assembly
Most microcontrollers give you a fixed menu of hardware peripherals — this many SPI buses, this many UARTs, take it or leave it. The Raspberry Pi Pico's RP2040 and RP2350 chips do something different: alongside their normal peripherals, they include PIO (Programmable I/O), a small, fast, purpose-built set of state machines you program yourself to implement almost any bit-level protocol in hardware-timed, deterministic code. This guide covers what PIO actually is, when to reach for it instead of bit-banging or a software driver, and how its two chip generations differ.
What PIO Actually Is
Each PIO block contains several independent state machines, and each state machine runs a tiny, specialized instruction set — not general-purpose C code, but a dedicated assembly language with around a dozen instructions (things like shifting data in/out, waiting on a GPIO pin state, jumping based on a pin or internal condition) built specifically for generating and reading precisely-timed digital signals. Because PIO instructions execute on dedicated hardware separate from the chip's main ARM or RISC-V cores, a PIO program keeps running with cycle-accurate timing regardless of what your main program is doing — no interrupt latency, no scheduler jitter, the core problem that makes software bit-banging unreliable for timing-sensitive protocols.
RP2040 vs RP2350: What Changed
| RP2040 | RP2350 | |
|---|---|---|
| PIO blocks | 2 | 3 |
| State machines per block | 4 | 4 |
| Total state machines | 8 | 12 |
| Instruction memory per block | 32 instructions | 32 instructions, with new instruction variants added to the set |
| New PIO instructions/features | — | Additional instructions (expanded IRQ flexibility and other refinements) and GPIO range improvements to support the RP2350's larger GPIO count on the QFN-80 variant |
The practical upshot: RP2350-based boards (Pico 2 and Pico 2 W) give you half again as many state machines to work with, which matters once a project wants to run several independent PIO-driven protocols simultaneously — say, driving an addressable LED strip, reading a quadrature encoder, and generating a custom serial protocol all at once without them contending for the same handful of state machines.
Common Things People Build With PIO
- Driving addressable LEDs (WS2812B/NeoPixel). The canonical PIO example: generating the precise, sub-microsecond timing these protocols need, something that's genuinely difficult to do reliably from software bit-banging on a general-purpose core.
- Custom or unusual serial protocols. Any device with a non-standard bit-level protocol that doesn't match UART/SPI/I2C — PIO lets you implement it in hardware-timed code rather than hoping software bit-banging (covered elsewhere on this site) keeps up.
- Extra UART, SPI, or I2C peripherals. When a project needs more serial peripherals than the chip's dedicated hardware blocks provide, PIO can implement additional instances.
- Logic analyzer-style capture. PIO can sample GPIO states at precise, high-speed intervals and feed them to DMA, turning the chip into a basic logic capture tool.
- Video-adjacent signal generation. Community projects have used PIO to generate VGA and DVI-class video timing signals, well beyond what the chip's intended peripheral set was designed for.
Writing a PIO Program: The Toolchain
You have three realistic entry points, in order of how much control they give you:
- MicroPython's
rp2.asm_piodecorator lets you write PIO assembly inline in a MicroPython script and load it onto a state machine with a few lines of Python — by far the fastest way to experiment, and good enough for most hobby projects. - The C SDK with
pioasmcompiles standalone PIO assembly files into headers you include in a C/C++ project — the path most existing PIO example libraries (including the official WS2812 example) ship as. - Hand-assembling or using higher-level PIO generator tools for the rare case where you're pushing against the instruction set's limits and need full control over every cycle.
Start with MicroPython if you're new to PIO — being able to iterate on a PIO program in seconds rather than a full C recompile cycle makes the learning curve much shorter, and you can always port a working MicroPython PIO program to C later if you need the performance headroom of running the rest of your application in C.
A Simple Mental Model Before You Dive In
Think of a PIO state machine as a tiny, extremely fast shift register with a few conditional instructions, fed by (and feeding) a FIFO that your main CPU core writes to and reads from via DMA or direct access. Your PIO program's job is almost always some variant of "wait for a condition on a pin, shift a bit in or out, repeat" — once you see a few example programs (the official pico-examples repository's PIO folder is the best starting point) through that lens, the instruction set's small size stops feeling limiting and starts feeling like exactly enough to do the job.
When Not to Reach for PIO
If your protocol is already supported by the chip's dedicated SPI, I2C, or UART hardware, use that instead — PIO is for filling gaps, not replacing peripherals that already do the job correctly and with far less development effort. PIO programs are also genuinely fiddly to debug compared to C or MicroPython running on the main core; budget real time to work through timing edge cases with a logic analyzer if you're implementing anything beyond a well-documented existing example.