Projects / Flameless candle

Flameless candle

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. NANO18.5 mA

    Wire the LEDs

    D5 and D6 through 220 ohm, cathodes to GND.

  2. NANO

    Wire the button

    D2 to GND.

  3. NANO18.5 mA

    Upload the sketch

    The LEDs start flickering.

  4. NANO18.5 mA

    Add a diffuser

    A frosted cup or tissue over the LEDs.

  5. NANO

    Press the button

    The candle goes out and comes back.

  6. NANO

    Change the flicker range

    Edit the target ranges.

  7. NANO

    Change the speed

    Edit the 40 to 180 millisecond range.

  8. NANO18.5 mA

    Add a third LED

    Use another PWM pin.

In short

How do you build a flameless candle?

Two warm LEDs flicker like a candle using smooth random brightness changes, and a button turns the candle on or off. You need Arduino Nano, LED 5 mm yellow × 2, Resistor 220 Ω × 2, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Yellow LED 1 anode (long leg)220 ohm to Nano D5PWM output 1
Yellow LED 2 anode (long leg)220 ohm to Nano D6PWM output 2
Both LED cathodes (short legs)Nano GNDCommon ground
Button leg 1Nano D2On and off input
Button leg 2Nano GNDPressed pulls D2 low

Code

A candle flame never holds still, but it does not jump randomly either: it drifts. The sketch copies that by giving each LED a target brightness picked at random and then moving smoothly towards it, a bit more on each step, instead of jumping straight to it. A new target is chosen every 40 to 180 milliseconds, so the light wanders in an irregular rhythm. About one pick in twelve is a deep dip, which looks like a draught on the flame. Two LEDs get slightly different random targets, so they never flicker together, and that makes the glow feel more natural. The LEDs are driven with PWM on pins D5 and D6, where the pin switches on and off quickly and the brightness depends on how long it is on. Yellow or warm white LEDs look best, and a frosted cup, a piece of tissue or a white plastic tube spreads the light into a soft glow. A button turns the candle on and off, and the sketch updates about fifty times a second so the effect looks smooth. This is a safe flameless alternative to a real candle, so there is no fire risk.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const int LED_A = 5, LED_B = 6, BTN = 2;     // D5 and D6 are PWM pins

struct Flame {
  float level;                                // current brightness 0 to 255
  float target;                               // where it is heading
  unsigned long nextChange;                   // when to pick a new target
};
Flame a = {120, 120, 0}, b = {120, 120, 0};

bool on = true;
bool wasPressed = false;
unsigned long lastPress = 0, lastStep = 0;

void pickTarget(Flame &f) {
  if (random(12) == 0) f.target = random(25, 80);       // an occasional deep flicker
  else                 f.target = random(110, 256);     // normal flicker
  f.nextChange = millis() + random(40, 180);            // how long to head there
}

void update(Flame &f) {
  if (millis() >= f.nextChange) pickTarget(f);
  f.level += (f.target - f.level) * 0.25;               // smooth moves, no harsh jumps
}

void setup() {
  pinMode(BTN, INPUT_PULLUP);
  randomSeed(analogRead(A0));                           // unconnected pin gives some noise
}

void loop() {
  bool pressed = (digitalRead(BTN) == LOW);
  if (pressed && !wasPressed && millis() - lastPress > 40) {
    lastPress = millis();
    on = !on;
  }
  wasPressed = pressed;

  if (millis() - lastStep >= 20) {                      // update about 50 times a second
    lastStep = millis();
    if (on) {
      update(a); update(b);
      analogWrite(LED_A, (int)a.level);
      analogWrite(LED_B, (int)b.level);
    } else {
      analogWrite(LED_A, 0);
      analogWrite(LED_B, 0);
    }
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Power onBoth LEDs flicker smoothly
Watch for a minuteOccasional deeper dips
Press the buttonBoth LEDs off
Press againFlicker restarts
Brightness at the peakClose to full, about 13 mA per LED

If it does not work

No flicker, steady light

Check the LEDs are on D5 and D6, which are PWM pins.

Flicker looks jumpy

Lower the 0.25 smoothing value a little.

Too dim

Raise the lower target range.

Both LEDs flicker the same

Check both use their own random targets.

Button does nothing

Check D2 to GND.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the flameless candle?

Arduino Nano, LED 5 mm yellow × 2, Resistor 220 Ω × 2, Tactile push button 6 mm, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.

How long does the flameless candle take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the flameless candle works?

Power on: Both LEDs flicker smoothly. Watch for a minute: Occasional deeper dips. Press the button: Both LEDs off.

No flicker, steady light: what should I check?

Check the LEDs are on D5 and D6, which are PWM pins.

Flicker looks jumpy: what should I check?

Lower the 0.25 smoothing value a little.

Too dim: what should I check?

Raise the lower target range.

Both LEDs flicker the same: what should I check?

Check both use their own random targets.