Rainbow to the pot of gold on an Arduino Uno
A WS2812B strip runs a flowing rainbow along an arch toward four LEDs shimmering gold in a pot at the end, with a knob for the speed. The whole build, an Arduino Uno and 5 more parts, 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $37. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino NeoPixel (WS2812B): Wiring and Code
What you'll learn
- Driving addressable LEDs with Adafruit NeoPixel
- Spreading hues evenly along a strip
- Animating by shifting a start offset
- Mapping a knob reading to a speed with
map
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
Two addressable LED lines are driven from the Uno. Pin 6 feeds the arch of 20 WS2812B LEDs and pin 5 feeds the pot of 4 LEDs, each through a 330 ohm resistor. A potentiometer on A0 sets the speed.
The rainbow works by moving where the color wheel starts. Each frame adds speed to start and spreads one full turn of hue across the 20 LEDs, so the colors flow along the arch. The four pot LEDs shimmer gold with a random flicker. A frame takes 20 ms.
Parts list
7 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit, MicroPython Pico Kit.
- 2 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 330 Ω (1) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 330 Ω (2) pin 1 | Arduino Uno R3 pin 5 |
| Potentiometer pin 3 | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs (1) pin VDD | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs (2) pin VDD | Arduino Uno R3 pin 5V |
| Potentiometer pin 1 | Arduino Uno R3 pin GND |
| Addressable RGB LEDs (1) pin VSS | Arduino Uno R3 pin GND |
| Addressable RGB LEDs (2) pin VSS | Arduino Uno R3 pin GND |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
| Addressable RGB LEDs (1) pin DIN | Resistor, 330 Ω (1) pin 2 |
| Addressable RGB LEDs (2) pin DIN | Resistor, 330 Ω (2) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Rainbow to the pot of gold: an arch of LEDs runs the rainbow toward a pot
// of gold that shimmers at the end, for St. Patrick's Day.
//
// rainbow 20 WS2812B LEDs, DIN <- 330 ohms <- pin 6
// gold 4 WS2812B LEDs in the pot, DIN <- 330 ohms <- pin 5
// knob potentiometer wiper -> A0, ends to 5 V and GND: the speed
//
// The arch is a strip bent over a doorway or along a shelf, and the pot is a
// little black cup with four LEDs in it under some gold paper coins. The
// legend says a pot of gold waits at the end of every rainbow; here it is
// always there.
//
// The rainbow moves by shifting where the color wheel starts. ColorHSV takes
// a hue from 0 to 65535 all the way round, so spreading one turn across the
// 20 LEDs and adding a little to the start every frame makes the colors flow
// along the arch. The knob sets how much is added, from a slow drift to a
// quick run, and the gold LEDs twinkle on their own.
//
// Things to try. Turn the knob. Make the arch flow the other way. Give the
// pot a green shimmer instead.
#include <Adafruit_NeoPixel.h>
const int ARCH = 20, POT = 4;
Adafruit_NeoPixel arch(ARCH, 6, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel gold(POT, 5, NEO_GRB + NEO_KHZ800);
uint16_t start = 0;
unsigned long frames = 0;
void setup() {
Serial.begin(9600);
arch.begin();
gold.begin();
arch.setBrightness(140);
gold.setBrightness(200);
Serial.println("Happy St. Patrick's Day! Follow the rainbow");
}
void loop() {
int speed = map(analogRead(A0), 0, 1023, 32, 1024);
start += speed;
for (int i = 0; i < ARCH; i++) {
uint16_t hue = start + (uint32_t)i * 65536 / ARCH;
arch.setPixelColor(i, arch.gamma32(arch.ColorHSV(hue)));
}
for (int i = 0; i < POT; i++) {
int glint = random(0, 10) == 0 ? 255 : random(140, 200);
gold.setPixelColor(i, glint, glint * 3 / 5, 0);
}
arch.show();
gold.show();
frames++;
if (frames % 200 == 1) {
Serial.print("Rainbow speed ");
Serial.println(speed);
}
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Opens serial, starts both strips with begin, sets brightness to 140 for the arch and 200 for the pot, and prints a greeting.
void setup() {
Serial.begin(9600);
arch.begin();
gold.begin();
arch.setBrightness(140);
gold.setBrightness(200);
Serial.println("Happy St. Patrick's Day! Follow the rainbow");
}loop sketch.ino
Reads the knob and maps it to a speed from 32 to 1024, then adds it to start. Each arch LED gets a hue from start plus its share of a full turn. Each gold LED gets a random warm glint. Both strips are sent with show, and a frame waits 20 ms.
void loop() {
int speed = map(analogRead(A0), 0, 1023, 32, 1024);
start += speed;
for (int i = 0; i < ARCH; i++) {
uint16_t hue = start + (uint32_t)i * 65536 / ARCH;
arch.setPixelColor(i, arch.gamma32(arch.ColorHSV(hue)));
}
for (int i = 0; i < POT; i++) {
int glint = random(0, 10) == 0 ? 255 : random(140, 200);
gold.setPixelColor(i, glint, glint * 3 / 5, 0);
}
arch.show();
gold.show();
frames++;
if (frames % 200 == 1) {
Serial.print("Rainbow speed ");
Serial.println(speed);
}
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LEDs show wrong colors This project
The strips are set to NEO_GRB at 800 kHz. A different LED type may need a different order. - Nothing lights This project
Check each strip's DIN comes through its 330 ohm resistor from pin 6 or pin 5, and VDD and VSS are connected. - The arch and pot seem swapped This project
The arch is on pin 6 with 20 LEDs and the pot is on pin 5 with 4. Check the pins and counts. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.