Hanukkah menorah on an Arduino Uno
Nine flickering WS2812B lights, eight candles and the shamash: each press is the next night, the shamash lights first and then tonight’s candles, newest first, left to right. The whole build, an Arduino Uno and 3 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 $33. On Mokxi they're free.
What you'll learn
- Controlling addressable LEDs by index
- Randomness for a flickering flame
- Counting presses to track state
- Keeping animation alive while waiting
- Using arrays to map places to LEDs
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
Nine WS2812B LEDs make a menorah: eight candles and the shamash, the helper candle, as the middle LED. Each button press is the next night of Hanukkah, from night 1 to night 8.
On each night the shamash lights first, then the candles light one at a time from left to right, so the newest candle is lit first. Every flame flickers on its own. Night 1 uses the rightmost candle. After night 8, the next press turns everything off.
Parts list
5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × 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 Ω pin 1 | Arduino Uno R3 pin 6 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin DIN | Resistor, 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.
// Hanukkah menorah: nine flickering lights, eight candles and the shamash in
// the middle. Each press of the button is the next night: the shamash lights
// first, then it lights tonight's candles one by one, newest first.
//
// candles 9 WS2812B LEDs in a row, DIN <- 330 ohms <- pin 6
// (LED 4, the middle one, is the shamash)
// button pin 2 -> button -> GND (INPUT_PULLUP): the next night
//
// Hanukkah lasts eight nights, with one more candle each night. The candles
// go into the menorah from right to left: the first night's candle on the
// far right, the second night's just to its left, and so on. The shamash,
// the helper candle, is lit first every night and is used to light the
// others, and those are lit from left to right, so the newest candle is
// always lit first. The sketch follows that order exactly.
//
// The shamash stands apart from the eight. On this row it is the middle LED;
// in a real build, mount it higher than the others, as many menorahs do.
//
// Every flame flickers on its own, a warm white that dips now and then, and
// the shamash glows a little brighter. After the eighth night, the next press
// puts every light out, ready for next year.
//
// Things to try. Press the button for each night. Slow the lighting with
// LIGHT_MS so you can follow the order.
#include <Adafruit_NeoPixel.h>
const int LIGHTS = 9;
const int SHAMASH = 4;
const int BUTTON_PIN = 2;
const unsigned long LIGHT_MS = 600;
// The eight candle places, left to right, skipping the shamash.
const int PLACES[8] = {0, 1, 2, 3, 5, 6, 7, 8};
Adafruit_NeoPixel menorah(LIGHTS, 6, NEO_GRB + NEO_KHZ800);
bool lit[LIGHTS];
int night = 0;
void flame(int i) {
int v = random(0, 15) == 0 ? random(110, 170) : random(200, 256);
if (i == SHAMASH) v = v < 230 ? v + 25 : 255;
menorah.setPixelColor(i, v, v * 3 / 4, v / 4);
}
void draw() {
for (int i = 0; i < LIGHTS; i++) {
if (lit[i]) flame(i);
else menorah.setPixelColor(i, 0);
}
menorah.show();
}
// Wait while every lit flame keeps flickering.
void wait(unsigned long ms) {
unsigned long until = millis() + ms;
while (millis() < until) {
draw();
delay(40);
}
}
void lightNight(int n) {
for (int i = 0; i < LIGHTS; i++) lit[i] = false;
draw();
Serial.print("Night ");
Serial.print(n);
Serial.println(": the shamash first");
lit[SHAMASH] = true;
wait(LIGHT_MS);
// Tonight's candles are the n rightmost places, lit from the left.
for (int k = 8 - n; k < 8; k++) {
lit[PLACES[k]] = true;
Serial.print(" candle in place ");
Serial.println(k + 1);
wait(LIGHT_MS);
}
Serial.print(n);
Serial.println(n == 1 ? " candle and the shamash" : " candles and the shamash");
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
menorah.begin();
menorah.setBrightness(170);
menorah.show();
Serial.println("Happy Hanukkah! Press the button for the first night");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
night++;
if (night > 8) {
night = 0;
for (int i = 0; i < LIGHTS; i++) lit[i] = false;
draw();
Serial.println("All eight nights done. Until next year");
} else {
lightNight(night);
}
while (digitalRead(BUTTON_PIN) == LOW) draw();
}
draw();
delay(40);
}
Code walkthrough
The key functions of the sketch, and what each one does.
flame sketch.ino
Picks a random brightness, usually bright with an occasional dip, and sets the LED to a warm color from it. The shamash gets a little extra brightness.
void flame(int i) {
int v = random(0, 15) == 0 ? random(110, 170) : random(200, 256);
if (i == SHAMASH) v = v < 230 ? v + 25 : 255;
menorah.setPixelColor(i, v, v * 3 / 4, v / 4);
}draw sketch.ino
Goes through all nine LEDs. Lit ones get a fresh flame color and unlit ones are set to off. Then it sends the colors to the strip.
void draw() {
for (int i = 0; i < LIGHTS; i++) {
if (lit[i]) flame(i);
else menorah.setPixelColor(i, 0);
}
menorah.show();
}wait sketch.ino
A delay that keeps flickering. It redraws every 40 ms until the time is up, so lit candles never freeze while the next one is coming.
// Wait while every lit flame keeps flickering.
void wait(unsigned long ms) {
unsigned long until = millis() + ms;
while (millis() < until) {
draw();
delay(40);
}
}lightNight sketch.ino
Clears all lights, lights the shamash, then lights tonight's candles from the left with LIGHT_MS between each. It uses PLACES to skip the shamash position.
void lightNight(int n) {
for (int i = 0; i < LIGHTS; i++) lit[i] = false;
draw();
Serial.print("Night ");
Serial.print(n);
Serial.println(": the shamash first");
lit[SHAMASH] = true;
wait(LIGHT_MS);
// Tonight's candles are the n rightmost places, lit from the left.
for (int k = 8 - n; k < 8; k++) {
lit[PLACES[k]] = true;
Serial.print(" candle in place ");
Serial.println(k + 1);
wait(LIGHT_MS);
}
Serial.print(n);
Serial.println(n == 1 ? " candle and the shamash" : " candles and the shamash");
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The wrong LED is the shamash This project
The shamash is LED number 4, counting from 0. Make sure your strip's first LED is the one nearest DIN. - Nothing lights This project
Check DIN goes through the 330 ohm resistor from pin 6, with VDD on 5V. - The button seems stuck This project
The sketch keeps flickering while the button is held. Let go and press again for the next night. - 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. - 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.