Ofrenda candles on an Arduino Uno
Six LED candles for a Día de los Muertos ofrenda, one for each person remembered: a button lights one at a time, and each flickers on its own PWM pin like a real flame. The whole build, an Arduino Uno and 13 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
- Fading LEDs with PWM and analogWrite
- Random numbers to make natural flicker
- Arrays to drive several pins in a loop
- A button with INPUT_PULLUP
- Keeping animation going during a button press
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 15
How it works
Six yellow LEDs stand in for candles on an ofrenda, the altar for Día de los Muertos. Each LED has its own PWM pin: 3, 5, 6, 9, 10 and 11. Press the button on pin 2 and one more candle lights.
A lit candle never holds one brightness. Every few milliseconds each picks a new level, usually bright, with an occasional deeper dip. Each candle chooses on its own, so they do not flicker together.
After the sixth press, one more press fades all the candles out together, and you can start again.
Parts list
15 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 × Full-size breadboard
- 6 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 6 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
Kits that can build it
These 12 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- ELEGOO UNO R3 Basic Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- LAFVIN Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic 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, 220 Ω (6) pin 1 | Arduino Uno R3 pin 11 |
| Resistor, 220 Ω (5) pin 1 | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω (4) pin 1 | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 5 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 3 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| LED, yellow (1) pin C | Arduino Uno R3 pin GND |
| LED, yellow (2) pin C | Arduino Uno R3 pin GND |
| LED, yellow (3) pin C | Arduino Uno R3 pin GND |
| LED, yellow (4) pin C | Arduino Uno R3 pin GND |
| LED, yellow (5) pin C | Arduino Uno R3 pin GND |
| LED, yellow (6) pin C | Arduino Uno R3 pin GND |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| LED, yellow (1) pin A | Resistor, 220 Ω (1) pin 2 |
| LED, yellow (2) pin A | Resistor, 220 Ω (2) pin 2 |
| LED, yellow (3) pin A | Resistor, 220 Ω (3) pin 2 |
| LED, yellow (4) pin A | Resistor, 220 Ω (4) pin 2 |
| LED, yellow (5) pin A | Resistor, 220 Ω (5) pin 2 |
| LED, yellow (6) pin A | Resistor, 220 Ω (6) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Ofrenda candles: six candle lights for an ofrenda, the home altar of Día de
// los Muertos. Press the button to light one candle at a time, one for each
// person you are remembering, and each one flickers like a real flame.
//
// candles pins 3, 5, 6, 9, 10, 11 -> 220 ohms -> yellow LED -> GND, six
// button pin 2 -> button -> GND (INPUT_PULLUP)
//
// On Día de los Muertos, on November 1st and 2nd, families in Mexico and
// around the world welcome back the spirits of loved ones who have died. The
// ofrenda holds their photos, their favorite foods, marigolds, sugar skulls
// and candles, often one for each person, to light their way home.
//
// Every candle has its own PWM pin, the six an Uno has, so each can flicker
// on its own: every few milliseconds it picks a brightness near the top,
// with an occasional deeper dip, so no two candles move together. LEDs are a
// good choice for an ofrenda full of paper decorations and dry flowers.
//
// Press the button once more after the sixth and they all fade out together.
//
// Things to try. Press the button six times. Make the flames calmer by
// raising the lowest brightness in flicker().
const int CANDLES = 6;
const int PINS[CANDLES] = {3, 5, 6, 9, 10, 11};
const int BUTTON_PIN = 2;
int lit = 0;
void flicker() {
for (int i = 0; i < CANDLES; i++) {
if (i < lit) {
int v = random(0, 12) == 0 ? random(60, 140) : random(170, 256);
analogWrite(PINS[i], v);
} else {
analogWrite(PINS[i], 0);
}
}
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
for (int i = 0; i < CANDLES; i++) pinMode(PINS[i], OUTPUT);
Serial.println("Ofrenda ready: press the button to light a candle");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
if (lit < CANDLES) {
lit++;
Serial.print("Candle ");
Serial.print(lit);
Serial.println(" lit");
} else {
for (int v = 200; v >= 0; v -= 5) {
for (int i = 0; i < CANDLES; i++) analogWrite(PINS[i], v);
delay(20);
}
lit = 0;
Serial.println("All candles out");
}
while (digitalRead(BUTTON_PIN) == LOW) flicker();
delay(50);
}
flicker();
delay(random(40, 90));
}
Code walkthrough
The key functions of the sketch, and what each one does.
flicker sketch.ino
Goes through all six candles. If a candle's number is below lit, it picks a brightness. One time in twelve it dips to a value between 60 and 139. Otherwise it picks between 170 and 255. Unlit candles are set to 0.
void flicker() {
for (int i = 0; i < CANDLES; i++) {
if (i < lit) {
int v = random(0, 12) == 0 ? random(60, 140) : random(170, 256);
analogWrite(PINS[i], v);
} else {
analogWrite(PINS[i], 0);
}
}
}setup sketch.ino
Starts serial, sets the button to INPUT_PULLUP and makes all six candle pins outputs. Then it prints a hint.
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
for (int i = 0; i < CANDLES; i++) pinMode(PINS[i], OUTPUT);
Serial.println("Ofrenda ready: press the button to light a candle");
}loop sketch.ino
On a press, it either lights the next candle or, if all six are lit, fades them all from 200 to 0 and resets lit. While the button is held it keeps calling flicker. Each pass ends with a random wait of 40 to 89 ms.
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
if (lit < CANDLES) {
lit++;
Serial.print("Candle ");
Serial.print(lit);
Serial.println(" lit");
} else {
for (int v = 200; v >= 0; v -= 5) {
for (int i = 0; i < CANDLES; i++) analogWrite(PINS[i], v);
delay(20);
}
lit = 0;
Serial.println("All candles out");
}
while (digitalRead(BUTTON_PIN) == LOW) flicker();
delay(50);
}
flicker();
delay(random(40, 90));
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A candle does not flicker, it is just on or off This project
Use only the PWM pins marked with a tilde, here 3, 5, 6, 9, 10 and 11. - The flames are too jumpy This project
Raise the lowest brightness values inflicker, which gives calmer flames. - 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 LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - 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.