Neopixel with RaspberryPI Pico

raspberrypi rpi pico rp2040 neopixel ws2812 c/c++

Pre-requisites

This code example creates a simple colored blinky with RaspberryPI Pico board with RP2040 microcontroller using the native C/C++ SDK. It shows how the first pixel is lighted with different colors with option A where a delay of 500ms is placed in between lighting each color. With Option B, where 500ms delay is removed, the cascading of pixel is shown.

Opion A: Displaying individual colors

Download Saleae Analyser File

Display 7 colors with 500ms delay

The logic analyzer traces show the 7 colors.

Display 7 colors with 500ms delay

The logic analyser traces show the Red LED signal zoomed in.

Zoomed in signal of the RED colored LED display

Option B: Cascading pixels

Download Saleae Analyser File

Display cascading color

The logic analyzer shows one signal that is cascading with multiple colors.

Display cascading colors

The logic analyzer shows a zoomed-in signal of the cascading colors.

Display cascading color

Code

Download code
#include <stdio.h>
#include <stdlib.h>

#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/clocks.h"
#include "ws2812.pio.h"

const int PIN_TX = 0;

static inline void put_pixel(uint32_t pixel_grb) {
  pio_sm_put_blocking(pio0, 0, pixel_grb << 8u);
}

static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) {
  return ((uint32_t)(r) << 8) |
         ((uint32_t)(g) << 16) |
         (uint32_t)(b);
}

int main() {
  stdio_init_all();

  PIO pio = pio0;
  int sm = 0;
  uint offset = pio_add_program(pio, &ws2812_program);
  char str[12];

  ws2812_program_init(pio, sm, offset, PIN_TX, 800000, true);

  while (true) {
    puts("Hello RGB and other colors!");

    // OPTION A: uncomment sleep_ms(500) and view the individual colors
    // OPTION B: comment sleep_ms(500) and view the pixels cascading colors
    put_pixel(urgb_u32(0xff, 0, 0));  // Red
    sleep_ms(500);
    put_pixel(urgb_u32(0, 0xff, 0));  // Green
    sleep_ms(500);
    put_pixel(urgb_u32(0, 0, 0xff));  // Blue
    sleep_ms(500);
    put_pixel(urgb_u32(0xff, 0xff, 0));  // Purple
    sleep_ms(500);
    put_pixel(urgb_u32(0, 0xff, 0xff));  // Cyan
    sleep_ms(500);
    put_pixel(urgb_u32(0xff, 0xff, 0xff));  // White
    sleep_ms(500);
    put_pixel(urgb_u32(0, 0, 0));  // Black or off
    sleep_ms(500);

    sleep_ms(1000);
    // Clear all pixels
    for (int i = 0; i <= 60; i++) {
      put_pixel(urgb_u32(0, 0, 0));  // Black or off
    }
    sleep_ms(1000);
  }
}

Makefile

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=c --filter=-legal/copyright,-build/include_subdir,-runtime/threadsafe_fn *.c

build:
	mkdir build
	cd build; cmake ..
	cd build; make -j4

all: clean build

flash:
	cp build/*.uf2 /Volumes/RPI-RP2
	sleep 2
	ls -al /dev/cu.usb*

clean:
	rm -rf build

Prototype

A photo of the actual setup.

Neopixel with RaspberryPI Pico prototype

Prototype

A photo of the actual setup.

Neopixel with RaspberryPI Pico prototype

Prototype

A photo of the actual setup.

Neopixel with RaspberryPI Pico prototype

Schematic

Wire up the hardware accordingly

Neopixel with RaspberryPI Pico schematic

References