XIAO RP2040 NeoPixel
The XIAO RP2040 powers its own NeoPixel from GPIO 11 and fades it around the color wheel on GPIO 12. The whole build, a Seeed XIAO RP2040, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 5 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $10.40. On Mokxi they're free.
What you'll learn
- A board with a built-in addressable LED
- Switching a power pin before using a part
- Using the
Adafruit_NeoPixellibrary - Hue as a position on a color wheel
- Difficulty: Beginner
- Time: About 5 minutes
- Board: Seeed XIAO RP2040
- Parts: 2
How it works
The XIAO RP2040 has a NeoPixel built in, so this project needs no wiring. The NeoPixel is a WS2812 on GPIO 12. Its power is switched by GPIO 11, so the sketch turns that pin HIGH first. Without it, the LED stays dark however much data it is sent.
Then the sketch uses the Adafruit NeoPixel library to fade the one pixel around the color wheel. It steps the hue in jumps of 2048 out of 65536, shows each color, and waits 40 ms. When the hue reaches the end, it starts over.
Parts list
2 parts, plus the jumper wires. They retail for about $10.40; on Mokxi they're free.
- 1 × Seeed XIAO RP2040
- 1 × Full-size breadboard
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// NeoPixel: the XIAO RP2040's own RGB LED.
//
// The NeoPixel is a WS2812 on GPIO 12 (PIN_NEOPIXEL), and its supply is
// switched by GPIO 11 (NEOPIXEL_POWER): drive that HIGH first, or the LED
// stays dark however much data it is sent, as on the real board.
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);
pixel.begin();
pixel.setBrightness(60);
}
void loop() {
for (long hue = 0; hue < 65536; hue += 2048) {
pixel.setPixelColor(0, pixel.ColorHSV(hue));
pixel.show();
delay(40);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes NEOPIXEL_POWER an output and drives it HIGH to power the LED. Then pixel.begin() starts the library and setBrightness(60) keeps the LED from being too bright.
void setup() {
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);
pixel.begin();
pixel.setBrightness(60);
}loop sketch.ino
A for loop runs hue from 0 up to 65536 in steps of 2048. Each time, ColorHSV(hue) makes a color, setPixelColor sets it, and show sends it. A 40 ms delay sets the speed of the fade.
void loop() {
for (long hue = 0; hue < 65536; hue += 2048) {
pixel.setPixelColor(0, pixel.ColorHSV(hue));
pixel.show();
delay(40);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED stays dark This project
The power pin must be set HIGH before sending any color. Keep those two lines insetup. - The colors change too fast or too slow This project
Change the 40 msdelayinloop, or the 2048 step size. - Wrong pin lights up, because the D numbers and the GPIO numbers are mixed. Seeed XIAO RP2040
The XIAO has silkscreen pins D0 to D10 that map to different RP2040 GPIO numbers. Use the pin names from your board package or look up the mapping. - The user LED seems backwards: on when the pin is low. Seeed XIAO RP2040
The three-color user LED is wired to 3.3 V, so it lights when the pin is LOW. Write LOW to turn a color on and HIGH to turn it off.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.