Crescent and star lantern on an Arduino Uno
Ten LEDs of a WS2812B ring glow as a warm crescent moon beside a twinkling LED star, and a light sensor lights the lantern at dusk, for Eid in any year. 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 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 with analogRead
- Two thresholds that stop the lantern from flickering at dusk
- Shaping brightness along a curve of addressable LEDs
- Using PWM with analogWrite for a twinkling LED
- Difficulty: Beginner
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
A light sensor module on A0 reads higher as the room gets darker. When the reading goes above 700, the lantern turns on: the crescent fades in over a moment, and the star starts to glow. When the reading drops below 600, both go out.
The crescent is the first 10 LEDs of a 16 LED WS2812B ring, warm in color and brightest in the middle of the curve. The star is a white LED on pin 5 that glows softly and flashes bright at random moments.
Parts list
7 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.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, whiteIn Starter Kit, Halloween Prop Kit, Logic Lab 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 |
| Resistor, 220 Ω pin 1 | Arduino Uno R3 pin 5 |
| 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 |
| LED, white pin C | Arduino Uno R3 pin GND |
| Photoresistor sensor module pin AO | Arduino Uno R3 pin A0 |
| Addressable RGB LEDs pin DIN | Resistor, 330 Ω pin 2 |
| LED, white pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Crescent and star lantern: a crescent moon of warm LEDs and a twinkling
// star, for a window or a table at Eid, lit by itself at dusk.
//
// light sensor module AO -> A0, VCC -> 5 V, GND -> GND
// crescent 16 WS2812B LEDs in a ring, DIN <- 330 ohms <- pin 6
// star pin 5 -> 220 ohms -> white LED -> GND
//
// Eid al-Fitr comes at the end of Ramadan, and Eid al-Adha about ten weeks
// later. Both follow the Islamic lunar calendar, which is about eleven days
// shorter than the solar year, so they move earlier each year and the start
// of each month waits on the new crescent moon. That is why the crescent and
// star are on so many Eid lanterns and cards, and why this sketch makes no
// claim about dates: it is a lantern for every year.
//
// The crescent is part of a ring: ten LEDs lit in a curve, brightest in the
// middle and fading toward the horns, with the other six dark. The star is
// one LED on a PWM pin that twinkles, a quick bright flash now and then over
// a soft glow.
//
// The light module's reading rises as the room gets darker, so the lantern
// lights when it goes above DARK_ABOVE and goes out in daylight.
//
// Things to try. Drag the light sensor's slider down. Make the moon fuller by
// lighting more of the ring with ARC.
#include <Adafruit_NeoPixel.h>
const int STAR_PIN = 5;
const int LEDS = 16;
const int ARC = 10; // LEDs in the crescent
const int DARK_ABOVE = 700; // A0 reading: on above this
const int LIGHT_BELOW = 600; // and off below this
Adafruit_NeoPixel moon(LEDS, 6, NEO_GRB + NEO_KHZ800);
bool lit = false;
int star = 0;
void drawMoon(int glow) {
moon.clear();
for (int i = 0; i < ARC; i++) {
// Brightest in the middle of the arc, dimmer toward the horns.
int fromMiddle = abs(2 * i - (ARC - 1));
int v = glow - fromMiddle * glow / (2 * ARC);
moon.setPixelColor(i, v, v * 3 / 4, v / 4);
}
moon.show();
}
void setup() {
Serial.begin(9600);
pinMode(STAR_PIN, OUTPUT);
moon.begin();
moon.setBrightness(170);
moon.show();
Serial.println("Eid Mubarak! The lantern lights at dusk");
}
void loop() {
int light = analogRead(A0);
if (!lit && light > DARK_ABOVE) {
lit = true;
// Fade the crescent in.
for (int g = 0; g <= 255; g += 5) {
drawMoon(g);
delay(20);
}
Serial.println("Dusk: the crescent and star are lit");
} else if (lit && light < LIGHT_BELOW) {
lit = false;
moon.clear();
moon.show();
analogWrite(STAR_PIN, 0);
Serial.println("Daylight: lantern off");
}
if (!lit) return;
// The star: a soft glow with a bright twinkle now and then.
if (random(0, 60) == 0) star = 255;
analogWrite(STAR_PIN, star < 60 ? 60 : star);
if (star > 60) star -= 12;
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
drawMoon sketch.ino
Clears the ring, then loops over the first ARC LEDs. Each one gets a value based on glow, reduced the further it is from the middle of the arc, so the horns are dimmer. The color has more red than green and little blue, which makes it warm.
void drawMoon(int glow) {
moon.clear();
for (int i = 0; i < ARC; i++) {
// Brightest in the middle of the arc, dimmer toward the horns.
int fromMiddle = abs(2 * i - (ARC - 1));
int v = glow - fromMiddle * glow / (2 * ARC);
moon.setPixelColor(i, v, v * 3 / 4, v / 4);
}
moon.show();
}setup sketch.ino
Starts serial, makes the star pin an output, and starts the ring with its brightness set to 170. It sends one show so the ring begins dark.
void setup() {
Serial.begin(9600);
pinMode(STAR_PIN, OUTPUT);
moon.begin();
moon.setBrightness(170);
moon.show();
Serial.println("Eid Mubarak! The lantern lights at dusk");
}loop sketch.ino
Reads the light sensor. Above DARK_ABOVE it fades the crescent in. Below LIGHT_BELOW it turns everything off. While lit, it sometimes sets star to 255 and lets it fall, with analogWrite never going below a soft glow of 60.
void loop() {
int light = analogRead(A0);
if (!lit && light > DARK_ABOVE) {
lit = true;
// Fade the crescent in.
for (int g = 0; g <= 255; g += 5) {
drawMoon(g);
delay(20);
}
Serial.println("Dusk: the crescent and star are lit");
} else if (lit && light < LIGHT_BELOW) {
lit = false;
moon.clear();
moon.show();
analogWrite(STAR_PIN, 0);
Serial.println("Daylight: lantern off");
}
if (!lit) return;
// The star: a soft glow with a bright twinkle now and then.
if (random(0, 60) == 0) star = 255;
analogWrite(STAR_PIN, star < 60 ? 60 : star);
if (star > 60) star -= 12;
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The lantern never turns on This project
Check the sensor's AO wire goes to A0, then drag the sensor's slider until the reading passesDARK_ABOVE. - Only part of the ring lights This project
That is intended.ARCis 10 of the 16 LEDs. Change it to light more. - 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. - The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.