Built-in project · Arduino Uno R3

Holi color mixer on an Arduino Uno

Three knobs mix red, green and blue light into any color on a ring of WS2812B LEDs, and a button throws a splash of bright Holi colors across it. The whole build, an Arduino Uno and 6 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 25 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Mixing red, green and blue light additively
  • Reading three analog inputs and scaling them to 0 to 255
  • Setting each LED of a ring with setPixelColor
  • Using random to create a splash effect
  • Difficulty: Beginner
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

Three potentiometers on A0, A1 and A2 set the red, green and blue levels. Each reading is divided by 4 to fit the 0 to 255 range, and the result is shown on all 12 LEDs of a WS2812B ring. Light adds, so red plus green looks yellow.

Pressing the button on pin 2 starts a splash. Eight random LEDs each get a random color from the SPLASH list and hold it for 20 to 59 frames. After that they return to your mix. The sketch also prints the mix over serial when it changes.

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 pinConnects to
Resistor, 330 Ω pin 1Arduino Uno R3 pin 6
Pushbutton pin 1aArduino Uno R3 pin 2
Potentiometer (1) pin 3Arduino Uno R3 pin 5V
Potentiometer (2) pin 3Arduino Uno R3 pin 5V
Potentiometer (3) pin 3Arduino Uno R3 pin 5V
Addressable RGB LEDs pin VDDArduino Uno R3 pin 5V
Potentiometer (1) pin 1Arduino Uno R3 pin GND
Potentiometer (2) pin 1Arduino Uno R3 pin GND
Potentiometer (3) pin 1Arduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin GND
Addressable RGB LEDs pin VSSArduino Uno R3 pin GND
Potentiometer (1) pin 2Arduino Uno R3 pin A0
Potentiometer (2) pin 2Arduino Uno R3 pin A1
Potentiometer (3) pin 2Arduino Uno R3 pin A2
Addressable RGB LEDs pin DINResistor, 330 Ω pin 2

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Holi color-mixing light: three knobs mix red, green and blue light into any
// color on a ring of LEDs, and a button throws a splash of bright colors
// across it, the way gulal powder flies at Holi.
//
//   knobs     three potentiometers, wipers -> A0 (red), A1 (green), A2 (blue)
//   button    pin 2 -> button -> GND        (INPUT_PULLUP)
//   ring      12 WS2812B LEDs, DIN <- 330 ohms <- pin 6
//
// Holi, the festival of colors, welcomes spring with friends and family
// throwing colored powder and water at each other until everyone is every
// color at once. Light mixes differently from powder: paint and powder get
// darker as you mix them, but light adds up. Red and green light make yellow,
// red and blue make magenta, green and blue make cyan, and all three make
// white. Turn the knobs and see.
//
// The splash picks random colors from a palette of bright Holi shades and
// lands each one on a random LED, then the ring fades back to your mix.
//
// Things to try. Turn red and green up with blue down: yellow. Press the
// button for a splash. Add your favorite color to SPLASH.

#include <Adafruit_NeoPixel.h>

const int BUTTON_PIN = 2;
const int LEDS = 12;
Adafruit_NeoPixel ring(LEDS, 6, NEO_GRB + NEO_KHZ800);

const uint32_t SPLASH[] = {0xFF1493, 0xFFD700, 0x00C853, 0x2979FF, 0xFF6D00, 0xAA00FF, 0x00E5FF};
const int SHADES = sizeof(SPLASH) / sizeof(SPLASH[0]);

uint8_t splashLeft[LEDS];   // frames each LED keeps its splash color
uint32_t splashColor[LEDS];
uint32_t lastMix = 0xFFFFFFFF;
int splashes = 0;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  ring.begin();
  ring.setBrightness(160);
  Serial.println("Holi color mixer: turn the knobs, press for a splash");
}

void loop() {
  uint8_t r = analogRead(A0) / 4;
  uint8_t g = analogRead(A1) / 4;
  uint8_t b = analogRead(A2) / 4;
  uint32_t mix = ring.Color(r, g, b);

  if (digitalRead(BUTTON_PIN) == LOW) {
    splashes++;
    for (int n = 0; n < 8; n++) {
      int i = random(0, LEDS);
      splashColor[i] = SPLASH[random(0, SHADES)];
      splashLeft[i] = random(20, 60);
    }
    Serial.print("Splash ");
    Serial.println(splashes);
    while (digitalRead(BUTTON_PIN) == LOW) delay(10);
  }

  for (int i = 0; i < LEDS; i++) {
    if (splashLeft[i] > 0) {
      splashLeft[i]--;
      ring.setPixelColor(i, splashColor[i]);
    } else {
      ring.setPixelColor(i, mix);
    }
  }
  ring.show();

  // Report the mix when it changes by more than a little.
  if ((mix >> 20) != (lastMix >> 20) || ((mix >> 12) & 0xF) != ((lastMix >> 12) & 0xF) || ((mix >> 4) & 0xF) != ((lastMix >> 4) & 0xF)) {
    lastMix = mix;
    Serial.print("Mix red ");
    Serial.print(r);
    Serial.print(" green ");
    Serial.print(g);
    Serial.print(" blue ");
    Serial.println(b);
  }
  delay(20);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial, sets the button pin to INPUT_PULLUP, and starts the ring at a brightness of 160. Brightness is capped so the ring is comfortable to look at and easier on the power supply.

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  ring.begin();
  ring.setBrightness(160);
  Serial.println("Holi color mixer: turn the knobs, press for a splash");
}

loop sketch.ino

Reads the three knobs and builds the mix color. If the button is pressed, it picks 8 random LEDs, gives each a random shade from SPLASH and a random countdown, then waits for release. Then it draws: an LED with frames left shows its splash color and counts down, and the others show the mix.

void loop() {
  uint8_t r = analogRead(A0) / 4;
  uint8_t g = analogRead(A1) / 4;
  uint8_t b = analogRead(A2) / 4;
  uint32_t mix = ring.Color(r, g, b);

  if (digitalRead(BUTTON_PIN) == LOW) {
    splashes++;
    for (int n = 0; n < 8; n++) {
      int i = random(0, LEDS);
      splashColor[i] = SPLASH[random(0, SHADES)];
      splashLeft[i] = random(20, 60);
    }
    Serial.print("Splash ");
    Serial.println(splashes);
    while (digitalRead(BUTTON_PIN) == LOW) delay(10);
  }

  for (int i = 0; i < LEDS; i++) {
    if (splashLeft[i] > 0) {
      splashLeft[i]--;
      ring.setPixelColor(i, splashColor[i]);
    } else {
      ring.setPixelColor(i, mix);
    }
  }
  ring.show();

  // ... 12 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • One color never changes This project
    Check that each knob's middle pin goes to its own analog pin: A0 red, A1 green, A2 blue.
  • The ring shows nothing This project
    Check the 330 ohm resistor sits between pin 6 and DIN, and that the ring has 5 V and ground.
  • 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 input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.