← How-Tos
electronics 10 days ago ◑ 16 views ◯ 4 min read

Programming the RP2040/RP2350 PIO Peripheral: Writing Custom Hardware Protocols in PIO Assembly

electronicsraspberry pi picorp2040rp2350pio

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

RP2040RP2350
PIO blocks23
State machines per block44
Total state machines812
Instruction memory per block32 instructions32 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

Writing a PIO Program: The Toolchain

You have three realistic entry points, in order of how much control they give you:

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.