Lunar New Year lantern on an Arduino Uno
A ring of WS2812B LEDs inside a red paper lantern glows red with flickers of gold like a candle, and a light sensor turns it on at dusk. 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 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Reading a light sensor on an analog pin
- Hysteresis: two thresholds to avoid flicker
- Addressable LEDs with the NeoPixel library
- Random drifting for a natural flicker
- Timing with
millisinstead of waiting
- Difficulty: Beginner
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A light sensor module on A0 reads higher as the room gets darker. When the reading goes above DARK_ABOVE (700), the lantern turns on. It turns off again only when the reading falls below LIGHT_BELOW (600). Two different thresholds stop it from blinking at dusk.
While on, flicker runs about every 15 ms for the ring of 12 WS2812B LEDs on pin 6. Each LED drifts one step toward a random brightness between 110 and 255, so the glow breathes. The brightness is the red channel.
Now and then, about 1 frame in GOLD_CHANCE (20) for an LED, a gold timer starts and adds some green, so that LED warms toward gold.
Parts list
5 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Photoresistor sensor moduleIn Desk Buddy Kit, Escape Box Kit, First Inventions 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 |
| Photoresistor sensor module pin VCC | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Photoresistor sensor module pin GND | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Photoresistor sensor module pin AO | Arduino Uno R3 pin A0 |
| 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.
// Lunar New Year lantern: a red paper lantern with a ring of LEDs inside that
// glows and flickers like a candle, and turns itself on at dusk.
//
// light sensor module AO -> A0, VCC -> 5 V, GND -> GND
// LED ring 12 WS2812B LEDs, DIN <- 330 ohms <- pin 6, VDD -> 5 V, VSS -> GND
//
// Red lanterns are hung for Lunar New Year in many communities, and for the
// Lantern Festival that closes the celebration. This one is red and gold:
// every LED is a warm red, and now and then one warms toward gold, the way a
// flame wanders.
//
// Why it flickers this way: a real flame is never still, but it does not
// strobe either. Each LED drifts toward a new random brightness one step at a
// time, so the glow breathes.
//
// The light module's reading rises as the room gets darker. Two thresholds,
// one to turn on and a brighter one to turn off, keep it from blinking at
// dusk while the light hovers near one number.
//
// Things to try. Drag the light sensor's slider down to make it dusk. Change
// GOLD_CHANCE for a calmer or livelier flame.
#include <Adafruit_NeoPixel.h>
const int RING_PIN = 6;
const int LEDS = 12;
const int DARK_ABOVE = 700; // A0 reading: on above this (about 25 lux)
const int LIGHT_BELOW = 600; // and off below this
const int GOLD_CHANCE = 20; // 1 in this many frames, an LED warms toward gold
Adafruit_NeoPixel ring(LEDS, RING_PIN, NEO_GRB + NEO_KHZ800);
uint8_t level[LEDS], goal[LEDS], gold[LEDS];
bool lit = false;
unsigned long nextFrame = 0;
void setup() {
Serial.begin(9600);
ring.begin();
ring.setBrightness(170);
ring.show();
for (int i = 0; i < LEDS; i++) {
level[i] = 150;
goal[i] = 150;
}
Serial.println("Lantern ready: it lights when the room gets dark");
}
void flicker() {
for (int i = 0; i < LEDS; i++) {
if (level[i] == goal[i]) goal[i] = random(110, 256);
if (level[i] < goal[i]) level[i]++;
else level[i]--;
if (random(0, GOLD_CHANCE) == 0) gold[i] = 120;
if (gold[i] > 0) gold[i]--;
uint8_t v = level[i];
// Red, with some green mixed in for the gold moments.
ring.setPixelColor(i, v, (uint16_t)gold[i] * v / 255, 0);
}
}
void loop() {
int light = analogRead(A0);
if (!lit && light > DARK_ABOVE) {
lit = true;
Serial.println("Dusk: lantern on");
} else if (lit && light < LIGHT_BELOW) {
lit = false;
ring.clear();
ring.show();
Serial.println("Daylight: lantern off");
}
if (!lit || millis() < nextFrame) return;
nextFrame = millis() + 15;
flicker();
ring.show();
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
setup starts serial and the LED ring, sets the ring brightness to 170, and gives every LED a starting level of 150 so the glow begins at a middle value.
void setup() {
Serial.begin(9600);
ring.begin();
ring.setBrightness(170);
ring.show();
for (int i = 0; i < LEDS; i++) {
level[i] = 150;
goal[i] = 150;
}
Serial.println("Lantern ready: it lights when the room gets dark");
}flicker sketch.ino
flicker moves each LED one step toward its random goal and picks a new goal when it arrives. It sometimes starts a gold timer that fades. Each LED's color is red at its level, plus some green from the gold timer.
void flicker() {
for (int i = 0; i < LEDS; i++) {
if (level[i] == goal[i]) goal[i] = random(110, 256);
if (level[i] < goal[i]) level[i]++;
else level[i]--;
if (random(0, GOLD_CHANCE) == 0) gold[i] = 120;
if (gold[i] > 0) gold[i]--;
uint8_t v = level[i];
// Red, with some green mixed in for the gold moments.
ring.setPixelColor(i, v, (uint16_t)gold[i] * v / 255, 0);
}
}loop sketch.ino
loop reads the light and decides whether the lantern is on. If it is off, or 15 ms have not passed, it returns. Otherwise it calls flicker and shows the result.
void loop() {
int light = analogRead(A0);
if (!lit && light > DARK_ABOVE) {
lit = true;
Serial.println("Dusk: lantern on");
} else if (lit && light < LIGHT_BELOW) {
lit = false;
ring.clear();
ring.show();
Serial.println("Daylight: lantern off");
}
if (!lit || millis() < nextFrame) return;
nextFrame = millis() + 15;
flicker();
ring.show();
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The lantern never turns on. This project
It lights when the reading is above 700, which means dark. Drag the light sensor's slider down. - It blinks on and off at dusk. This project
KeepLIGHT_BELOWlower thanDARK_ABOVEso there is a gap between them. - The ring stays dark when it should glow. This project
The ring's DIN goes through the 330 ohm resistor to pin 6, and VDD to 5V and VSS to GND. - The digital output never changes, or is always on, no matter the light. Photoresistor sensor module
Turn the trimmer on the module until DO flips at the light level you want. The digital output is set by that threshold, and which direction means bright depends on the board. - Analog readings go down when it gets brighter, and the code expects the opposite. Photoresistor sensor module
The LDR sits in a divider, so the direction depends on how the module is built. Print the raw value in the light and dark, then flip it in code with 1023 minus the reading if needed. - 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.