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

Let's make a flickering candle!

A little light dances and flickers all by itself, just like a real flame.

Pretend candles and fake fireplaces flicker exactly like this!

The story

The problem

A steady blink looks like a robot. A real candle wobbles and flickers in surprise ways.

Think of it like

It's like a candle flame in the wind — it never dances the exact same way twice.

Meet the parts

It thinks and tells the light when to flicker.

Arduino

The brain

Loading part…

Slows the power down so the light does not get hurt.

Resistor

The protector

Loading part…

A tiny light that glows like a candle.

LED

The flame

Loading part…

How it works

1

A quick glow

The light turns on and waits a surprise amount of time — never the same blink twice.

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

A quick dim

The light goes dim for another surprise wait, so the rhythm feels wobbly like a flame.

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

Sometimes a big flare

Once in a while the candle adds an extra bright glow — like the flame jumping! 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 — ready to build!

    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

  • Press the green Run button — the blinking should look wobbly, not perfect.
  • Watch for little bright flares, like a flame jumping up!

Peek at code

Get ready and shake the dice

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

setup() gets pin 13 ready, and randomSeed shakes up the surprise numbers so each run flickers differently.

The flicker

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

Each wait is a surprise number, so the light flickers fast and slow — that is what makes it look alive.

The bonus flare

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

Once in a while this part adds a longer, brighter glow — like the candle flame leaping up.

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. Which part runs again and again?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Yes! loop() runs again and again, forever.

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

  • A. So each wait is a different surprise length
  • B. To turn the light off forever
  • C. To read a button
Why: Yes! Surprise waits make the flicker look like a real flame.

Code lab — try on your own

  1. Make the flicker snappier — change random(20, 80) to random(10, 50) on line 8.

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

  2. Add a little note (comment) on the line that shakes up the surprise numbers.

    Hint: Type // and your words at the end of line 4.

Code walkthrough

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

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

A light flickers with surprise timing so it looks like a real candle.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

const int LED_PIN = 13;

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It gets pin 13 ready for the light and shakes up the surprise numbers.

Why here

Things we do only once go inside setup().

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

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where the light flickers fast and slow, never the same way twice.

Why here

Things that repeat go inside loop().

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 a number inside loop(), then press Run to make the flicker faster or slower.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It makes pin 13 ready to push power to the light.

Why here

It goes in setup() because we only set it once.

  pinMode(LED_PIN, OUTPUT);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

ON lights up the candle, OFF makes it dim.

Why here

It goes in loop() so the light can keep flickering.

  digitalWrite(LED_PIN, HIGH);

analogRead

Big idea

analogRead reads a number from a pin — anything from 0 to 1023.

In this project

It reads a tiny bit of noise from pin 0 to help pick fresh surprise numbers.

Why here

In setup(), to shake up the randomness before the candle starts.

  randomSeed(analogRead(0));

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It waits a surprise amount of time so the flicker looks alive.

Why here

Right after we turn the light on or off.

  delay(random(20, 80));

random

Big idea

random picks a surprise number inside a range you choose.

In this project

It picks a different wait time each flicker, so no two blinks match.

Why here

In loop(), where we want a new surprise number every time.

  delay(random(20, 80));