Built-in project · Arduino Uno R3

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 Runs in your browser. Free, and no account needed.

Lunar New Year lantern · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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
// 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();
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 6; Resistor, 330 Ω pin 1
  • Arduino Uno R3 pin 5V; Photoresistor sensor module pin VCC; Addressable RGB LEDs pin VDD
  • Ground: Arduino Uno R3 pin GND; Photoresistor sensor module pin GND; Addressable RGB LEDs pin VSS
  • Arduino Uno R3 pin A0; Photoresistor sensor module pin AO
  • Resistor, 330 Ω pin 2; Addressable RGB LEDs pin DIN

Change it and keep it

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