Try Embedded

Pico: drive a NeoPixel ring with a four-instruction PIO program

Hand-assemble the WS2812 PIO program, feed it 24-bit colours through the TX FIFO, and spin a comet round a ring of eight NeoPixels at 800 kHz while the CPU…

Output · Advanced

In a real product: Addressable LED status rings (Consumer electronics): Smart speakers, chargers and robots show their state on rings of WS2812 LEDs driven by a timer or PIO peripheral.

A WS2812 LED (a NeoPixel) has one data input and no clock. Each bit is a pulse on that one wire: a short high pulse for a 0 and a long one for a 1, 1.25 µs per bit, 24 bits per LED. Each LED keeps the first 24 bits it gets and passes the rest on to the next. Bit-banging that with exact timing ties up a CPU. On the RP2040 a PIO state machine makes the pulses, and the CPU only writes one word per LED.

Here a four-instruction PIO program drives a ring of eight LEDs on GP16. The CPU draws a comet: a bright head and a fading tail, one step round the ring every 80 ms.

What you'll learn

  • The WS2812 bit timing, and how side-set changes a pin in the same cycle as an instruction
  • out with autopull: the state machine refills its shift register from the TX FIFO by itself
  • Setting the clock divider so that one bit is exactly 10 PIO cycles at 800 kHz
  • Why the CPU's only job is pio_sm_put_blocking() once per LED

How it works

The program, with its encoding (one side-set bit, so the delay field has four bits):

AddressInstructionWordCycles
0out x, 1 side 0 [2]62213, pin low
1jmp !x, 3 side 1 [1]11232, pin high
2jmp 0 side 1 [4]14005, pin high: a 1
3nop side 0 [4]A4425, pin low: a 0

Every bit starts with the pin low for 3 cycles while out shifts the next bit into X. Then the pin goes high for 2 cycles. For a 1 it stays high for 5 more, for a 0 it goes low for those 5. Each bit is 10 cycles either way, and wrap goes from address 3 back to 0. So the high time is 7 cycles for a 1 and 2 for a 0.

The divider makes those cycles the right length: 200 MHz / (800 kHz × 10) = 25, so one cycle is 125 ns. A 1 is high for 875 ns and a 0 for 250 ns, inside the WS2812's limits.

The state machine shifts left (most significant bit first) and autopulls after 24 bits. The CPU writes each colour as GRB << 8, the order WS2812s expect, in the top 24 bits of a word. When the FIFO is empty, the program stalls on out with the pin low. A low longer than 50 µs tells the LEDs that the frame has ended.

Try this

  • Change the divider to 50. The bits are twice as long and out of the WS2812's timing: what do the LEDs show?
  • Make the tail longer, with a gentler fade (>> 1 instead of >> 2).
  • Add a second ring on GP17 with a second state machine running the same program.
  • Send 0x00FF0000 to every LED, then 0xFF000000. Which one is green?

sketch.ino

// Raspberry Pi Pico: eight WS2812 LEDs on GP16 driven by a hand-assembled PIO program at 800 kHz; the CPU writes one
// 24-bit colour per LED into the state machine's TX FIFO and draws a comet going round the ring.
#include "hardware/pio.h"
#include "hardware/clocks.h"

const uint PIN = 16, LEDS = 8;

// out x,1 side 0 [2] / jmp !x,3 side 1 [1] / jmp 0 side 1 [4] / nop side 0 [4]
static const uint16_t ws2812_code[] = { 0x6221, 0x1123, 0x1400, 0xA442 };
static const struct pio_program ws2812_program = { .instructions = ws2812_code, .length = 4, .origin = -1 };

PIO pio = pio0;
uint sm = 0;

void ws2812_init(uint pin, float bitrate) {
  uint offset = pio_add_program(pio, &ws2812_program);
  pio_gpio_init(pio, pin);
  pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  pio_sm_config c = pio_get_default_sm_config();
  sm_config_set_wrap(&c, offset + 0, offset + 3);
  sm_config_set_sideset(&c, 1, false, false);          // one side-set bit, not optional, not pindirs
  sm_config_set_sideset_pins(&c, pin);
  sm_config_set_out_shift(&c, false, true, 24);         // shift left, autopull every 24 bits
  sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);        // an 8-word TX FIFO
  sm_config_set_clkdiv(&c, clock_get_hz(clk_sys) / (bitrate * 10));
  pio_sm_init(pio, sm, offset, &c);
  pio_sm_set_enabled(pio, sm, true);
}

// One LED's colour, in the order the WS2812 takes it: green, red, blue, in the word's top 24 bits.
void put_pixel(uint8_t r, uint8_t g, uint8_t b) {
  pio_sm_put_blocking(pio, sm, ((uint32_t)g << 24) | ((uint32_t)r << 16) | ((uint32_t)b << 8));
}

void setup() {
  Serial.begin(115200);
  ws2812_init(PIN, 800000);
  Serial.printf("WS2812 on GP%u, PIO divider %.1f\n", PIN, clock_get_hz(clk_sys) / 8000000.0f);
}

void loop() {
  static uint head = 0, frames = 0;
  for (uint i = 0; i < LEDS; i++) {
    uint behind = (head + LEDS - i) % LEDS;              // 0 at the head, 1 just behind it, ...
    uint8_t level = behind < 4 ? 255 >> (2 * behind) : 0;
    put_pixel(level, level / 3, 255 - level);            // an orange comet on a blue ring
  }
  head = (head + 1) % LEDS;
  if (++frames % 10 == 0) Serial.printf("frame %u, head at LED %u\n", frames, head);
  delay(80);                                             // > 50 us low: the LEDs latch the frame
}

Lessons behind it