Mission 09 · Stage 1

Electronic Candle

flicker timing with delay()

Flicker an LED with random delay() timing like a candle flame.

Electronic Candle circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 13

Resistor

pin 2

Resistor

pin 1

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

Random flicker like a real flame!

random() picks different delay() times so the glow feels alive.

Fake candles and fire effects use random timing.

The story

The problem

A steady blink feels robotic — you want organic flicker.

Think of it like

Like a candle flame — never blinks on the exact same beat.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Limits current to the LED

Resistor

Safety

Loading part…

Shows output from the code

LED

Light

Loading part…

How it works

1

Quick glow

random(20, 80) picks a different short ON time every pass — not a fixed blink.

digitalWrite(LED_PIN, HIGH);
delay(random(20, 80));
2

Quick dim

OFF time is random too, so the rhythm feels uneven like a flame.

digitalWrite(LED_PIN, LOW);
delay(random(10, 40));
3

Sometimes brighter

About 30% of the time the candle adds an extra bright pulse, then loop() flickers again.

if (random(0, 10) > 7) { ... longer glow ... }

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed!

    Loading part…
  2. 2

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  3. 3

    Place LED

    Place the LED (led1) on the breadboard.

    Loading part…
  4. 4

    Connect Arduino pin 13 to Resistor (r1) 2

    Connect Arduino pin 13 to Resistor (r1) 2.

  5. 5

    Connect Resistor (r1) 1 to LED (led1) anode (+)

    Connect Resistor (r1) 1 to LED (led1) anode (+).

  6. 6

    Connect LED (led1) cathode (-) to Arduino GND

    Connect LED (led1) cathode (-) to Arduino GND.

Try it

  • Run — the blink should NOT look perfectly regular.
  • Watch for occasional longer bright moments.

Peek at code

Random seed

void setup() {
  pinMode(LED_PIN, OUTPUT);
  randomSeed(analogRead(0));
}

randomSeed() in setup() makes random() give different flicker each run. pinMode prepares the LED output.

Random flicker

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(random(20, 80));
  digitalWrite(LED_PIN, LOW);
  delay(random(10, 40));

Each delay(random(...)) picks new wait times — that is what makes the candle feel alive.

Bonus bright pulse

  if (random(0, 10) > 7) {
    digitalWrite(LED_PIN, HIGH);
    delay(random(100, 300));
  }

The if block sometimes adds a longer glow — like a flame jumping.

Show full sketch (electronic-candle.ino)
const int LED_PIN = 13;
void setup() {
  pinMode(LED_PIN, OUTPUT);
  randomSeed(analogRead(0));
}
void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(random(20, 80));
  digitalWrite(LED_PIN, LOW);
  delay(random(10, 40));
  if (random(0, 10) > 7) {
    digitalWrite(LED_PIN, HIGH);
    delay(random(100, 300));
  }
}

Quick quiz

Q1. Where does repeating work belong?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Correct—loop() runs again and again.

Q2. Why use random() inside delay() here?

  • A. Each pause is a different length
  • B. To turn the LED off forever
  • C. To read a button
Why: Correct — random delays make the flicker feel natural.

Code lab — try on your own

  1. Make flicker snappier — lower the random ON range to random(10, 50) on line 8.

    Hint: Edit both numbers inside random(20, 80).

  2. Add a comment on the randomSeed line explaining why we need it.

    Hint: Line 4 in setup().

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robo Gurukul Studio.

Program overview

Technical

Sketches have globals, then setup() once, then loop() forever.

In this project

Flicker an LED with random delay() timing like a candle flame.

Why here

Read from top to bottom. Hover words or lines for help!

const int LED_PIN = 13;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Electronic Candle.

Why here

One-time setup belongs here—not in loop().

void setup() {
  pinMode(LED_PIN, OUTPUT);
  randomSeed(analogRead(0));
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Electronic Candle.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(random(20, 80));
  digitalWrite(LED_PIN, LOW);
  delay(random(10, 40));
  if (random(0, 10) > 7) {
    digitalWrite(LED_PIN, HIGH);
    delay(random(100, 300));
  }
}

Try this: Change numbers in loop(), then compile and run the simulator.

pinMode

Technical

Tells a pin if it listens or drives something.

In this project

Gets the Electronic Candle circuit ready in the simulator.

Why here

Goes in setup() because we only set pins once at the start.

  pinMode(LED_PIN, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Electronic Candle.

Why here

Goes in loop() so it can keep changing while the program runs.

  digitalWrite(LED_PIN, HIGH);

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Electronic Candle.

Why here

Goes in loop() to keep checking the sensor.

  randomSeed(analogRead(0));

delay

Technical

Waits for some time. Nothing else runs during the wait.

In this project

Controls speed so you can see Electronic Candle in the simulator.

Why here

Right after an action that should stay the same for a moment.

  delay(random(20, 80));

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Electronic Candle.

Why here

In loop() when you want different values each time.

  delay(random(20, 80));