Tutorials
Count PIO Instruction Cycles Before Choosing the RP2040 Clock Divider
Published · Updated
An RP2040 PIO state machine clocked at 10 MHz can produce a 1 MHz square wave without sending ten instructions to instruction memory. The delay fields consume state-machine cycles too.
Count the executed path from one rising edge to the next

Write the short program on paper. Mark instructions that change a pin, instructions that occupy time, and conditional branches that may take another path. Ordinary wait-free PIO instructions execute in one state-machine cycle plus any encoded delay.
Hardware wrap can return to the start without an explicit jump cycle. A blocking FIFO access or a WAIT whose condition has not arrived stretches the waveform beyond that nominal cycle count.
Look for missed trigger edges · Check CPOL and CPHA on the captured waveform
Balance five high cycles against five low cycles

Suppose clk_sys is 125 MHz. An SM divider of 12.5 gives an average 10 MHz state-machine rate. With the output pin configured for SET, execute an instruction to set it high with four additional delay cycles, then one to set it low with the same delay, using hardware wrap.
Ten cycles make a nominal 1 µs period. Because the divider is fractional, individual SM ticks can span different integer numbers of system-clock periods. If edge-to-edge jitter matters, compare an integer-divider design against the timing requirement.
Why does the measured low interval occasionally grow by many microseconds?
Inspect blocking pulls, waits and the executed branch path. Divider quantization can move an edge by system-clock ticks. A stalled state machine can wait until data or an external condition arrives.
Replay the worked example

- SET high with delay 4 · 1 + 4 · 0.5 µs
- SET low with delay 4 · 1 + 4 · 0.5 µs
- Hardware wrap · 10 total cycles · 1 MHz output
Separate throughput from transaction latency · Use the board bring-up checklist
Test a counter at its boundaries.
Raspberry Pi RP2040 Datasheet, chapter 3, Programmable I/O.
Photograph credit and licence
Dilshan Jayakody from Maharagama, Sri Lanka (Wikimedia Commons), CC BY-SA 2.0. Site-sized copy.
Original photograph https://commons.wikimedia.org/wiki/File:8_Channel_USB_logic_analyzer_(5172098012).jpg
Licence https://creativecommons.org/licenses/by-sa/2.0
Back to Tutorials