Print or save as PDF

Use your browser's Print dialog (Cmd/Ctrl + P) and choose "Save as PDF".

← Back to book

Welcome to Robo Gurukul!

Your Arduino Adventure — Stages 1 to 3

Introduction

What is this book?

This book helps you learn Arduino step by step through 30 fun projects. Each chapter matches a mission in Robo Gurukul Studio — the same lessons you see in the desktop app, written so you can read, build, and understand even when you are away from the computer.

Safety first

  • Always use a resistor with LEDs — it protects the LED and the Arduino pin.
  • Connect and disconnect wires only when the Arduino is unplugged from USB power.
  • Ask a teacher or adult if you are unsure about any connection.
  • Never connect motors or high-power devices directly to Arduino pins without the right driver circuit.
  • Keep food and drinks away from your breadboard and components.

Stage 1: Output Control

setup(), loop(), digitalWrite(), delay()

Stage 1 is where every Arduino journey begins. You will learn how a program is organized — setup() runs once, loop() runs forever — and how to turn outputs on and off with digitalWrite() and delay().

Mission 01 · Stage 1

Blink LED

setup() and loop() program structure

Blink an external LED on a half breadboard through a current-limiting resistor connected to pin 13.

Blink LED circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 13

Breadboard

13t e

Resistor

pin 1

LED

anode (+)

LED

cathode (-)

Breadboard

tn 16

Breadboard

tn 3

Arduino

GND

See it

Every program has setup() and loop()!

Your first sketch: turn an LED on and off forever.

Blinking lights show power or status on almost every gadget.

The story

The problem

You need to understand how Arduino code is organized before anything else.

Think of it like

setup() is like getting dressed once; loop() is your daily routine on repeat.

Meet the parts

Connects components in rows like a real breadboard

Breadboard

Workspace

Loading part…

Decides when the LED is on or off

Arduino

Brain

Loading part…

Shows output from the code

LED

Light

Loading part…

Limits current to the LED

Resistor

Safety

Loading part…

How it works

1

setup() runs once

When power turns on, setup() configures pin 13 — then never runs again.

void setup() {
  pinMode(LED_PIN, OUTPUT);
}
2

Turn LED on

Pin 13 goes HIGH and current flows through the LED.

digitalWrite(LED_PIN, HIGH);
3

Wait a moment

The light stays on for half a second.

delay(500);
4

Turn off and repeat

LED off, another pause, then loop() jumps back to step 2 forever.

digitalWrite(LED_PIN, LOW);
delay(500);

Then loop back to step 2

Build the circuit

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

  1. 1

    Place Breadboard

    Place the Breadboard (bb1) on the breadboard.

    Breadboard placed — build like the real world!

    Loading part…
  2. 2

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed — ready to build!

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Resistor added across breadboard rows 13 and 14!

    Loading part…
  4. 4

    Place LED

    Place the LED (led1) on the breadboard.

    LED ready on the breadboard!

    Loading part…
  5. 5

    Connect Arduino pin 13 to Breadboard (bb1) 13t.e

    Arduino pin 13 to breadboard row 13 e

    Tip: Arduino pin 13 to breadboard row 13 e

    Signal on the breadboard!

  6. 6

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

    Resistor to LED anode on the breadboard

    Tip: Resistor to LED anode on the breadboard

    LED linked to resistor!

  7. 7

    Connect LED (led1) cathode (-) to Breadboard (bb1) tn.16

    LED cathode to breadboard ground rail at column 16

    Tip: LED cathode to breadboard ground rail at column 16

    Ground rail linked!

  8. 8

    Connect Breadboard (bb1) tn.3 to Arduino GND

    Breadboard ground rail to Arduino GND

    Tip: Breadboard ground rail to Arduino GND

    Circuit complete!

Try it

  • Press Run — the LED should blink in the simulator.
  • Remember: setup() once, loop() forever!

Peek at code

Pin name constant

const int LED_PIN = 13;

LED_PIN is a friendly name for pin 13 — use it everywhere instead of repeating the number.

setup() — runs once

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

setup() runs one time when the board starts. pinMode says pin 13 is an output.

loop() — blink forever

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

loop() turns the LED on, waits, off, waits — then Arduino runs loop() again automatically.

Show full sketch (blink-led.ino)
const int LED_PIN = 13;
void setup() {
  pinMode(LED_PIN, OUTPUT);
}
void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

Quick quiz

Q1. What does delay(500) do?

  • A. Waits half a second
  • B. Sets brightness to 500
  • C. Turns pin 500 on
Why: Correct—500 milliseconds is half a second between blinks.

Q2. Which function runs only once when the board starts?

  • A. setup()
  • B. loop()
  • C. delay()
Why: Correct — setup() runs one time at the start.

Code lab — try on your own

  1. Make the LED blink faster by lowering both delay() numbers.

    Hint: Try 200 or 300 instead of 500.

  2. Add a short comment on the line that turns the LED on (digitalWrite).

    Hint: Use // at the end or start of that line.

  3. Add a comment on line 1 explaining what LED_PIN means.

    Hint: Example: // external LED on pin 13

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

Blink an external LED on a half breadboard through a current-limiting resistor connected to pin 13.

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 Blink LED.

Why here

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

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Blink LED.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}

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 Blink LED 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 Blink LED.

Why here

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

  digitalWrite(LED_PIN, HIGH);

delay

Technical

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

In this project

Controls speed so you can see Blink LED in the simulator.

Why here

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

  delay(500);
Mission 02 · Stage 1

Fast & Slow Blink

changing delay() values

Alternate fast and slow blink speeds by changing delay() values.

Fast & Slow Blink circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 13

Resistor

pin 2

Resistor

pin 1

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

Same blink — two speeds!

Change delay() numbers to make the LED blink fast, then slow.

Different blink speeds mean different things on real devices.

The story

The problem

One blink speed is boring — you want to control timing.

Think of it like

Like clapping fast then slow — the action is the same, the pause changes.

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

Blink fast — ON

The LED turns on, then waits only 150 ms — a quick flash.

digitalWrite(LED_PIN, HIGH);
delay(FAST_MS);
2

Blink fast — OFF

Same short pause with the LED off. FAST_MS controls both quick blinks.

digitalWrite(LED_PIN, LOW);
delay(FAST_MS);
3

Blink slow — ON

Now delay(SLOW_MS) keeps the LED on much longer — easy to see the difference.

digitalWrite(LED_PIN, HIGH);
delay(SLOW_MS);
4

Blink slow — OFF

After the long pause, loop() starts over with fast blinks again.

digitalWrite(LED_PIN, LOW);
delay(SLOW_MS);

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

  • Press Run — watch two quick blinks, then two slow ones.
  • Count the rhythm: fast-fast, slow-slow, repeat!

Peek at code

Timing constants

const int LED_PIN = 13;
const int FAST_MS = 150;
const int SLOW_MS = 800;

FAST_MS and SLOW_MS are names for two different delay() lengths. Change these numbers to retune the whole pattern.

Setup the pin

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

setup() runs once and sets pin 13 as an output so we can drive the LED.

Fast then slow loop

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(FAST_MS);
  digitalWrite(LED_PIN, LOW);
  delay(FAST_MS);
  digitalWrite(LED_PIN, HIGH);
  delay(SLOW_MS);
  digitalWrite(LED_PIN, LOW);
  delay(SLOW_MS);
}

loop() alternates two fast blinks, then two slow blinks. The delay() numbers — not digitalWrite — set the speed.

Show full sketch (fast-slow-blink.ino)
const int LED_PIN = 13;
const int FAST_MS = 150;
const int SLOW_MS = 800;
void setup() {
  pinMode(LED_PIN, OUTPUT);
}
void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(FAST_MS);
  digitalWrite(LED_PIN, LOW);
  delay(FAST_MS);
  digitalWrite(LED_PIN, HIGH);
  delay(SLOW_MS);
  digitalWrite(LED_PIN, LOW);
  delay(SLOW_MS);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What does a bigger number inside delay() do?

  • A. Waits longer
  • B. Makes the LED brighter
  • C. Runs setup() again
Why: Yes — more milliseconds means a longer wait.

Code lab — try on your own

  1. Make the fast blinks even quicker by lowering FAST_MS (try 80 or 100).

    Hint: Edit the number on the line with FAST_MS = 150.

  2. Make the slow blinks longer by raising SLOW_MS (try 1200 or 1500).

    Hint: Edit the line with SLOW_MS = 800.

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

Alternate fast and slow blink speeds by changing delay() values.

Why here

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

const int LED_PIN = 13;
const int FAST_MS = 150;
const int SLOW_MS = 800;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Fast & Slow Blink.

Why here

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

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Fast & Slow Blink.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(FAST_MS);
  digitalWrite(LED_PIN, LOW);
  delay(FAST_MS);
  digitalWrite(LED_PIN, HIGH);
  delay(SLOW_MS);
  digitalWrite(LED_PIN, LOW);
  delay(SLOW_MS);
}

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 Fast & Slow Blink 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 Fast & Slow Blink.

Why here

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

  digitalWrite(LED_PIN, HIGH);

delay

Technical

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

In this project

Controls speed so you can see Fast & Slow Blink in the simulator.

Why here

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

  delay(FAST_MS);
Mission 03 · Stage 1

Traffic Light

sequencing multiple outputs with timing

Cycle red, yellow, and green LEDs like a mini traffic signal.

Traffic Light circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 13

Red resistor

pin 2

Red resistor

pin 1

Red LED

anode (+)

Red LED

cathode (-)

Arduino

GND

Arduino

pin 12

Yellow resistor

pin 2

Yellow resistor

pin 1

Yellow LED

anode (+)

Yellow LED

cathode (-)

Arduino

GND

Arduino

pin 11

Green resistor

pin 2

Green resistor

pin 1

Green LED

anode (+)

Green LED

cathode (-)

Arduino

GND

See it

Three lights, one sequence!

Red, yellow, green — only one color on at a time, in order.

Real traffic lights use the same timed sequence idea.

The story

The problem

One LED is not enough — you need multiple outputs in a fixed order.

Think of it like

Like a conductor cueing musicians one at a time.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Red light in the sequence

Red LED

Stop

Loading part…

Yellow light in the sequence

Yellow LED

Caution

Loading part…

Green light in the sequence

Green LED

Go

Loading part…

Protects the red LED

Red resistor

Safety

Loading part…

Protects the yellow LED

Yellow resistor

Safety

Loading part…

Protects the green LED

Green resistor

Safety

Loading part…

How it works

1

Red light

Turn every light off first, then only red stays on for 2 seconds.

allOff();
digitalWrite(PIN_RED, HIGH);
delay(2000);
2

Yellow light

Red goes off; yellow warns for a shorter 800 ms.

allOff();
digitalWrite(PIN_YELLOW, HIGH);
delay(800);
3

Green light

Yellow off, green on — then loop() repeats from red again.

allOff();
digitalWrite(PIN_GREEN, HIGH);
delay(2000);

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 Red LED

    Place the Red LED (ledRed) on the breadboard.

    Loading part…
  3. 3

    Place Yellow LED

    Place the Yellow LED (ledYellow) on the breadboard.

    Loading part…
  4. 4

    Place Green LED

    Place the Green LED (ledGreen) on the breadboard.

    Loading part…
  5. 5

    Place Red resistor

    Place the Red resistor (rRed) on the breadboard.

    Loading part…
  6. 6

    Place Yellow resistor

    Place the Yellow resistor (rYellow) on the breadboard.

    Loading part…
  7. 7

    Place Green resistor

    Place the Green resistor (rGreen) on the breadboard.

    Loading part…
  8. 8

    Connect Arduino pin 13 to Red resistor (rRed) 2

    Connect Arduino pin 13 to Red resistor (rRed) 2.

  9. 9

    Connect Red resistor (rRed) 1 to Red LED (ledRed) anode (+)

    Connect Red resistor (rRed) 1 to Red LED (ledRed) anode (+).

  10. 10

    Connect Red LED (ledRed) cathode (-) to Arduino GND

    Connect Red LED (ledRed) cathode (-) to Arduino GND.

  11. 11

    Connect Arduino pin 12 to Yellow resistor (rYellow) 2

    Connect Arduino pin 12 to Yellow resistor (rYellow) 2.

  12. 12

    Connect Yellow resistor (rYellow) 1 to Yellow LED (ledYellow) anode (+)

    Connect Yellow resistor (rYellow) 1 to Yellow LED (ledYellow) anode (+).

  13. 13

    Connect Yellow LED (ledYellow) cathode (-) to Arduino GND

    Connect Yellow LED (ledYellow) cathode (-) to Arduino GND.

  14. 14

    Connect Arduino pin 11 to Green resistor (rGreen) 2

    Connect Arduino pin 11 to Green resistor (rGreen) 2.

  15. 15

    Connect Green resistor (rGreen) 1 to Green LED (ledGreen) anode (+)

    Connect Green resistor (rGreen) 1 to Green LED (ledGreen) anode (+).

  16. 16

    Connect Green LED (ledGreen) cathode (-) to Arduino GND

    Connect Green LED (ledGreen) cathode (-) to Arduino GND.

Try it

  • Run the sim — only one LED should glow at a time.
  • Watch the order: red, yellow, green, then repeat.

Peek at code

Three output pins

const int PIN_RED = 13;
const int PIN_YELLOW = 12;
const int PIN_GREEN = 11;

Each color has its own pin number at the top. Names like PIN_RED make the code easier to read.

allOff() helper

void allOff() {
  digitalWrite(PIN_RED, LOW);
  digitalWrite(PIN_YELLOW, LOW);
  digitalWrite(PIN_GREEN, LOW);
}

Before each new color, every light is turned LOW so only one shows at a time.

Traffic sequence

void loop() {
  allOff();
  digitalWrite(PIN_RED, HIGH);
  delay(2000);
  allOff();
  digitalWrite(PIN_YELLOW, HIGH);
  delay(800);
  allOff();
  digitalWrite(PIN_GREEN, HIGH);
  delay(2000);
}

loop() runs red → yellow → green in order. delay() sets how long each color stays on.

Show full sketch (traffic-light.ino)
const int PIN_RED = 13;
const int PIN_YELLOW = 12;
const int PIN_GREEN = 11;
void allOff() {
  digitalWrite(PIN_RED, LOW);
  digitalWrite(PIN_YELLOW, LOW);
  digitalWrite(PIN_GREEN, LOW);
}
void setup() {
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_YELLOW, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  allOff();
}
void loop() {
  allOff();
  digitalWrite(PIN_RED, HIGH);
  delay(2000);
  allOff();
  digitalWrite(PIN_YELLOW, HIGH);
  delay(800);
  allOff();
  digitalWrite(PIN_GREEN, HIGH);
  delay(2000);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. Why turn the other lights OFF before turning one ON?

  • A. Only one color should show at a time
  • B. To save pin numbers
  • C. Because delay() requires it
Why: Correct — sequencing means one active output at each step.

Code lab — try on your own

  1. Make red stay on longer — change its delay(2000) to delay(3000).

    Hint: Find the delay right after digitalWrite(PIN_RED, HIGH).

  2. Make yellow faster — change delay(800) to delay(400).

    Hint: The yellow delay is on the line after PIN_YELLOW turns on.

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

Cycle red, yellow, and green LEDs like a mini traffic signal.

Why here

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

const int PIN_RED = 13;
const int PIN_YELLOW = 12;
const int PIN_GREEN = 11;
void allOff() {
  digitalWrite(PIN_RED, LOW);
  digitalWrite(PIN_YELLOW, LOW);
  digitalWrite(PIN_GREEN, LOW);
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Traffic Light.

Why here

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

void setup() {
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_YELLOW, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  allOff();
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Traffic Light.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  allOff();
  digitalWrite(PIN_RED, HIGH);
  delay(2000);
  allOff();
  digitalWrite(PIN_YELLOW, HIGH);
  delay(800);
  allOff();
  digitalWrite(PIN_GREEN, HIGH);
  delay(2000);
}

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 Traffic Light circuit ready in the simulator.

Why here

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

  pinMode(PIN_RED, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Traffic Light.

Why here

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

  digitalWrite(PIN_RED, LOW);

delay

Technical

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

In this project

Controls speed so you can see Traffic Light in the simulator.

Why here

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

  delay(2000);
Mission 04 · Stage 1

LED Chaser

loop index drives a moving pattern

Light one segment at a time on a bar graph using a loop index.

LED Chaser circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

LED bar graph

A1

Arduino

pin 3

LED bar graph

A2

Arduino

pin 4

LED bar graph

A3

Arduino

pin 5

LED bar graph

A4

Arduino

pin 6

LED bar graph

A5

Arduino

pin 7

LED bar graph

A6

Arduino

pin 8

LED bar graph

A7

Arduino

pin 9

LED bar graph

A8

Arduino

pin 10

LED bar graph

A9

Arduino

pin 11

LED bar graph

A10

LED bar graph

C10

Arduino

GND

See it

One lit segment at a time!

A loop index picks which LED is ON — the dot runs down the bar.

Loading bars and volume meters chase like this.

The story

The problem

You want a moving pattern, not just one blinking LED.

Think of it like

Like spotlighting one person in a line, then the next.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Ten segments you can light up

LED bar graph

Display

Loading part…

How it works

1

Clear the bar

An inner loop sets every segment LOW before lighting the next one.

digitalWrite(FIRST_PIN + j, LOW);
2

Light segment i

The outer loop variable i picks which bar segment is ON — that is the chase!

digitalWrite(FIRST_PIN + i, HIGH);
3

Pause and next

A short delay lets you see the dot move; then i increases and the chase continues.

delay(100);

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 LED bar graph

    Place the LED bar graph (bar1) on the breadboard.

    Loading part…
  3. 3

    Connect Arduino pin 2 to LED bar graph (bar1) A1

    Connect Arduino pin 2 to LED bar graph (bar1) A1.

  4. 4

    Connect Arduino pin 3 to LED bar graph (bar1) A2

    Connect Arduino pin 3 to LED bar graph (bar1) A2.

  5. 5

    Connect Arduino pin 4 to LED bar graph (bar1) A3

    Connect Arduino pin 4 to LED bar graph (bar1) A3.

  6. 6

    Connect Arduino pin 5 to LED bar graph (bar1) A4

    Connect Arduino pin 5 to LED bar graph (bar1) A4.

  7. 7

    Connect Arduino pin 6 to LED bar graph (bar1) A5

    Connect Arduino pin 6 to LED bar graph (bar1) A5.

  8. 8

    Connect Arduino pin 7 to LED bar graph (bar1) A6

    Connect Arduino pin 7 to LED bar graph (bar1) A6.

  9. 9

    Connect Arduino pin 8 to LED bar graph (bar1) A7

    Connect Arduino pin 8 to LED bar graph (bar1) A7.

  10. 10

    Connect Arduino pin 9 to LED bar graph (bar1) A8

    Connect Arduino pin 9 to LED bar graph (bar1) A8.

  11. 11

    Connect Arduino pin 10 to LED bar graph (bar1) A9

    Connect Arduino pin 10 to LED bar graph (bar1) A9.

  12. 12

    Connect Arduino pin 11 to LED bar graph (bar1) A10

    Connect Arduino pin 11 to LED bar graph (bar1) A10.

  13. 13

    Connect LED bar graph (bar1) C10 to Arduino GND

    Connect LED bar graph (bar1) C10 to Arduino GND.

Try it

  • Run and follow the lit segment from left to right.
  • Only one bar LED should be on at a time.

Peek at code

Pin range

const int FIRST_PIN = 2;
const int NUM_LEDS = 10;

FIRST_PIN is the first bar segment; NUM_LEDS is how many segments the loop uses.

Setup all outputs

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

A for-loop calls pinMode on pins 2 through 11 so each segment can be driven.

Chase loop

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < NUM_LEDS; j++) {
      digitalWrite(FIRST_PIN + j, LOW);
    }
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(100);
  }
}

Outer loop picks i; inner loop clears all LEDs; then one segment turns on. That is how the dot runs along the bar.

Show full sketch (led-chaser.ino)
const int FIRST_PIN = 2;
const int NUM_LEDS = 10;
void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}
void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < NUM_LEDS; j++) {
      digitalWrite(FIRST_PIN + j, LOW);
    }
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(100);
  }
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What does the loop variable i control in this sketch?

  • A. Which LED is lit
  • B. How bright the bar is
  • C. The random seed
Why: Correct — i is the index that selects one output pin.

Code lab — try on your own

  1. Make the chase faster — change delay(100) to delay(50).

    Hint: There is only one delay() inside loop().

  2. Add a comment on the line that lights one segment (digitalWrite with + i).

    Hint: Use // at the end of that line.

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

Light one segment at a time on a bar graph using a loop index.

Why here

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

const int FIRST_PIN = 2;
const int NUM_LEDS = 10;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for LED Chaser.

Why here

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

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in LED Chaser.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < NUM_LEDS; j++) {
      digitalWrite(FIRST_PIN + j, LOW);
    }
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(100);
  }
}

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 LED Chaser circuit ready in the simulator.

Why here

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

    pinMode(FIRST_PIN + i, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in LED Chaser.

Why here

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

      digitalWrite(FIRST_PIN + j, LOW);

delay

Technical

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

In this project

Controls speed so you can see LED Chaser in the simulator.

Why here

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

    delay(100);
Mission 05 · Stage 1

Multiple LED Patterns

nested loops and pattern tables

Show different bar graph patterns stored in tables and loops.

Multiple LED Patterns circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

LED bar graph

A1

Arduino

pin 3

LED bar graph

A2

Arduino

pin 4

LED bar graph

A3

Arduino

pin 5

LED bar graph

A4

Arduino

pin 6

LED bar graph

A5

Arduino

pin 7

LED bar graph

A6

Arduino

pin 8

LED bar graph

A7

Arduino

pin 9

LED bar graph

A8

Arduino

pin 10

LED bar graph

A9

Arduino

pin 11

LED bar graph

A10

LED bar graph

C10

Arduino

GND

See it

Patterns stored in tables!

Arrays and loops replay different ON/OFF shapes on the bar.

Displays often look up patterns from tables instead of typing each step.

The story

The problem

Writing every LED by hand gets tedious — reuse pattern data.

Think of it like

Like reading sheet music: the notes are written once, played in a loop.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Ten segments you can light up

LED bar graph

Display

Loading part…

How it works

1

Show stripes

patternA[] is a table of 1s and 0s — the loop in showPattern() writes each segment ON or OFF.

showPattern(patternA);
delay(400);
2

Show pairs

patternB[] is a different shape on the bar — same function, different data.

showPattern(patternB);
delay(400);
3

Show center block

Then loop() starts again with patternA. Tables + loops avoid rewriting every LED by hand.

showPattern(patternC);
delay(400);

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 LED bar graph

    Place the LED bar graph (bar1) on the breadboard.

    Loading part…
  3. 3

    Connect Arduino pin 2 to LED bar graph (bar1) A1

    Connect Arduino pin 2 to LED bar graph (bar1) A1.

  4. 4

    Connect Arduino pin 3 to LED bar graph (bar1) A2

    Connect Arduino pin 3 to LED bar graph (bar1) A2.

  5. 5

    Connect Arduino pin 4 to LED bar graph (bar1) A3

    Connect Arduino pin 4 to LED bar graph (bar1) A3.

  6. 6

    Connect Arduino pin 5 to LED bar graph (bar1) A4

    Connect Arduino pin 5 to LED bar graph (bar1) A4.

  7. 7

    Connect Arduino pin 6 to LED bar graph (bar1) A5

    Connect Arduino pin 6 to LED bar graph (bar1) A5.

  8. 8

    Connect Arduino pin 7 to LED bar graph (bar1) A6

    Connect Arduino pin 7 to LED bar graph (bar1) A6.

  9. 9

    Connect Arduino pin 8 to LED bar graph (bar1) A7

    Connect Arduino pin 8 to LED bar graph (bar1) A7.

  10. 10

    Connect Arduino pin 9 to LED bar graph (bar1) A8

    Connect Arduino pin 9 to LED bar graph (bar1) A8.

  11. 11

    Connect Arduino pin 10 to LED bar graph (bar1) A9

    Connect Arduino pin 10 to LED bar graph (bar1) A9.

  12. 12

    Connect Arduino pin 11 to LED bar graph (bar1) A10

    Connect Arduino pin 11 to LED bar graph (bar1) A10.

  13. 13

    Connect LED bar graph (bar1) C10 to Arduino GND

    Connect LED bar graph (bar1) C10 to Arduino GND.

Try it

  • Run and name each shape before it changes.
  • Patterns come from the arrays — not magic!

Peek at code

Pattern tables

const byte patternA[] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
const byte patternB[] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
const byte patternC[] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0};

Each array stores which segments are on (1) or off (0). Adding a new look means a new table, not ten new lines.

showPattern()

void showPattern(const byte *pat) {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, pat[i] ? HIGH : LOW);
  }
}

One for-loop walks the table and calls digitalWrite for each segment. Reuse this function for every pattern.

Play patterns in loop

void loop() {
  showPattern(patternA);
  delay(400);
  showPattern(patternB);
  delay(400);
  showPattern(patternC);
  delay(400);
}

loop() calls showPattern three times with pauses — that is the full repeating show.

Show full sketch (led-patterns.ino)
const int FIRST_PIN = 2;
const int NUM_LEDS = 10;
const byte patternA[] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
const byte patternB[] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
const byte patternC[] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0};
void showPattern(const byte *pat) {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, pat[i] ? HIGH : LOW);
  }
}
void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}
void loop() {
  showPattern(patternA);
  delay(400);
  showPattern(patternB);
  delay(400);
  showPattern(patternC);
  delay(400);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. Why store patterns in arrays like patternA[]?

  • A. Reuse the same shape with one loop
  • B. Make the Arduino faster
  • C. Replace pinMode()
Why: Correct — a table plus a loop avoids copy-pasting many lines.

Code lab — try on your own

  1. Speed up the show — change all delay(400) to delay(200).

    Hint: There are three delay(400) lines in loop().

  2. Flip one segment in patternA — change a 1 to 0 or a 0 to 1 on line 3.

    Hint: Edit the numbers inside the curly braces of patternA.

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

Show different bar graph patterns stored in tables and loops.

Why here

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

const int FIRST_PIN = 2;
const int NUM_LEDS = 10;
const byte patternA[] = {1, 0, 1, 0, 1, 0, 1, 0, 1, 0};
const byte patternB[] = {1, 1, 0, 0, 1, 1, 0, 0, 1, 1};
const byte patternC[] = {0, 0, 0, 0, 1, 1, 1, 1, 0, 0};
void showPattern(const byte *pat) {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, pat[i] ? HIGH : LOW);
  }
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Multiple LED Patterns.

Why here

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

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Multiple LED Patterns.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  showPattern(patternA);
  delay(400);
  showPattern(patternB);
  delay(400);
  showPattern(patternC);
  delay(400);
}

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 Multiple LED Patterns circuit ready in the simulator.

Why here

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

    pinMode(FIRST_PIN + i, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Multiple LED Patterns.

Why here

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

    digitalWrite(FIRST_PIN + i, pat[i] ? HIGH : LOW);

delay

Technical

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

In this project

Controls speed so you can see Multiple LED Patterns in the simulator.

Why here

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

  delay(400);
Mission 06 · Stage 1

Alternate LEDs

alternating output states

Turn two LEDs on and off in opposite states.

Alternate LEDs circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 12

Red resistor

pin 2

Red resistor

pin 1

Red LED

anode (+)

Red LED

cathode (-)

Arduino

GND

Arduino

pin 13

Green resistor

pin 2

Green resistor

pin 1

Green LED

anode (+)

Green LED

cathode (-)

Arduino

GND

See it

When one is ON, the other is OFF!

Two LEDs take opposite states — like a seesaw.

Direction indicators and turn signals alternate outputs.

The story

The problem

You need two outputs that never match at the same time.

Think of it like

Like a seesaw — one side up means the other is down.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

First alternating light

Red LED

Output A

Loading part…

Second alternating light

Green LED

Output B

Loading part…

Protects the red LED

Red resistor

Safety

Loading part…

Protects the green LED

Green resistor

Safety

Loading part…

How it works

1

LED A wins

Pin 12 is HIGH while pin 13 is LOW — opposite states on purpose.

digitalWrite(LED_A, HIGH);
digitalWrite(LED_B, LOW);
2

Hold

Both states stay visible for 300 ms so you can see who is on.

delay(300);
3

LED B wins

Now the roles flip — A off, B on. Like a seesaw.

digitalWrite(LED_A, LOW);
digitalWrite(LED_B, HIGH);
4

Hold and repeat

Another pause, then loop() swaps back to A again.

delay(300);

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 Red LED

    Place the Red LED (ledA) on the breadboard.

    Loading part…
  3. 3

    Place Green LED

    Place the Green LED (ledB) on the breadboard.

    Loading part…
  4. 4

    Place Red resistor

    Place the Red resistor (rA) on the breadboard.

    Loading part…
  5. 5

    Place Green resistor

    Place the Green resistor (rB) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 12 to Red resistor (rA) 2

    Connect Arduino pin 12 to Red resistor (rA) 2.

  7. 7

    Connect Red resistor (rA) 1 to Red LED (ledA) anode (+)

    Connect Red resistor (rA) 1 to Red LED (ledA) anode (+).

  8. 8

    Connect Red LED (ledA) cathode (-) to Arduino GND

    Connect Red LED (ledA) cathode (-) to Arduino GND.

  9. 9

    Connect Arduino pin 13 to Green resistor (rB) 2

    Connect Arduino pin 13 to Green resistor (rB) 2.

  10. 10

    Connect Green resistor (rB) 1 to Green LED (ledB) anode (+)

    Connect Green resistor (rB) 1 to Green LED (ledB) anode (+).

  11. 11

    Connect Green LED (ledB) cathode (-) to Arduino GND

    Connect Green LED (ledB) cathode (-) to Arduino GND.

Try it

  • Run — red and green should never both be bright together.
  • Watch them take turns like a blinker.

Peek at code

Two output pins

const int LED_A = 12;
const int LED_B = 13;

LED_A and LED_B name the two pins we alternate between.

Setup both pins

void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
}

Both pins are outputs so we can drive two LEDs independently.

Alternate loop

void loop() {
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, LOW);
  delay(300);
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, HIGH);
  delay(300);
}

Each half of loop() sets opposite HIGH/LOW pairs, with delay() between swaps.

Show full sketch (alternate-leds.ino)
const int LED_A = 12;
const int LED_B = 13;
void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
}
void loop() {
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, LOW);
  delay(300);
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, HIGH);
  delay(300);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. When LED A is HIGH, LED B should be…

  • A. LOW (opposite)
  • B. HIGH (same)
  • C. Unconnected
Why: Correct — alternating means opposite ON/OFF states.

Code lab — try on your own

  1. Swap faster — change both delay(300) to delay(150).

    Hint: There are two matching delay lines in loop().

  2. Add a comment on the first digitalWrite(LED_A, HIGH) line explaining “A on”.

    Hint: Use // at the end of line 8.

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

Turn two LEDs on and off in opposite states.

Why here

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

const int LED_A = 12;
const int LED_B = 13;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Alternate LEDs.

Why here

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

void setup() {
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Alternate LEDs.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  digitalWrite(LED_A, HIGH);
  digitalWrite(LED_B, LOW);
  delay(300);
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, HIGH);
  delay(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 Alternate LEDs circuit ready in the simulator.

Why here

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

  pinMode(LED_A, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Alternate LEDs.

Why here

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

  digitalWrite(LED_A, HIGH);

delay

Technical

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

In this project

Controls speed so you can see Alternate LEDs in the simulator.

Why here

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

  delay(300);
Mission 07 · Stage 1

RGB LED Basics

three digital outputs for RGB channels

Turn red, green, and blue channels on and off with digitalWrite().

RGB LED Basics circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 9

Red resistor

pin 1

Red resistor

pin 2

RGB LED

red (R)

Arduino

pin 10

Green resistor

pin 1

Green resistor

pin 2

RGB LED

green (G)

Arduino

pin 11

Blue resistor

pin 1

Blue resistor

pin 2

RGB LED

blue (B)

RGB LED

common (COM)

Arduino

GND

See it

Three pins, three colors!

Turn red, green, and blue channels on one at a time with digitalWrite().

RGB LEDs mix colors in screens and mood lights.

The story

The problem

One color channel is not enough — an RGB LED has three inputs.

Think of it like

Like three light switches — one for red, green, and blue.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Three color channels in one bulb

RGB LED

Color light

Loading part…

Protects the red channel

Red resistor

Safety

Loading part…

Protects the green channel

Green resistor

Safety

Loading part…

Protects the blue channel

Blue resistor

Safety

Loading part…

How it works

1

Red channel

Only the red pin is HIGH; green and blue stay off — pure red light.

allOff();
digitalWrite(PIN_R, HIGH);
delay(500);
2

Green channel

Same idea: one channel on at a time with digitalWrite, not brightness yet.

allOff();
digitalWrite(PIN_G, HIGH);
delay(500);
3

Blue channel

Blue turn, then loop() starts the red cycle again.

allOff();
digitalWrite(PIN_B, HIGH);
delay(500);

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 RGB LED

    Place the RGB LED (rgb1) on the breadboard.

    Loading part…
  3. 3

    Place Red resistor

    Place the Red resistor (rr) on the breadboard.

    Loading part…
  4. 4

    Place Green resistor

    Place the Green resistor (rg) on the breadboard.

    Loading part…
  5. 5

    Place Blue resistor

    Place the Blue resistor (rb) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 9 to Red resistor (rr) 1

    Connect Arduino pin 9 to Red resistor (rr) 1.

  7. 7

    Connect Red resistor (rr) 2 to RGB LED (rgb1) R

    Connect Red resistor (rr) 2 to RGB LED (rgb1) R.

  8. 8

    Connect Arduino pin 10 to Green resistor (rg) 1

    Connect Arduino pin 10 to Green resistor (rg) 1.

  9. 9

    Connect Green resistor (rg) 2 to RGB LED (rgb1) G

    Connect Green resistor (rg) 2 to RGB LED (rgb1) G.

  10. 10

    Connect Arduino pin 11 to Blue resistor (rb) 1

    Connect Arduino pin 11 to Blue resistor (rb) 1.

  11. 11

    Connect Blue resistor (rb) 2 to RGB LED (rgb1) B

    Connect Blue resistor (rb) 2 to RGB LED (rgb1) B.

  12. 12

    Connect RGB LED (rgb1) COM to Arduino GND

    Connect RGB LED (rgb1) COM to Arduino GND.

Try it

  • Run — colors should change one at a time: red, green, blue.
  • Only one channel is on at each step.

Peek at code

Three channel pins

const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;

An RGB LED needs three separate digital outputs — one per color.

allOff() helper

void allOff() {
  digitalWrite(PIN_R, LOW);
  digitalWrite(PIN_G, LOW);
  digitalWrite(PIN_B, LOW);
}

Turns all three channels LOW so the next color starts clean.

Color cycle

void loop() {
  allOff();
  digitalWrite(PIN_R, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_G, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_B, HIGH);
  delay(500);
}

loop() shows red, then green, then blue — each with the same delay().

Show full sketch (rgb-led-basics.ino)
const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;
void allOff() {
  digitalWrite(PIN_R, LOW);
  digitalWrite(PIN_G, LOW);
  digitalWrite(PIN_B, LOW);
}
void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}
void loop() {
  allOff();
  digitalWrite(PIN_R, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_G, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_B, HIGH);
  delay(500);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. How many output pins drive the RGB LED here?

  • A. Three (R, G, B)
  • B. One shared pin
  • C. Zero — it is automatic
Why: Correct — three digitalWrite() pins control R, G, and B.

Code lab — try on your own

  1. Show each color quicker — change every delay(500) to delay(250).

    Hint: Three delay lines in loop(), one after each color.

  2. Add a comment on the red digitalWrite line.

    Hint: The line right after the first allOff() in loop().

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

Turn red, green, and blue channels on and off with digitalWrite().

Why here

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

const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;
void allOff() {
  digitalWrite(PIN_R, LOW);
  digitalWrite(PIN_G, LOW);
  digitalWrite(PIN_B, LOW);
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for RGB LED Basics.

Why here

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

void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in RGB LED Basics.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  allOff();
  digitalWrite(PIN_R, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_G, HIGH);
  delay(500);
  allOff();
  digitalWrite(PIN_B, HIGH);
  delay(500);
}

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 RGB LED Basics circuit ready in the simulator.

Why here

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

  pinMode(PIN_R, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in RGB LED Basics.

Why here

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

  digitalWrite(PIN_R, LOW);

delay

Technical

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

In this project

Controls speed so you can see RGB LED Basics in the simulator.

Why here

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

  delay(500);
Mission 08 · Stage 1

RGB Color Mixer

combining channel values for colors

Mix RGB channels to make yellow, cyan, magenta, and white.

RGB Color Mixer circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 9

Red resistor

pin 1

Red resistor

pin 2

RGB LED

red (R)

Arduino

pin 10

Green resistor

pin 1

Green resistor

pin 2

RGB LED

green (G)

Arduino

pin 11

Blue resistor

pin 1

Blue resistor

pin 2

RGB LED

blue (B)

RGB LED

common (COM)

Arduino

GND

See it

Mix channels to make new colors!

Turn two or three channels ON together — yellow, cyan, white, and more.

Screens mix red, green, and blue to paint every color.

The story

The problem

Single channels give basic colors — combinations make more.

Think of it like

Like mixing paint — red plus green makes yellow.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Three color channels in one bulb

RGB LED

Color light

Loading part…

Protects the red channel

Red resistor

Safety

Loading part…

Protects the green channel

Green resistor

Safety

Loading part…

Protects the blue channel

Blue resistor

Safety

Loading part…

How it works

1

Pure red

Only the red channel is on.

setChannels(HIGH, LOW, LOW);
2

Yellow mix

Red and green together look yellow — two channels ON at once.

setChannels(HIGH, HIGH, LOW);
3

Pure green

Just green this time.

setChannels(LOW, HIGH, LOW);
4

Cyan mix

Green plus blue makes cyan.

setChannels(LOW, HIGH, HIGH);
5

Pure blue

Blue alone.

setChannels(LOW, LOW, HIGH);
6

Magenta mix

Red plus blue makes magenta.

setChannels(HIGH, LOW, HIGH);
7

White — all on

All three channels together look white, then loop() repeats.

setChannels(HIGH, HIGH, HIGH);

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 RGB LED

    Place the RGB LED (rgb1) on the breadboard.

    Loading part…
  3. 3

    Place Red resistor

    Place the Red resistor (rr) on the breadboard.

    Loading part…
  4. 4

    Place Green resistor

    Place the Green resistor (rg) on the breadboard.

    Loading part…
  5. 5

    Place Blue resistor

    Place the Blue resistor (rb) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 9 to Red resistor (rr) 1

    Connect Arduino pin 9 to Red resistor (rr) 1.

  7. 7

    Connect Red resistor (rr) 2 to RGB LED (rgb1) R

    Connect Red resistor (rr) 2 to RGB LED (rgb1) R.

  8. 8

    Connect Arduino pin 10 to Green resistor (rg) 1

    Connect Arduino pin 10 to Green resistor (rg) 1.

  9. 9

    Connect Green resistor (rg) 2 to RGB LED (rgb1) G

    Connect Green resistor (rg) 2 to RGB LED (rgb1) G.

  10. 10

    Connect Arduino pin 11 to Blue resistor (rb) 1

    Connect Arduino pin 11 to Blue resistor (rb) 1.

  11. 11

    Connect Blue resistor (rb) 2 to RGB LED (rgb1) B

    Connect Blue resistor (rb) 2 to RGB LED (rgb1) B.

  12. 12

    Connect RGB LED (rgb1) COM to Arduino GND

    Connect RGB LED (rgb1) COM to Arduino GND.

Try it

  • Run and call out each color name as it changes.
  • Notice when two channels are on — that is a mix!

Peek at code

setChannels()

void setChannels(int r, int g, int b) {
  digitalWrite(PIN_R, r);
  digitalWrite(PIN_G, g);
  digitalWrite(PIN_B, b);
}

One function sets all three pins at once — easier than three separate digitalWrite lines each time.

Setup RGB pins

void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}

All three color pins are configured as outputs in setup().

Color mixing loop

void loop() {
  setChannels(HIGH, LOW, LOW);
  delay(600);
  setChannels(HIGH, HIGH, LOW);
  delay(600);
  setChannels(LOW, HIGH, LOW);
  delay(600);
  setChannels(LOW, HIGH, HIGH);
  delay(600);
  setChannels(LOW, LOW, HIGH);
  delay(600);
  setChannels(HIGH, LOW, HIGH);
  delay(600);
  setChannels(HIGH, HIGH, HIGH);
  delay(600);
}

Each setChannels call is a different recipe. Combining channels is how new colors appear.

Show full sketch (rgb-color-mixer.ino)
const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;
void setChannels(int r, int g, int b) {
  digitalWrite(PIN_R, r);
  digitalWrite(PIN_G, g);
  digitalWrite(PIN_B, b);
}
void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}
void loop() {
  setChannels(HIGH, LOW, LOW);
  delay(600);
  setChannels(HIGH, HIGH, LOW);
  delay(600);
  setChannels(LOW, HIGH, LOW);
  delay(600);
  setChannels(LOW, HIGH, HIGH);
  delay(600);
  setChannels(LOW, LOW, HIGH);
  delay(600);
  setChannels(HIGH, LOW, HIGH);
  delay(600);
  setChannels(HIGH, HIGH, HIGH);
  delay(600);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. Which channels are ON to make yellow?

  • A. Red and green
  • B. Red and blue
  • C. Green and blue only
Why: Correct — red and green together look yellow on an RGB LED.

Code lab — try on your own

  1. Cycle colors faster — change delay(600) to delay(300) throughout loop().

    Hint: Many delay lines share the same number.

  2. Add a comment on the yellow setChannels(HIGH, HIGH, LOW) line.

    Hint: It is the second setChannels call in loop().

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

Mix RGB channels to make yellow, cyan, magenta, and white.

Why here

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

const int PIN_R = 9;
const int PIN_G = 10;
const int PIN_B = 11;
void setChannels(int r, int g, int b) {
  digitalWrite(PIN_R, r);
  digitalWrite(PIN_G, g);
  digitalWrite(PIN_B, b);
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for RGB Color Mixer.

Why here

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

void setup() {
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in RGB Color Mixer.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  setChannels(HIGH, LOW, LOW);
  delay(600);
  setChannels(HIGH, HIGH, LOW);
  delay(600);
  setChannels(LOW, HIGH, LOW);
  delay(600);
  setChannels(LOW, HIGH, HIGH);
  delay(600);
  setChannels(LOW, LOW, HIGH);
  delay(600);
  setChannels(HIGH, LOW, HIGH);
  delay(600);
  setChannels(HIGH, HIGH, HIGH);
  delay(600);
}

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 RGB Color Mixer circuit ready in the simulator.

Why here

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

  pinMode(PIN_R, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in RGB Color Mixer.

Why here

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

  digitalWrite(PIN_R, r);

delay

Technical

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

In this project

Controls speed so you can see RGB Color Mixer in the simulator.

Why here

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

  delay(600);
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));
Mission 10 · Stage 1

Mini Light Show

timed show sequences

Run a timed light show sequence on a bar graph.

Mini Light Show circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

LED bar graph

A1

Arduino

pin 3

LED bar graph

A2

Arduino

pin 4

LED bar graph

A3

Arduino

pin 5

LED bar graph

A4

Arduino

pin 6

LED bar graph

A5

Arduino

pin 7

LED bar graph

A6

Arduino

pin 8

LED bar graph

A7

Arduino

pin 9

LED bar graph

A8

Arduino

pin 10

LED bar graph

A9

Arduino

pin 11

LED bar graph

A10

LED bar graph

C10

Arduino

GND

See it

Your own light show sequence!

Wave in, wave out, then blink all — a timed multi-step show.

Stage lights and signs run scripted sequences like this.

The story

The problem

One pattern is not enough — chain several timed acts together.

Think of it like

Like a dance routine: step one, step two, finale.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Ten segments you can light up

LED bar graph

Display

Loading part…

How it works

1

Act 1 — wave in

One segment at a time turns on from left to right — the bar fills up.

digitalWrite(FIRST_PIN + i, HIGH);
delay(80);
2

Hold full bar

Every segment stays on briefly before the next act.

delay(500);
3

Act 2 — wave out

Segments turn off one by one from right to left.

digitalWrite(FIRST_PIN + i, LOW);
delay(80);
4

Act 3 — blink together

The whole bar flashes three times — a finale before the pause.

allOn(); delay(150); allOff(); delay(150);
5

Reset and repeat

A long pause, then loop() runs the whole show from Act 1 again.

delay(800);

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 LED bar graph

    Place the LED bar graph (bar1) on the breadboard.

    Loading part…
  3. 3

    Connect Arduino pin 2 to LED bar graph (bar1) A1

    Connect Arduino pin 2 to LED bar graph (bar1) A1.

  4. 4

    Connect Arduino pin 3 to LED bar graph (bar1) A2

    Connect Arduino pin 3 to LED bar graph (bar1) A2.

  5. 5

    Connect Arduino pin 4 to LED bar graph (bar1) A3

    Connect Arduino pin 4 to LED bar graph (bar1) A3.

  6. 6

    Connect Arduino pin 5 to LED bar graph (bar1) A4

    Connect Arduino pin 5 to LED bar graph (bar1) A4.

  7. 7

    Connect Arduino pin 6 to LED bar graph (bar1) A5

    Connect Arduino pin 6 to LED bar graph (bar1) A5.

  8. 8

    Connect Arduino pin 7 to LED bar graph (bar1) A6

    Connect Arduino pin 7 to LED bar graph (bar1) A6.

  9. 9

    Connect Arduino pin 8 to LED bar graph (bar1) A7

    Connect Arduino pin 8 to LED bar graph (bar1) A7.

  10. 10

    Connect Arduino pin 9 to LED bar graph (bar1) A8

    Connect Arduino pin 9 to LED bar graph (bar1) A8.

  11. 11

    Connect Arduino pin 10 to LED bar graph (bar1) A9

    Connect Arduino pin 10 to LED bar graph (bar1) A9.

  12. 12

    Connect Arduino pin 11 to LED bar graph (bar1) A10

    Connect Arduino pin 11 to LED bar graph (bar1) A10.

  13. 13

    Connect LED bar graph (bar1) C10 to Arduino GND

    Connect LED bar graph (bar1) C10 to Arduino GND.

Try it

  • Run the full show: fill up, hold, empty, blink, pause.
  • Try to predict which act comes next!

Peek at code

Helper functions

void allOff() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, LOW);
  }
}
void allOn() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, HIGH);
  }
}

allOff() and allOn() use loops so we do not write ten digitalWrite lines by hand.

Setup the bar

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

Every bar pin is set to OUTPUT once in setup().

Full show sequence

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(80);
  }
  delay(500);
  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    digitalWrite(FIRST_PIN + i, LOW);
    delay(80);
  }
  delay(300);
  for (int n = 0; n < 3; n++) {
    allOn();
    delay(150);
    allOff();
    delay(150);
  }
  delay(800);
}

loop() chains wave-in, pause, wave-out, blink finale, and rest — a timed light show.

Show full sketch (mini-light-show.ino)
const int FIRST_PIN = 2;
const int NUM_LEDS = 10;
void allOff() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, LOW);
  }
}
void allOn() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, HIGH);
  }
}
void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}
void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(80);
  }
  delay(500);
  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    digitalWrite(FIRST_PIN + i, LOW);
    delay(80);
  }
  delay(300);
  for (int n = 0; n < 3; n++) {
    allOn();
    delay(150);
    allOff();
    delay(150);
  }
  delay(800);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What makes this a "show sequence"?

  • A. Several timed steps in order in loop()
  • B. One delay() only
  • C. No digitalWrite() calls
Why: Correct — ordered steps with delays build the full show.

Code lab — try on your own

  1. Speed up the wave — change delay(80) to delay(40) in both wave loops.

    Hint: Lines inside the for-loops that light segments one by one.

  2. Add one extra flash — change the blink loop from n < 3 to n < 4.

    Hint: Find for (int n = 0; n < 3; n++).

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

Run a timed light show sequence on a bar graph.

Why here

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

const int FIRST_PIN = 2;
const int NUM_LEDS = 10;
void allOff() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, LOW);
  }
}
void allOn() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, HIGH);
  }
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Mini Light Show.

Why here

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

void setup() {
  for (int i = 0; i < NUM_LEDS; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);
  }
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Mini Light Show.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  for (int i = 0; i < NUM_LEDS; i++) {
    digitalWrite(FIRST_PIN + i, HIGH);
    delay(80);
  }
  delay(500);
  for (int i = NUM_LEDS - 1; i >= 0; i--) {
    digitalWrite(FIRST_PIN + i, LOW);
    delay(80);
  }
  delay(300);
  for (int n = 0; n < 3; n++) {
    allOn();
    delay(150);
    allOff();
    delay(150);
  }
  delay(800);
}

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 Mini Light Show circuit ready in the simulator.

Why here

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

    pinMode(FIRST_PIN + i, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Mini Light Show.

Why here

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

    digitalWrite(FIRST_PIN + i, LOW);

delay

Technical

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

In this project

Controls speed so you can see Mini Light Show in the simulator.

Why here

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

    delay(80);

Stage 2: Inputs

digitalRead(), if statements

Stage 2 teaches inputs and decisions. Your Arduino can read buttons and switches with digitalRead(), then use if statements to choose what to do. You will build games, timers, and alarms.

Mission 11 · Stage 2

Push Button LED

digitalRead() reads an input pin

Toggle an LED with a pushbutton using internal pull-up resistor on pin 2.

Push Button LED circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button

pin 1

Button

pin 2

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

Your code can listen!

Press the button — digitalRead() tells the Arduino what you did.

Every keyboard key and game controller button works this way.

The story

The problem

Outputs alone cannot react — you need to read an input pin.

Think of it like

Like asking “Is the door open?” before turning on a light.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Press to signal the Arduino

Button

Input

Loading part…

Protects the LED

Resistor

Safety

Loading part…

Mirrors button state

LED

Output

Loading part…

How it works

1

Read the button

digitalRead() returns HIGH or LOW. With INPUT_PULLUP, pressed means LOW.

bool pressed = (digitalRead(BUTTON_PIN) == LOW);
2

Mirror to the LED

The LED follows the button — on while you hold it, off when released.

digitalWrite(LED_PIN, pressed ? HIGH : LOW);
3

Small pause

A tiny debounce delay keeps readings stable, then loop() reads again.

delay(50);

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 Button

    Place the Button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  4. 4

    Place LED

    Place the LED (led1) on the breadboard.

    Loading part…
  5. 5

    Connect Arduino pin 2 to Button (btn1) 1.l

    Connect Arduino pin 2 to Button (btn1) 1.l.

  6. 6

    Connect Button (btn1) 2.l to Arduino GND

    Connect Button (btn1) 2.l to Arduino GND.

  7. 7

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  8. 8

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

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

  9. 9

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

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

Try it

  • Hold the button — LED should stay on.
  • Release — LED turns off. Watch Serial too!

Peek at code

Input pull-up

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);

INPUT_PULLUP makes the pin HIGH until you press the button to GND.

digitalRead()

void loop() {
  bool pressed = (digitalRead(BUTTON_PIN) == LOW);
  digitalWrite(LED_PIN, pressed ? HIGH : LOW);
  Serial.println(pressed ? "Button pressed" : "Button released");
  delay(50);

Each loop pass reads the button and updates the LED and Serial monitor.

Show full sketch (pushbutton.ino)
const int BUTTON_PIN = 2;
const int LED_PIN    = 13;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  bool pressed = (digitalRead(BUTTON_PIN) == LOW);
  digitalWrite(LED_PIN, pressed ? HIGH : LOW);
  Serial.println(pressed ? "Button pressed" : "Button released");
  delay(50);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. With INPUT_PULLUP, what does digitalRead return when the button is pressed?

  • A. LOW
  • B. HIGH
  • C. A random number
Why: Correct — pressed connects to GND, so the pin reads LOW.

Code lab — try on your own

  1. Slow the read loop — change delay(50) to delay(100).

    Hint: Line 12 in loop().

  2. Add a comment on the digitalRead line explaining “pressed = LOW”.

    Hint: Line 9.

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

Toggle an LED with a pushbutton using internal pull-up resistor on pin 2.

Why here

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

const int BUTTON_PIN = 2;
const int LED_PIN    = 13;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Pushbutton.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Pushbutton.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  bool pressed = (digitalRead(BUTTON_PIN) == LOW);
  digitalWrite(LED_PIN, pressed ? HIGH : LOW);
  Serial.println(pressed ? "Button pressed" : "Button released");
  delay(50);
}

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 Pushbutton circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Pushbutton.

Why here

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

  digitalWrite(LED_PIN, pressed ? HIGH : LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Pushbutton.

Why here

Goes in loop() so we can react when something changes.

  bool pressed = (digitalRead(BUTTON_PIN) == LOW);

delay

Technical

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

In this project

Controls speed so you can see Pushbutton in the simulator.

Why here

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

  delay(50);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Pushbutton print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Pushbutton.

Why here

In loop() when each reading should appear on its own line.

  Serial.println(pressed ? "Button pressed" : "Button released");
Mission 12 · Stage 2

Push Button Buzzer

if statement branches output

Play a buzzer tone when the button is pressed using an if statement.

Push Button Buzzer circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button

pin 1

Button

pin 2

Arduino

GND

Buzzer

pin 1 (+)

Arduino

pin 8

Buzzer

pin 2 (-)

Arduino

GND

See it

if — your first decision!

Press the button and the buzzer plays; release and it stops.

if statements choose what code runs — in alarms, games, and robots.

The story

The problem

You want different outputs depending on whether the button is pressed.

Think of it like

Like saying “If it is raining, take an umbrella.”

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Triggers the if branch

Button

Input

Loading part…

Beeps when pressed

Buzzer

Sound

Loading part…

How it works

1

Check button

The if condition is true only while you hold the button.

if (digitalRead(BUTTON_PIN) == LOW)
2

Play tone

Inside the if block — buzzer sounds at 440 Hz.

tone(BUZZER_PIN, 440);
3

Otherwise stop

When not pressed, noTone() keeps the buzzer quiet.

else { noTone(BUZZER_PIN); }

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 Button

    Place the Button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Buzzer

    Place the Buzzer (bz1) on the breadboard.

    Loading part…
  4. 4

    Connect Arduino pin 2 to Button (btn1) 1.l

    Connect Arduino pin 2 to Button (btn1) 1.l.

  5. 5

    Connect Button (btn1) 2.l to Arduino GND

    Connect Button (btn1) 2.l to Arduino GND.

  6. 6

    Connect Buzzer (bz1) 1 to Arduino pin 8

    Connect Buzzer (bz1) 1 to Arduino pin 8.

  7. 7

    Connect Buzzer (bz1) 2 to Arduino GND

    Connect Buzzer (bz1) 2 to Arduino GND.

Try it

  • Press and hold — you should hear a steady beep.
  • Release — silence! That is if/else in action.

Peek at code

Setup input and output

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);
}

Button is an input with pull-up; buzzer pin is an output.

if / else branch

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    tone(BUZZER_PIN, 440);
  } else {
    noTone(BUZZER_PIN);
  }
  delay(20);
}

One branch plays tone(), the other calls noTone() — classic if decision.

Show full sketch (pushbutton-buzzer.ino)
const int BUTTON_PIN = 2;
const int BUZZER_PIN = 8;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    tone(BUZZER_PIN, 440);
  } else {
    noTone(BUZZER_PIN);
  }
  delay(20);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What runs when the button is NOT pressed?

  • A. The else block with noTone()
  • B. setup() again
  • C. tone() forever
Why: Correct — else calls noTone() to stop the buzzer.

Code lab — try on your own

  1. Change the tone pitch — try tone(BUZZER_PIN, 880) instead of 440.

    Hint: Inside the if block, line 9.

  2. Add a comment above the if line: // buzzer when pressed

    Hint: Line 8.

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

Play a buzzer tone when the button is pressed using an if statement.

Why here

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

const int BUTTON_PIN = 2;
const int BUZZER_PIN = 8;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Push Button Buzzer.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Push Button Buzzer.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    tone(BUZZER_PIN, 440);
  } else {
    noTone(BUZZER_PIN);
  }
  delay(20);
}

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 Push Button Buzzer circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Push Button Buzzer.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(BUTTON_PIN) == LOW) {

tone

Technical

Plays a beep on a buzzer pin at a chosen pitch.

In this project

Makes sounds in Push Button Buzzer.

Why here

Goes in loop() when you want notes or alarms.

    tone(BUZZER_PIN, 440);

noTone

Technical

Stops the buzzer sound on that pin.

In this project

Turns off the sound in Push Button Buzzer.

Why here

After tone() or before a pause so the buzzer is silent.

    noTone(BUZZER_PIN);

delay

Technical

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

In this project

Controls speed so you can see Push Button Buzzer in the simulator.

Why here

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

  delay(20);
Mission 13 · Stage 2

Security Switch

switch state vs momentary button

Use a slide switch to toggle an LED on and off.

Security Switch circuit diagram

Pin connections

Part 1Part 2

Slide switch

pin 2

Arduino

pin 2

Slide switch

pin 1

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

A switch stays where you leave it!

Unlike a momentary button, the slide switch keeps its ON or OFF state.

Light switches on walls are latching — they hold their position.

The story

The problem

A push button only signals while pressed — sometimes you need a lasting state.

Think of it like

A button is a doorbell; a switch is a light switch on the wall.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Stays ON or OFF

Slide switch

Input

Loading part…

Protects the LED

Resistor

Safety

Loading part…

Shows switch position

LED

Output

Loading part…

How it works

1

Read switch position

digitalRead works the same — but the switch stays LOW or HIGH until you move it.

bool active = (digitalRead(SWITCH_PIN) == LOW);
2

Show state on LED

LED stays on while the switch is ON — no need to keep holding anything.

digitalWrite(LED_PIN, active ? HIGH : LOW);

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 Slide switch

    Place the Slide switch (sw1) on the breadboard.

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  4. 4

    Place LED

    Place the LED (led1) on the breadboard.

    Loading part…
  5. 5

    Connect Slide switch (sw1) 2 to Arduino pin 2

    Connect Slide switch (sw1) 2 to Arduino pin 2.

  6. 6

    Connect Slide switch (sw1) 1 to Arduino GND

    Connect Slide switch (sw1) 1 to Arduino GND.

  7. 7

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  8. 8

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

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

  9. 9

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

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

Try it

  • Flip the switch — LED should stay on or off.
  • Compare with pushbutton: no need to hold it!

Peek at code

Switch pin setup

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Slide Switch Demo");
}

Same INPUT_PULLUP pattern as a button — the part behaves differently in hardware.

Read and mirror

void loop() {
  bool active = (digitalRead(SWITCH_PIN) == LOW);
  digitalWrite(LED_PIN, active ? HIGH : LOW);
  Serial.println(active ? "Switch ON  -> LED ON" : "Switch OFF -> LED OFF");
  delay(100);
}

loop() keeps checking the switch; the LED reflects the latched position.

Show full sketch (slide-switch.ino)
const int SWITCH_PIN = 2;
const int LED_PIN    = 13;
void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Slide Switch Demo");
}
void loop() {
  bool active = (digitalRead(SWITCH_PIN) == LOW);
  digitalWrite(LED_PIN, active ? HIGH : LOW);
  Serial.println(active ? "Switch ON  -> LED ON" : "Switch OFF -> LED OFF");
  delay(100);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. How is a slide switch different from a push button?

  • A. It keeps its position until you move it
  • B. It only works in setup()
  • C. It does not use digitalRead()
Why: Correct — a switch holds ON or OFF; a button returns when released.

Code lab — try on your own

  1. Change delay(100) to delay(200) in loop().

    Hint: Last line of loop().

  2. Add a comment on line 10: // switch ON when LOW

    Hint: The active = digitalRead line.

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

Use a slide switch to toggle an LED on and off.

Why here

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

const int SWITCH_PIN = 2;
const int LED_PIN    = 13;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Slide Switch.

Why here

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

void setup() {
  pinMode(SWITCH_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Slide Switch Demo");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Slide Switch.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  bool active = (digitalRead(SWITCH_PIN) == LOW);
  digitalWrite(LED_PIN, active ? HIGH : LOW);
  Serial.println(active ? "Switch ON  -> LED ON" : "Switch OFF -> LED OFF");
  delay(100);
}

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 Slide Switch circuit ready in the simulator.

Why here

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

  pinMode(SWITCH_PIN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Slide Switch.

Why here

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

  digitalWrite(LED_PIN, active ? HIGH : LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Slide Switch.

Why here

Goes in loop() so we can react when something changes.

  bool active = (digitalRead(SWITCH_PIN) == LOW);

delay

Technical

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

In this project

Controls speed so you can see Slide Switch in the simulator.

Why here

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

  delay(100);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Slide Switch print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Slide Switch.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Slide Switch Demo");
Mission 14 · Stage 2

Toggle Button

edge detection and toggle latch

Each button press toggles an LED on or off using edge detection.

Toggle Button circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button

pin 1

Button

pin 2

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

One press, flip the state!

Detect the moment of press (edge) to toggle the LED on or off.

Power buttons on devices often toggle with edge detection.

The story

The problem

Holding a button on is not a toggle — you need to catch the press instant.

Think of it like

Like flipping a coin once per tap — not while your finger rests.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Each press toggles state

Button

Input

Loading part…

Protects the LED

Resistor

Safety

Loading part…

Stays on until next press

LED

Output

Loading part…

How it works

1

Remember last reading

We compare today’s reading with lastReading to spot a change.

bool reading = digitalRead(BUTTON_PIN);
2

Detect press edge

HIGH→LOW means the button was just pressed — one edge, one toggle.

if (lastReading == HIGH && reading == LOW)
3

Flip stored state

ledOn latches the LED state between presses.

ledOn = !ledOn;
digitalWrite(LED_PIN, ledOn ? HIGH : LOW);

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 Button

    Place the Button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  4. 4

    Place LED

    Place the LED (led1) on the breadboard.

    Loading part…
  5. 5

    Connect Arduino pin 2 to Button (btn1) 1.l

    Connect Arduino pin 2 to Button (btn1) 1.l.

  6. 6

    Connect Button (btn1) 2.l to Arduino GND

    Connect Button (btn1) 2.l to Arduino GND.

  7. 7

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  8. 8

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

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

  9. 9

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

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

Try it

  • Tap the button — LED toggles each press.
  • Hold it down — should NOT flicker on/off rapidly.

Peek at code

Toggle state variables

bool ledOn = false;
bool lastReading = HIGH;

ledOn remembers the LED; lastReading remembers the previous button read.

Edge detection

void loop() {
  bool reading = digitalRead(BUTTON_PIN);
  if (lastReading == HIGH && reading == LOW) {
    ledOn = !ledOn;
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
  }
  lastReading = reading;
  delay(10);
}

Only on a fresh press does the LED flip — holding the button does nothing extra.

Show full sketch (toggle-button.ino)
const int BUTTON_PIN = 2;
const int LED_PIN = 13;
bool ledOn = false;
bool lastReading = HIGH;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}
void loop() {
  bool reading = digitalRead(BUTTON_PIN);
  if (lastReading == HIGH && reading == LOW) {
    ledOn = !ledOn;
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
  }
  lastReading = reading;
  delay(10);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. Why check lastReading == HIGH && reading == LOW?

  • A. To detect the instant of pressing
  • B. To make the LED brighter
  • C. To run setup() again
Why: Correct — edge detection fires once per press, not while held.

Code lab — try on your own

  1. Make edge checks snappier — change delay(10) to delay(5).

    Hint: Last line of loop().

  2. Comment the line ledOn = !ledOn explaining “flip state”.

    Hint: Line 13.

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

Each button press toggles an LED on or off using edge detection.

Why here

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

const int BUTTON_PIN = 2;
const int LED_PIN = 13;
bool ledOn = false;
bool lastReading = HIGH;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Toggle Button.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Toggle Button.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  bool reading = digitalRead(BUTTON_PIN);
  if (lastReading == HIGH && reading == LOW) {
    ledOn = !ledOn;
    digitalWrite(LED_PIN, ledOn ? HIGH : LOW);
  }
  lastReading = reading;
  delay(10);
}

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 Toggle Button circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Toggle Button.

Why here

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

  digitalWrite(LED_PIN, LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Toggle Button.

Why here

Goes in loop() so we can react when something changes.

  bool reading = digitalRead(BUTTON_PIN);

delay

Technical

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

In this project

Controls speed so you can see Toggle Button in the simulator.

Why here

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

  delay(10);
Mission 15 · Stage 2

Reaction Timer

millis() with if conditions

Measure button reaction time with millis() after a random GO signal.

Reaction Timer circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button

pin 1

Button

pin 2

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

GO LED

anode (+)

GO LED

cathode (-)

Arduino

GND

See it

Time reactions with millis()!

Wait… wait… GO! Press as fast as you can — millis() measures your speed.

Games and sports timers use millis() so other code can still run.

The story

The problem

delay() would freeze everything — you need a clock that keeps ticking.

Think of it like

millis() is a stopwatch that never stops counting milliseconds.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Press when GO lights up

Button

Input

Loading part…

Protects the GO LED

Resistor

Safety

Loading part…

Tells you when to press

GO LED

Signal

Loading part…

How it works

1

Wait random time

No delay() — we compare millis() to a target time stored in waitUntil.

if (state == WAIT && millis() >= waitUntil)
2

Show GO

LED turns on and we record the exact moment with goTime.

digitalWrite(GO_LED, HIGH);
goTime = millis();
3

Measure reaction

Subtract goTime from now to get milliseconds since GO.

reaction = millis() - goTime;

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 Button

    Place the Button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  4. 4

    Place GO LED

    Place the GO LED (led1) on the breadboard.

    Loading part…
  5. 5

    Connect Arduino pin 2 to Button (btn1) 1.l

    Connect Arduino pin 2 to Button (btn1) 1.l.

  6. 6

    Connect Button (btn1) 2.l to Arduino GND

    Connect Button (btn1) 2.l to Arduino GND.

  7. 7

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  8. 8

    Connect Resistor (r1) 2 to GO LED (led1) anode (+)

    Connect Resistor (r1) 2 to GO LED (led1) anode (+).

  9. 9

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

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

Try it

  • Open Serial Monitor — wait for GO!, then press fast.
  • Read your reaction time in milliseconds.

Peek at code

State machine

enum State { WAIT, GO, SCORED };
State state = WAIT;
unsigned long goTime = 0;
unsigned long waitUntil = 0;

State enum tracks WAIT, GO, and SCORED — the program mode changes with if.

scheduleWait()

void scheduleWait() {
  digitalWrite(GO_LED, LOW);
  waitUntil = millis() + random(2000, 5000);
  state = WAIT;
}

Sets a future millis() target for the random wait — non-blocking timing.

Reaction math

void loop() {
  if (state == WAIT && millis() >= waitUntil) {
    digitalWrite(GO_LED, HIGH);
    goTime = millis();
    state = GO;
    Serial.println("GO! Press the button!");
  }
  if (state == GO && digitalRead(BUTTON_PIN) == LOW) {
    unsigned long reaction = millis() - goTime;
    Serial.print("Reaction: ");
    Serial.print(reaction);
    Serial.println(" ms");
    state = SCORED;
    delay(2000);
    scheduleWait();
  }
  delay(10);

loop() uses if on state and millis() to start GO and compute reaction time.

Show full sketch (reaction-timer.ino)
const int BUTTON_PIN = 2;
const int GO_LED = 13;
enum State { WAIT, GO, SCORED };
State state = WAIT;
unsigned long goTime = 0;
unsigned long waitUntil = 0;
void scheduleWait() {
  digitalWrite(GO_LED, LOW);
  waitUntil = millis() + random(2000, 5000);
  state = WAIT;
}
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(GO_LED, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  scheduleWait();
  Serial.println("Reaction Timer — wait for GO!");
}
void loop() {
  if (state == WAIT && millis() >= waitUntil) {
    digitalWrite(GO_LED, HIGH);
    goTime = millis();
    state = GO;
    Serial.println("GO! Press the button!");
  }
  if (state == GO && digitalRead(BUTTON_PIN) == LOW) {
    unsigned long reaction = millis() - goTime;
    Serial.print("Reaction: ");
    Serial.print(reaction);
    Serial.println(" ms");
    state = SCORED;
    delay(2000);
    scheduleWait();
  }
  delay(10);
}

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 millis() instead of delay() while waiting?

  • A. millis() lets the program check other conditions
  • B. millis() is louder
  • C. delay() reads buttons faster
Why: Correct — millis() counts time without freezing the whole sketch.

Code lab — try on your own

  1. Make waits shorter — change random(2000, 5000) to random(1000, 3000) in scheduleWait().

    Hint: Line 9.

  2. Comment line 28 explaining reaction = time since GO.

    Hint: The reaction = millis() - goTime line.

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

Measure button reaction time with millis() after a random GO signal.

Why here

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

const int BUTTON_PIN = 2;
const int GO_LED = 13;
enum State { WAIT, GO, SCORED };
State state = WAIT;
unsigned long goTime = 0;
unsigned long waitUntil = 0;
void scheduleWait() {
  digitalWrite(GO_LED, LOW);
  waitUntil = millis() + random(2000, 5000);
  state = WAIT;
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Reaction Timer.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(GO_LED, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  scheduleWait();
  Serial.println("Reaction Timer — wait for GO!");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Reaction Timer.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (state == WAIT && millis() >= waitUntil) {
    digitalWrite(GO_LED, HIGH);
    goTime = millis();
    state = GO;
    Serial.println("GO! Press the button!");
  }
  if (state == GO && digitalRead(BUTTON_PIN) == LOW) {
    unsigned long reaction = millis() - goTime;
    Serial.print("Reaction: ");
    Serial.print(reaction);
    Serial.println(" ms");
    state = SCORED;
    delay(2000);
    scheduleWait();
  }
  delay(10);
}

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 Reaction Timer circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Reaction Timer.

Why here

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

  digitalWrite(GO_LED, LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Reaction Timer.

Why here

Goes in loop() so we can react when something changes.

  if (state == GO && digitalRead(BUTTON_PIN) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Reaction Timer.

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 Reaction Timer in the simulator.

Why here

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

    delay(2000);

millis

Technical

Counts milliseconds since the board started.

In this project

Tracks time in Reaction Timer without using delay().

Why here

In loop() when you need to know how much time passed.

  waitUntil = millis() + random(2000, 5000);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Reaction Timer.

Why here

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

  waitUntil = millis() + random(2000, 5000);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Reaction Timer print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Reaction Timer on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

    Serial.print("Reaction: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Reaction Timer.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Reaction Timer — wait for GO!");
Mission 16 · Stage 2

Quiz Buzzer

multiple inputs and first-wins logic

Two players race — first button press wins and locks out the other.

Quiz Buzzer circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Player A button

pin 1

Player A button

pin 2

Arduino

GND

Arduino

pin 3

Player B button

pin 1

Player B button

pin 2

Arduino

GND

Arduino

pin 12

Resistor A

pin 2

Resistor A

pin 1

Player A LED

anode (+)

Player A LED

cathode (-)

Arduino

GND

Arduino

pin 13

Resistor B

pin 2

Resistor B

pin 1

Player B LED

anode (+)

Player B LED

cathode (-)

Arduino

GND

Arduino

pin 8

Buzzer

pin 1 (+)

Buzzer

pin 2 (-)

Arduino

GND

See it

First press wins!

Two players, one buzzer — locked stops the other from scoring.

Quiz shows use exactly this “first valid input wins” logic.

The story

The problem

Two inputs can fire at once — you need rules for who wins.

Think of it like

Like slapping a buzzer in trivia — first hand down locks the round.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

First press wins

Player A button

Input

Loading part…

Second player buzzes in

Player B button

Input

Loading part…

Shows A won the round

Player A LED

Output

Loading part…

Shows B won the round

Player B LED

Output

Loading part…

Protects A LED

Resistor A

Safety

Loading part…

Protects B LED

Resistor B

Safety

Loading part…

Celebrates the winner

Buzzer

Sound

Loading part…

How it works

1

Still open?

Only accept input while locked is false.

if (!locked && digitalRead(BTN_A) == LOW)
2

Player A wins

First valid press sets locked so B cannot win this round.

locked = true;
digitalWrite(LED_A, HIGH);
tone(BUZZER, 523);
3

Reset round

After both buttons release, locked clears for the next question.

resetRound();

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 Player A button

    Place the Player A button (btnA) on the breadboard.

    Loading part…
  3. 3

    Place Player B button

    Place the Player B button (btnB) on the breadboard.

    Loading part…
  4. 4

    Place Player A LED

    Place the Player A LED (ledA) on the breadboard.

    Loading part…
  5. 5

    Place Player B LED

    Place the Player B LED (ledB) on the breadboard.

    Loading part…
  6. 6

    Place Resistor A

    Place the Resistor A (rA) on the breadboard.

    Loading part…
  7. 7

    Place Resistor B

    Place the Resistor B (rB) on the breadboard.

    Loading part…
  8. 8

    Place Buzzer

    Place the Buzzer (bz1) on the breadboard.

    Loading part…
  9. 9

    Connect Arduino pin 2 to Player A button (btnA) 1.l

    Connect Arduino pin 2 to Player A button (btnA) 1.l.

  10. 10

    Connect Player A button (btnA) 2.l to Arduino GND

    Connect Player A button (btnA) 2.l to Arduino GND.

  11. 11

    Connect Arduino pin 3 to Player B button (btnB) 1.l

    Connect Arduino pin 3 to Player B button (btnB) 1.l.

  12. 12

    Connect Player B button (btnB) 2.l to Arduino GND

    Connect Player B button (btnB) 2.l to Arduino GND.

  13. 13

    Connect Arduino pin 12 to Resistor A (rA) 2

    Connect Arduino pin 12 to Resistor A (rA) 2.

  14. 14

    Connect Resistor A (rA) 1 to Player A LED (ledA) anode (+)

    Connect Resistor A (rA) 1 to Player A LED (ledA) anode (+).

  15. 15

    Connect Player A LED (ledA) cathode (-) to Arduino GND

    Connect Player A LED (ledA) cathode (-) to Arduino GND.

  16. 16

    Connect Arduino pin 13 to Resistor B (rB) 2

    Connect Arduino pin 13 to Resistor B (rB) 2.

  17. 17

    Connect Resistor B (rB) 1 to Player B LED (ledB) anode (+)

    Connect Resistor B (rB) 1 to Player B LED (ledB) anode (+).

  18. 18

    Connect Player B LED (ledB) cathode (-) to Arduino GND

    Connect Player B LED (ledB) cathode (-) to Arduino GND.

  19. 19

    Connect Arduino pin 8 to Buzzer (bz1) 1

    Connect Arduino pin 8 to Buzzer (bz1) 1.

  20. 20

    Connect Buzzer (bz1) 2 to Arduino GND

    Connect Buzzer (bz1) 2 to Arduino GND.

Try it

  • Player A: red button. Player B: blue button.
  • First press lights their LED — second press should do nothing until reset.

Peek at code

Lock flag

bool locked = false;
void resetRound() {
  locked = false;
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, LOW);
  noTone(BUZZER);
}

locked is a boolean game rule — once true, other presses are ignored.

First-wins logic

void loop() {
  if (!locked && digitalRead(BTN_A) == LOW) {
    locked = true;
    digitalWrite(LED_A, HIGH);
    tone(BUZZER, 523, 500);
  } else if (!locked && digitalRead(BTN_B) == LOW) {
    locked = true;
    digitalWrite(LED_B, HIGH);
    tone(BUZZER, 440, 500);
  }
  if (locked && digitalRead(BTN_A) == HIGH && digitalRead(BTN_B) == HIGH) {
    delay(1500);
    resetRound();
  }
  delay(20);
}

if / else if checks A then B; resetRound() clears LEDs and locked.

Show full sketch (quiz-buzzer.ino)
const int BTN_A = 2;
const int BTN_B = 3;
const int LED_A = 12;
const int LED_B = 13;
const int BUZZER = 8;
bool locked = false;
void resetRound() {
  locked = false;
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, LOW);
  noTone(BUZZER);
}
void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  resetRound();
}
void loop() {
  if (!locked && digitalRead(BTN_A) == LOW) {
    locked = true;
    digitalWrite(LED_A, HIGH);
    tone(BUZZER, 523, 500);
  } else if (!locked && digitalRead(BTN_B) == LOW) {
    locked = true;
    digitalWrite(LED_B, HIGH);
    tone(BUZZER, 440, 500);
  }
  if (locked && digitalRead(BTN_A) == HIGH && digitalRead(BTN_B) == HIGH) {
    delay(1500);
    resetRound();
  }
  delay(20);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What does locked = true prevent?

  • A. The other player winning after the first press
  • B. The buzzer from ever working
  • C. setup() from running
Why: Correct — first press locks the round for that player.

Code lab — try on your own

  1. Shorten between rounds — change delay(1500) to delay(800).

    Hint: Line 32.

  2. Comment line 23: // Player A wins first

    Hint: Inside the first if block.

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

Two players race — first button press wins and locks out the other.

Why here

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

const int BTN_A = 2;
const int BTN_B = 3;
const int LED_A = 12;
const int LED_B = 13;
const int BUZZER = 8;
bool locked = false;
void resetRound() {
  locked = false;
  digitalWrite(LED_A, LOW);
  digitalWrite(LED_B, LOW);
  noTone(BUZZER);
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Quiz Buzzer.

Why here

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

void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(LED_A, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  resetRound();
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Quiz Buzzer.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (!locked && digitalRead(BTN_A) == LOW) {
    locked = true;
    digitalWrite(LED_A, HIGH);
    tone(BUZZER, 523, 500);
  } else if (!locked && digitalRead(BTN_B) == LOW) {
    locked = true;
    digitalWrite(LED_B, HIGH);
    tone(BUZZER, 440, 500);
  }
  if (locked && digitalRead(BTN_A) == HIGH && digitalRead(BTN_B) == HIGH) {
    delay(1500);
    resetRound();
  }
  delay(20);
}

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 Quiz Buzzer circuit ready in the simulator.

Why here

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

  pinMode(BTN_A, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Quiz Buzzer.

Why here

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

  digitalWrite(LED_A, LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Quiz Buzzer.

Why here

Goes in loop() so we can react when something changes.

  if (!locked && digitalRead(BTN_A) == LOW) {

tone

Technical

Plays a beep on a buzzer pin at a chosen pitch.

In this project

Makes sounds in Quiz Buzzer.

Why here

Goes in loop() when you want notes or alarms.

    tone(BUZZER, 523, 500);

noTone

Technical

Stops the buzzer sound on that pin.

In this project

Turns off the sound in Quiz Buzzer.

Why here

After tone() or before a pause so the buzzer is silent.

  noTone(BUZZER);

delay

Technical

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

In this project

Controls speed so you can see Quiz Buzzer in the simulator.

Why here

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

    delay(1500);
Mission 17 · Stage 2

Electronic Dice

random() triggered by a button

Roll a random number 1–6 on a 7-segment display when you press a button.

Electronic Dice circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Roll button

pin 1

Roll button

pin 2

Arduino

GND

Arduino

pin 3

7-segment display

seg A

Arduino

pin 4

7-segment display

seg B

Arduino

pin 5

7-segment display

seg C

Arduino

pin 6

7-segment display

seg D

Arduino

pin 7

7-segment display

seg E

Arduino

pin 8

7-segment display

seg F

Arduino

pin 9

7-segment display

seg G

7-segment display

COM

Arduino

GND

See it

Roll the dice with random()!

Press the button — random(1, 7) picks 1–6 on the display.

Board games and apps use random() for unpredictable outcomes.

The story

The problem

You want a new number each press, not a fixed pattern.

Think of it like

Like shaking a real die — you cannot guess the next value.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Triggers a new random roll

Roll button

Input

Loading part…

Shows die face 1–6

7-segment display

Display

Loading part…

How it works

1

Wait for press

Roll only when the player presses.

if (digitalRead(BUTTON_PIN) == LOW)
2

Pick random value

random(1, 7) gives 1 through 6 — perfect for a die.

int roll = random(1, 7);
3

Light segments

DIGITS[] table drives each segment ON/OFF for the number.

showDigit(roll);

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 Roll button

    Place the Roll button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place 7-segment display

    Place the 7-segment display (seg1) on the breadboard.

    Loading part…
  4. 4

    Connect Arduino pin 2 to Roll button (btn1) 1.l

    Connect Arduino pin 2 to Roll button (btn1) 1.l.

  5. 5

    Connect Roll button (btn1) 2.l to Arduino GND

    Connect Roll button (btn1) 2.l to Arduino GND.

  6. 6

    Connect Arduino pin 3 to 7-segment display (seg1) anode (+)

    Connect Arduino pin 3 to 7-segment display (seg1) anode (+).

  7. 7

    Connect Arduino pin 4 to 7-segment display (seg1) B

    Connect Arduino pin 4 to 7-segment display (seg1) B.

  8. 8

    Connect Arduino pin 5 to 7-segment display (seg1) cathode (-)

    Connect Arduino pin 5 to 7-segment display (seg1) cathode (-).

  9. 9

    Connect Arduino pin 6 to 7-segment display (seg1) D

    Connect Arduino pin 6 to 7-segment display (seg1) D.

  10. 10

    Connect Arduino pin 7 to 7-segment display (seg1) E

    Connect Arduino pin 7 to 7-segment display (seg1) E.

  11. 11

    Connect Arduino pin 8 to 7-segment display (seg1) F

    Connect Arduino pin 8 to 7-segment display (seg1) F.

  12. 12

    Connect Arduino pin 9 to 7-segment display (seg1) G

    Connect Arduino pin 9 to 7-segment display (seg1) G.

  13. 13

    Connect 7-segment display (seg1) COM.2 to Arduino GND

    Connect 7-segment display (seg1) COM.2 to Arduino GND.

Try it

  • Press the button repeatedly — numbers 1–6 should change.
  • No fair guessing — random() decides!

Peek at code

Segment patterns

const uint8_t DIGITS[7] = {
  0b0000000,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
};

Each digit is a bitmask — seven segments turn on/off to draw the number.

showDigit()

void showDigit(int digit) {
  uint8_t pattern = DIGITS[digit];
  for (int seg = 0; seg < 7; seg++) {
    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);
  }
}

Loops segments and uses digitalWrite with bit math from the pattern.

Roll on button

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    int roll = random(1, 7);
    showDigit(roll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(10);
    }
    delay(200);
  }
  delay(20);
}

Button triggers random(), then showDigit displays the result until next roll.

Show full sketch (electronic-dice.ino)
const int BUTTON_PIN = 2;
const int SEG_A = 3;
const uint8_t DIGITS[7] = {
  0b0000000,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
};
void showDigit(int digit) {
  uint8_t pattern = DIGITS[digit];
  for (int seg = 0; seg < 7; seg++) {
    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);
  }
}
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  for (int i = 0; i < 7; i++) {
    pinMode(SEG_A + i, OUTPUT);
  }
  showDigit(1);
  randomSeed(analogRead(0));
}
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    int roll = random(1, 7);
    showDigit(roll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(10);
    }
    delay(200);
  }
  delay(20);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What does random(1, 7) return?

  • A. 1 to 6 inclusive
  • B. 1 to 7 inclusive
  • C. Always 3
Why: Correct — seven is excluded, giving six die faces.

Code lab — try on your own

  1. Add a comment on randomSeed line explaining different rolls each run.

    Hint: Line 24 in setup().

  2. Change the post-roll delay(200) to delay(400).

    Hint: Line 33 in loop().

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

Roll a random number 1–6 on a 7-segment display when you press a button.

Why here

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

const int BUTTON_PIN = 2;
const int SEG_A = 3;
const uint8_t DIGITS[7] = {
  0b0000000,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
};
void showDigit(int digit) {
  uint8_t pattern = DIGITS[digit];
  for (int seg = 0; seg < 7; seg++) {
    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);
  }
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Electronic Dice.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  for (int i = 0; i < 7; i++) {
    pinMode(SEG_A + i, OUTPUT);
  }
  showDigit(1);
  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 Dice.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    int roll = random(1, 7);
    showDigit(roll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(10);
    }
    delay(200);
  }
  delay(20);
}

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 Dice circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Electronic Dice.

Why here

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

    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Electronic Dice.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(BUTTON_PIN) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Electronic Dice.

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 Dice in the simulator.

Why here

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

      delay(10);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Electronic Dice.

Why here

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

    int roll = random(1, 7);
Mission 18 · Stage 2

Simon Says

sequence compare with input

Watch a growing LED sequence, then repeat it with matching buttons.

Simon Says circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Red button

pin 1

Red button

pin 2

Arduino

GND

Arduino

pin 3

Green button

pin 1

Green button

pin 2

Arduino

GND

Arduino

pin 4

Yellow button

pin 1

Yellow button

pin 2

Arduino

GND

Arduino

pin 5

Blue button

pin 1

Blue button

pin 2

Arduino

GND

Arduino

pin 8

Red resistor

pin 2

Red resistor

pin 1

Red LED

anode (+)

Red LED

cathode (-)

Arduino

GND

Arduino

pin 9

Green resistor

pin 2

Green resistor

pin 1

Green LED

anode (+)

Green LED

cathode (-)

Arduino

GND

Arduino

pin 10

Yellow resistor

pin 2

Yellow resistor

pin 1

Yellow LED

anode (+)

Yellow LED

cathode (-)

Arduino

GND

Arduino

pin 11

Blue resistor

pin 2

Blue resistor

pin 1

Blue LED

anode (+)

Blue LED

cathode (-)

Arduino

GND

See it

Repeat the growing pattern!

Watch the LED sequence, then press matching buttons — one step longer each round.

Memory games train pattern matching — same idea as Simon toys.

The story

The problem

You must compare each input to a stored sequence step by step.

Think of it like

Like “Simon says” — copy the pattern or you are out.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Matches red in the sequence

Red button

Input

Loading part…

Matches green in the sequence

Green button

Input

Loading part…

Matches yellow in the sequence

Yellow button

Input

Loading part…

Matches blue in the sequence

Blue button

Input

Loading part…

Shows red in the pattern

Red LED

Output

Loading part…

Shows green in the pattern

Green LED

Output

Loading part…

Shows yellow in the pattern

Yellow LED

Output

Loading part…

Shows blue in the pattern

Blue LED

Output

Loading part…

Protects red LED

Red resistor

Safety

Loading part…

Protects green LED

Green resistor

Safety

Loading part…

Protects yellow LED

Yellow resistor

Safety

Loading part…

Protects blue LED

Blue resistor

Safety

Loading part…

How it works

1

Play sequence

Flashes LEDs in order from the sequence[] array.

showSequence();
2

Read your button

Each press maps to an index 0–3 matching an LED color.

digitalRead(BTN_PINS[i]) == LOW
3

Compare step

Wrong button triggers fail; correct adds another random step.

if (i != sequence[step]) failPattern();

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 Red button

    Place the Red button (btn0) on the breadboard.

    Loading part…
  3. 3

    Place Green button

    Place the Green button (btn1) on the breadboard.

    Loading part…
  4. 4

    Place Yellow button

    Place the Yellow button (btn2) on the breadboard.

    Loading part…
  5. 5

    Place Blue button

    Place the Blue button (btn3) on the breadboard.

    Loading part…
  6. 6

    Place Red LED

    Place the Red LED (led0) on the breadboard.

    Loading part…
  7. 7

    Place Green LED

    Place the Green LED (led1) on the breadboard.

    Loading part…
  8. 8

    Place Yellow LED

    Place the Yellow LED (led2) on the breadboard.

    Loading part…
  9. 9

    Place Blue LED

    Place the Blue LED (led3) on the breadboard.

    Loading part…
  10. 10

    Place Red resistor

    Place the Red resistor (r0) on the breadboard.

    Loading part…
  11. 11

    Place Green resistor

    Place the Green resistor (r1) on the breadboard.

    Loading part…
  12. 12

    Place Yellow resistor

    Place the Yellow resistor (r2) on the breadboard.

    Loading part…
  13. 13

    Place Blue resistor

    Place the Blue resistor (r3) on the breadboard.

    Loading part…
  14. 14

    Connect Arduino pin 2 to Red button (btn0) 1.l

    Connect Arduino pin 2 to Red button (btn0) 1.l.

  15. 15

    Connect Red button (btn0) 2.l to Arduino GND

    Connect Red button (btn0) 2.l to Arduino GND.

  16. 16

    Connect Arduino pin 3 to Green button (btn1) 1.l

    Connect Arduino pin 3 to Green button (btn1) 1.l.

  17. 17

    Connect Green button (btn1) 2.l to Arduino GND

    Connect Green button (btn1) 2.l to Arduino GND.

  18. 18

    Connect Arduino pin 4 to Yellow button (btn2) 1.l

    Connect Arduino pin 4 to Yellow button (btn2) 1.l.

  19. 19

    Connect Yellow button (btn2) 2.l to Arduino GND

    Connect Yellow button (btn2) 2.l to Arduino GND.

  20. 20

    Connect Arduino pin 5 to Blue button (btn3) 1.l

    Connect Arduino pin 5 to Blue button (btn3) 1.l.

  21. 21

    Connect Blue button (btn3) 2.l to Arduino GND

    Connect Blue button (btn3) 2.l to Arduino GND.

  22. 22

    Connect Arduino pin 8 to Red resistor (r0) 2

    Connect Arduino pin 8 to Red resistor (r0) 2.

  23. 23

    Connect Red resistor (r0) 1 to Red LED (led0) anode (+)

    Connect Red resistor (r0) 1 to Red LED (led0) anode (+).

  24. 24

    Connect Red LED (led0) cathode (-) to Arduino GND

    Connect Red LED (led0) cathode (-) to Arduino GND.

  25. 25

    Connect Arduino pin 9 to Green resistor (r1) 2

    Connect Arduino pin 9 to Green resistor (r1) 2.

  26. 26

    Connect Green resistor (r1) 1 to Green LED (led1) anode (+)

    Connect Green resistor (r1) 1 to Green LED (led1) anode (+).

  27. 27

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

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

  28. 28

    Connect Arduino pin 10 to Yellow resistor (r2) 2

    Connect Arduino pin 10 to Yellow resistor (r2) 2.

  29. 29

    Connect Yellow resistor (r2) 1 to Yellow LED (led2) anode (+)

    Connect Yellow resistor (r2) 1 to Yellow LED (led2) anode (+).

  30. 30

    Connect Yellow LED (led2) cathode (-) to Arduino GND

    Connect Yellow LED (led2) cathode (-) to Arduino GND.

  31. 31

    Connect Arduino pin 11 to Blue resistor (r3) 2

    Connect Arduino pin 11 to Blue resistor (r3) 2.

  32. 32

    Connect Blue resistor (r3) 1 to Blue LED (led3) anode (+)

    Connect Blue resistor (r3) 1 to Blue LED (led3) anode (+).

  33. 33

    Connect Blue LED (led3) cathode (-) to Arduino GND

    Connect Blue LED (led3) cathode (-) to Arduino GND.

Try it

  • Watch the color order first — then press matching buttons.
  • Wrong press blinks all LEDs — sequence resets.

Peek at code

Sequence storage

const int BTN_PINS[] = {2, 3, 4, 5};
const int LED_PINS[] = {8, 9, 10, 11};
const int NUM = 4;
const int MAX_LEN = 6;
int sequence[MAX_LEN];
int seqLen = 1;
int step = 0;
enum Phase { SHOW, INPUT };
Phase phase = SHOW;

sequence[] holds the pattern; seqLen grows as you succeed.

showSequence()

void showSequence() {
  for (int i = 0; i < seqLen; i++) {
    flash(sequence[i], 350);
    delay(180);
  }
}

Plays each stored index by flashing the matching LED.

Input compare

void loop() {
  if (phase == SHOW) {
    showSequence();
    step = 0;
    phase = INPUT;
    return;
  }
  for (int i = 0; i < NUM; i++) {
    if (digitalRead(BTN_PINS[i]) == LOW) {
      delay(180);
      flash(i, 250);
      if (i != sequence[step]) {
        failPattern();
        return;
      }
      step++;
      if (step >= seqLen) {
        seqLen++;
        if (seqLen > MAX_LEN) {
          seqLen = MAX_LEN;
        }
        sequence[seqLen - 1] = random(0, NUM);
        phase = SHOW;
      }
      while (digitalRead(BTN_PINS[i]) == LOW) {
        delay(10);
      }
    }
  }
  delay(20);
}

INPUT phase checks each press against sequence[step] — core Simon logic.

Show full sketch (simon-says.ino)
const int BTN_PINS[] = {2, 3, 4, 5};
const int LED_PINS[] = {8, 9, 10, 11};
const int NUM = 4;
const int MAX_LEN = 6;
int sequence[MAX_LEN];
int seqLen = 1;
int step = 0;
enum Phase { SHOW, INPUT };
Phase phase = SHOW;
void flash(int idx, int ms) {
  digitalWrite(LED_PINS[idx], HIGH);
  delay(ms);
  digitalWrite(LED_PINS[idx], LOW);
  delay(120);
}
void showSequence() {
  for (int i = 0; i < seqLen; i++) {
    flash(sequence[i], 350);
    delay(180);
  }
}
void failPattern() {
  for (int blink = 0; blink < 3; blink++) {
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], HIGH);
    }
    delay(200);
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], LOW);
    }
    delay(200);
  }
  seqLen = 1;
  sequence[0] = random(0, NUM);
  phase = SHOW;
}
void setup() {
  for (int i = 0; i < NUM; i++) {
    pinMode(BTN_PINS[i], INPUT_PULLUP);
    pinMode(LED_PINS[i], OUTPUT);
  }
  randomSeed(analogRead(0));
  sequence[0] = random(0, NUM);
}
void loop() {
  if (phase == SHOW) {
    showSequence();
    step = 0;
    phase = INPUT;
    return;
  }
  for (int i = 0; i < NUM; i++) {
    if (digitalRead(BTN_PINS[i]) == LOW) {
      delay(180);
      flash(i, 250);
      if (i != sequence[step]) {
        failPattern();
        return;
      }
      step++;
      if (step >= seqLen) {
        seqLen++;
        if (seqLen > MAX_LEN) {
          seqLen = MAX_LEN;
        }
        sequence[seqLen - 1] = random(0, NUM);
        phase = SHOW;
      }
      while (digitalRead(BTN_PINS[i]) == LOW) {
        delay(10);
      }
    }
  }
  delay(20);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What happens when you press the wrong button?

  • A. failPattern() resets the game
  • B. The sequence gets longer
  • C. Nothing — it ignores wrong presses
Why: Correct — wrong input fails and sequence length goes back to 1.

Code lab — try on your own

  1. Slow the show — change flash delay 350 to 500 in showSequence (inside flash call line 18).

    Hint: Line 18.

  2. Comment line 56 explaining wrong button check.

    Hint: if (i != sequence[step])

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

Watch a growing LED sequence, then repeat it with matching buttons.

Why here

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

const int BTN_PINS[] = {2, 3, 4, 5};
const int LED_PINS[] = {8, 9, 10, 11};
const int NUM = 4;
const int MAX_LEN = 6;
int sequence[MAX_LEN];
int seqLen = 1;
int step = 0;
enum Phase { SHOW, INPUT };
Phase phase = SHOW;
void flash(int idx, int ms) {
  digitalWrite(LED_PINS[idx], HIGH);
  delay(ms);
  digitalWrite(LED_PINS[idx], LOW);
  delay(120);
}
void showSequence() {
  for (int i = 0; i < seqLen; i++) {
    flash(sequence[i], 350);
    delay(180);
  }
}
void failPattern() {
  for (int blink = 0; blink < 3; blink++) {
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], HIGH);
    }
    delay(200);
    for (int i = 0; i < NUM; i++) {
      digitalWrite(LED_PINS[i], LOW);
    }
    delay(200);
  }
  seqLen = 1;
  sequence[0] = random(0, NUM);
  phase = SHOW;
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Simon Says.

Why here

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

void setup() {
  for (int i = 0; i < NUM; i++) {
    pinMode(BTN_PINS[i], INPUT_PULLUP);
    pinMode(LED_PINS[i], OUTPUT);
  }
  randomSeed(analogRead(0));
  sequence[0] = random(0, NUM);
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Simon Says.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (phase == SHOW) {
    showSequence();
    step = 0;
    phase = INPUT;
    return;
  }
  for (int i = 0; i < NUM; i++) {
    if (digitalRead(BTN_PINS[i]) == LOW) {
      delay(180);
      flash(i, 250);
      if (i != sequence[step]) {
        failPattern();
        return;
      }
      step++;
      if (step >= seqLen) {
        seqLen++;
        if (seqLen > MAX_LEN) {
          seqLen = MAX_LEN;
        }
        sequence[seqLen - 1] = random(0, NUM);
        phase = SHOW;
      }
      while (digitalRead(BTN_PINS[i]) == LOW) {
        delay(10);
      }
    }
  }
  delay(20);
}

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 Simon Says circuit ready in the simulator.

Why here

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

    pinMode(BTN_PINS[i], INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Simon Says.

Why here

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

  digitalWrite(LED_PINS[idx], HIGH);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Simon Says.

Why here

Goes in loop() so we can react when something changes.

    if (digitalRead(BTN_PINS[i]) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Simon Says.

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 Simon Says in the simulator.

Why here

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

  delay(ms);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Simon Says.

Why here

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

  sequence[0] = random(0, NUM);
Mission 19 · Stage 2

Memory Game

game state with if decisions

Remember the Serial hint and press the correct button to grow your score.

Memory Game circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button A

pin 1

Button A

pin 2

Arduino

GND

Arduino

pin 3

Button B

pin 1

Button B

pin 2

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

Score LED

anode (+)

Score LED

cathode (-)

Arduino

GND

See it

Remember and choose!

Serial hints “press A” or “press B” — pick the right button to grow your score.

Games keep state variables (score, waiting) and branch with if.

The story

The problem

Programs need memory between loop passes — not just instant reads.

Think of it like

Like remembering a card color and picking the matching pile.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Choice A in the memory game

Button A

Input

Loading part…

Choice B in the memory game

Button B

Input

Loading part…

Protects the score LED

Resistor

Safety

Loading part…

Flashes on correct answers

Score LED

Output

Loading part…

How it works

1

Pick target

pickNext() secretly chooses A or B and prints a hint.

lastChoice = random(0, 2);
2

Wait for input

Only while waiting is true do button presses count.

if (waiting && digitalRead(BTN_A) == LOW)
3

Score or reset

Correct if grows score; wrong if clears it — game state with if.

if (choice == lastChoice) score++; else score = 0;

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 Button A

    Place the Button A (btnA) on the breadboard.

    Loading part…
  3. 3

    Place Button B

    Place the Button B (btnB) on the breadboard.

    Loading part…
  4. 4

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  5. 5

    Place Score LED

    Place the Score LED (led1) on the breadboard.

    Loading part…
  6. 6

    Connect Arduino pin 2 to Button A (btnA) 1.l

    Connect Arduino pin 2 to Button A (btnA) 1.l.

  7. 7

    Connect Button A (btnA) 2.l to Arduino GND

    Connect Button A (btnA) 2.l to Arduino GND.

  8. 8

    Connect Arduino pin 3 to Button B (btnB) 1.l

    Connect Arduino pin 3 to Button B (btnB) 1.l.

  9. 9

    Connect Button B (btnB) 2.l to Arduino GND

    Connect Button B (btnB) 2.l to Arduino GND.

  10. 10

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  11. 11

    Connect Resistor (r1) 2 to Score LED (led1) anode (+)

    Connect Resistor (r1) 2 to Score LED (led1) anode (+).

  12. 12

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

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

Try it

  • Read Serial hint before pressing.
  • Correct answers stack score — wrong resets to 0.

Peek at code

Game variables

int lastChoice = 0;
int score = 0;
bool waiting = true;

lastChoice, score, and waiting remember the game between loop() calls.

check()

void check(int choice) {
  waiting = false;
  if (choice == lastChoice) {
    score++;
    digitalWrite(SCORE_LED, HIGH);
    Serial.print("Correct! Score: ");
    Serial.println(score);
  } else {
    score = 0;
    digitalWrite(SCORE_LED, LOW);
    Serial.println("Wrong — score reset");
  }
  delay(800);
  digitalWrite(SCORE_LED, LOW);
  delay(400);
  pickNext();
  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
    delay(10);
  }
}

Compares choice to lastChoice and updates score and LED feedback.

Main loop

void loop() {
  if (waiting && digitalRead(BTN_A) == LOW) {
    check(0);
  } else if (waiting && digitalRead(BTN_B) == LOW) {
    check(1);
  }
  delay(20);
}

Routes button A or B to check() only when waiting for an answer.

Show full sketch (memory-game.ino)
const int BTN_A = 2;
const int BTN_B = 3;
const int SCORE_LED = 13;
int lastChoice = 0;
int score = 0;
bool waiting = true;
void pickNext() {
  lastChoice = random(0, 2);
  waiting = true;
  Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
}
void check(int choice) {
  waiting = false;
  if (choice == lastChoice) {
    score++;
    digitalWrite(SCORE_LED, HIGH);
    Serial.print("Correct! Score: ");
    Serial.println(score);
  } else {
    score = 0;
    digitalWrite(SCORE_LED, LOW);
    Serial.println("Wrong — score reset");
  }
  delay(800);
  digitalWrite(SCORE_LED, LOW);
  delay(400);
  pickNext();
  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
    delay(10);
  }
}
void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(SCORE_LED, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Memory Game — match the hint!");
  pickNext();
}
void loop() {
  if (waiting && digitalRead(BTN_A) == LOW) {
    check(0);
  } else if (waiting && digitalRead(BTN_B) == LOW) {
    check(1);
  }
  delay(20);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What does the waiting variable control?

  • A. Whether button presses are accepted
  • B. The random seed only
  • C. LED brightness
Why: Correct — waiting gates input so only one answer counts per hint.

Code lab — try on your own

  1. Change the hint text on line 10 to include the word “Remember”.

    Hint: Edit the Serial.println in pickNext().

  2. Give more time after a wrong answer — change delay(800) to delay(1200) in check().

    Hint: Line 24.

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

Remember the Serial hint and press the correct button to grow your score.

Why here

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

const int BTN_A = 2;
const int BTN_B = 3;
const int SCORE_LED = 13;
int lastChoice = 0;
int score = 0;
bool waiting = true;
void pickNext() {
  lastChoice = random(0, 2);
  waiting = true;
  Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
}
void check(int choice) {
  waiting = false;
  if (choice == lastChoice) {
    score++;
    digitalWrite(SCORE_LED, HIGH);
    Serial.print("Correct! Score: ");
    Serial.println(score);
  } else {
    score = 0;
    digitalWrite(SCORE_LED, LOW);
    Serial.println("Wrong — score reset");
  }
  delay(800);
  digitalWrite(SCORE_LED, LOW);
  delay(400);
  pickNext();
  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {
    delay(10);
  }
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Memory Game.

Why here

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

void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(SCORE_LED, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Memory Game — match the hint!");
  pickNext();
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Memory Game.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (waiting && digitalRead(BTN_A) == LOW) {
    check(0);
  } else if (waiting && digitalRead(BTN_B) == LOW) {
    check(1);
  }
  delay(20);
}

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 Memory Game circuit ready in the simulator.

Why here

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

  pinMode(BTN_A, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Memory Game.

Why here

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

    digitalWrite(SCORE_LED, HIGH);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Memory Game.

Why here

Goes in loop() so we can react when something changes.

  while (digitalRead(BTN_A) == LOW || digitalRead(BTN_B) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Memory Game.

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 Memory Game in the simulator.

Why here

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

  delay(800);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Memory Game.

Why here

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

  lastChoice = random(0, 2);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Memory Game print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Memory Game on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

    Serial.print("Correct! Score: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Memory Game.

Why here

In loop() when each reading should appear on its own line.

  Serial.println(lastChoice == 0 ? "Hint: press A" : "Hint: press B");
Mission 20 · Stage 2

Mini Alarm

armed and disarmed if tree

Arm or disarm an alarm with a button; motion triggers the buzzer when armed.

Mini Alarm circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Arm button

pin 1

Arm button

pin 2

Arduino

GND

Motion sensor

VCC

Arduino

5V

Motion sensor

GND

Arduino

GND

Motion sensor

OUT

Arduino

pin 7

Arduino

pin 8

Buzzer

pin 1 (+)

Buzzer

pin 2 (-)

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

Status LED

anode (+)

Status LED

cathode (-)

Arduino

GND

See it

Arm it — motion sounds the alarm!

Toggle armed with a button; if armed AND motion, buzzer rings.

Home alarms combine mode switches with sensor if conditions.

The story

The problem

Real systems nest if tests — armed mode changes what motion means.

Think of it like

Like turning on a house alarm before leaving — only then does motion matter.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Toggles armed/disarmed

Arm button

Input

Loading part…

Detects movement

Motion sensor

Input

Loading part…

Sounds when armed and motion

Buzzer

Alarm

Loading part…

Protects status LED

Resistor

Safety

Loading part…

On when system is armed

Status LED

Output

Loading part…

How it works

1

Toggle armed

Button edge flips armed and the status LED shows the mode.

armed = !armed;
2

Check motion

Both must be true — armed AND motion — before alarming.

if (armed && digitalRead(PIR_PIN) == HIGH)
3

Sound or silence

Buzzer on only in armed+motion; disarmed always silent.

tone(BUZZER, 880) / noTone(BUZZER)

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 Arm button

    Place the Arm button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Motion sensor

    Place the Motion sensor (pir1) on the breadboard.

    Loading part…
  4. 4

    Place Buzzer

    Place the Buzzer (bz1) on the breadboard.

    Loading part…
  5. 5

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  6. 6

    Place Status LED

    Place the Status LED (led1) on the breadboard.

    Loading part…
  7. 7

    Connect Arduino pin 2 to Arm button (btn1) 1.l

    Connect Arduino pin 2 to Arm button (btn1) 1.l.

  8. 8

    Connect Arm button (btn1) 2.l to Arduino GND

    Connect Arm button (btn1) 2.l to Arduino GND.

  9. 9

    Connect Motion sensor (pir1) VCC to Arduino pin 5

    Connect Motion sensor (pir1) VCC to Arduino pin 5.

  10. 10

    Connect Motion sensor (pir1) GND to Arduino GND

    Connect Motion sensor (pir1) GND to Arduino GND.

  11. 11

    Connect Motion sensor (pir1) OUT to Arduino pin 7

    Connect Motion sensor (pir1) OUT to Arduino pin 7.

  12. 12

    Connect Arduino pin 8 to Buzzer (bz1) 1

    Connect Arduino pin 8 to Buzzer (bz1) 1.

  13. 13

    Connect Buzzer (bz1) 2 to Arduino GND

    Connect Buzzer (bz1) 2 to Arduino GND.

  14. 14

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  15. 15

    Connect Resistor (r1) 2 to Status LED (led1) anode (+)

    Connect Resistor (r1) 2 to Status LED (led1) anode (+).

  16. 16

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

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

Try it

  • Press button to arm — status LED on.
  • Trigger motion while armed — buzzer should sound. Disarm to stop.

Peek at code

Arm toggle

void loop() {
  bool btn = digitalRead(ARM_BTN);
  if (lastBtn == HIGH && btn == LOW) {
    armed = !armed;
    digitalWrite(STATUS_LED, armed ? HIGH : LOW);
    Serial.println(armed ? "ARMED" : "DISARMED");
    noTone(BUZZER);
  }
  lastBtn = btn;

Edge detection on the arm button flips armed and prints ARMED/DISARMED.

Nested if alarm

  if (armed && digitalRead(PIR_PIN) == HIGH) {
    tone(BUZZER, 880);
  } else {
    noTone(BUZZER);
  }
  delay(20);

armed && motion is an AND condition — classic security logic.

Show full sketch (mini-alarm.ino)
const int ARM_BTN = 2;
const int PIR_PIN = 7;
const int BUZZER = 8;
const int STATUS_LED = 13;
bool armed = false;
bool lastBtn = HIGH;
void setup() {
  pinMode(ARM_BTN, INPUT_PULLUP);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(STATUS_LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mini Alarm — press button to arm/disarm");
}
void loop() {
  bool btn = digitalRead(ARM_BTN);
  if (lastBtn == HIGH && btn == LOW) {
    armed = !armed;
    digitalWrite(STATUS_LED, armed ? HIGH : LOW);
    Serial.println(armed ? "ARMED" : "DISARMED");
    noTone(BUZZER);
  }
  lastBtn = btn;
  if (armed && digitalRead(PIR_PIN) == HIGH) {
    tone(BUZZER, 880);
  } else {
    noTone(BUZZER);
  }
  delay(20);
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. When does the buzzer sound?

  • A. When armed AND motion detected
  • B. Whenever motion happens
  • C. Only in setup()
Why: Correct — armed && PIR HIGH triggers tone().

Code lab — try on your own

  1. Change alarm pitch — use tone(BUZZER, 660) instead of 880.

    Hint: Line 25.

  2. Comment line 24 explaining armed AND motion.

    Hint: The if (armed && ...) line.

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

Arm or disarm an alarm with a button; motion triggers the buzzer when armed.

Why here

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

const int ARM_BTN = 2;
const int PIR_PIN = 7;
const int BUZZER = 8;
const int STATUS_LED = 13;
bool armed = false;
bool lastBtn = HIGH;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Mini Alarm.

Why here

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

void setup() {
  pinMode(ARM_BTN, INPUT_PULLUP);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(STATUS_LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mini Alarm — press button to arm/disarm");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Mini Alarm.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  bool btn = digitalRead(ARM_BTN);
  if (lastBtn == HIGH && btn == LOW) {
    armed = !armed;
    digitalWrite(STATUS_LED, armed ? HIGH : LOW);
    Serial.println(armed ? "ARMED" : "DISARMED");
    noTone(BUZZER);
  }
  lastBtn = btn;
  if (armed && digitalRead(PIR_PIN) == HIGH) {
    tone(BUZZER, 880);
  } else {
    noTone(BUZZER);
  }
  delay(20);
}

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 Mini Alarm circuit ready in the simulator.

Why here

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

  pinMode(ARM_BTN, INPUT_PULLUP);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in Mini Alarm.

Why here

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

    digitalWrite(STATUS_LED, armed ? HIGH : LOW);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Mini Alarm.

Why here

Goes in loop() so we can react when something changes.

  bool btn = digitalRead(ARM_BTN);

tone

Technical

Plays a beep on a buzzer pin at a chosen pitch.

In this project

Makes sounds in Mini Alarm.

Why here

Goes in loop() when you want notes or alarms.

    tone(BUZZER, 880);

noTone

Technical

Stops the buzzer sound on that pin.

In this project

Turns off the sound in Mini Alarm.

Why here

After tone() or before a pause so the buzzer is silent.

    noTone(BUZZER);

delay

Technical

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

In this project

Controls speed so you can see Mini Alarm in the simulator.

Why here

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

  delay(20);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Mini Alarm print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Mini Alarm.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Mini Alarm — press button to arm/disarm");

Stage 3: Variables

variables, counters, arithmetic

Stage 3 introduces variables — named boxes that store numbers your program can change. You will count, score, time events, and do math, building counters, stopwatches, and even a simple calculator.

Mission 21 · Stage 3

LED Counter

int variable stores a count

Use an int variable to count loop repeats and print the count on Serial.

LED Counter circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 13

Resistor

pin 2

Resistor

pin 1

LED

anode (+)

LED

cathode (-)

Arduino

GND

See it

Variables remember numbers!

loopCount grows by 1 every loop — watch Serial and the LED blink together.

Step counters, scoreboards, and page views all use variables that count up.

The story

The problem

You need the Arduino to remember a number between loop() passes.

Think of it like

Like a tally clicker — each loop adds one tick to the total.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Grow the counter

Each time loop() runs, we add 1 to loopCount — the variable remembers the total.

loopCount = loopCount + 1;
2

Show on Serial

Serial Monitor prints the current value so you can watch it climb.

Serial.print("Count: ");
Serial.println(loopCount);
3

Flash the LED

A quick blink celebrates each new count before loop() starts over.

digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
4

Wait and repeat

Pause so you can read the number, then loop() increments again.

delay(800);

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…

Try it

  • Open Serial Monitor at 9600 baud.
  • Watch Count: 1, 2, 3… and the LED blink each time.

Peek at code

Counter variable

const int LED_PIN = 13;
int loopCount = 0;

loopCount is an int — a named box that stores a whole number and keeps its value between loops.

Setup pin and Serial

  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("LED Counter — watch the count grow!");
}
void loop() {

setup() runs once: LED pin becomes an output and Serial is ready to print.

Increment and blink

  Serial.print("Count: ");
  Serial.println(loopCount);
  digitalWrite(LED_PIN, HIGH);
  delay(200);
  digitalWrite(LED_PIN, LOW);
  delay(800);
}

loop() adds 1, prints, blinks the LED, waits — then repeats forever.

Show full sketch (led-counter.ino)
const int LED_PIN = 13;
int loopCount = 0;
void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("LED Counter — watch the count grow!");
}
void loop() {
  loopCount = loopCount + 1;
  Serial.print("Count: ");
  Serial.println(loopCount);
  digitalWrite(LED_PIN, HIGH);
  delay(200);
  digitalWrite(LED_PIN, LOW);
  delay(800);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What does loopCount = loopCount + 1 do?

  • A. Adds 1 to the stored count
  • B. Resets the count to zero
  • C. Turns off the LED forever
Why: Correct — the variable grows by one every loop pass.

Code lab — try on your own

  1. Start counting from 5 — change int loopCount = 0 to int loopCount = 5.

    Hint: Line 2 at the top of the sketch.

  2. Blink faster — change delay(800) to delay(400) at the end of loop().

    Hint: Last delay in loop(), line 17.

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

Use an int variable to count loop repeats and print the count on Serial.

Why here

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

const int LED_PIN = 13;
int loopCount = 0;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for LED Counter.

Why here

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

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("LED Counter — watch the count grow!");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in LED Counter.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  loopCount = loopCount + 1;
  Serial.print("Count: ");
  Serial.println(loopCount);
  digitalWrite(LED_PIN, HIGH);
  delay(200);
  digitalWrite(LED_PIN, LOW);
  delay(800);
}

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 LED Counter 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 LED Counter.

Why here

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

  digitalWrite(LED_PIN, HIGH);

delay

Technical

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

In this project

Controls speed so you can see LED Counter in the simulator.

Why here

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

  delay(200);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets LED Counter print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from LED Counter on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

  Serial.print("Count: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for LED Counter.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("LED Counter — watch the count grow!");
Mission 22 · Stage 3

Digital Counter

display a counter value

Count 0-9 on a common-cathode 7-segment display controlled via 7 digital pins.

Digital Counter circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

7-segment display

seg A

Arduino

pin 3

7-segment display

seg B

Arduino

pin 4

7-segment display

seg C

Arduino

pin 5

7-segment display

seg D

Arduino

pin 6

7-segment display

seg E

Arduino

pin 7

7-segment display

seg F

Arduino

pin 8

7-segment display

seg G

7-segment display

COM

Arduino

GND

7-segment display

COM

Arduino

GND

See it

Light up digits 0–9!

A DIGITS[] table tells each segment ON or OFF — the display counts like a mini scoreboard.

Microwave timers, elevator floors, and digital clocks use 7-segment displays.

The story

The problem

Showing a number needs seven tiny lights controlled together — not one LED.

Think of it like

Like drawing numbers with seven stick pieces — each digit has its own pattern.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Shows digits 0 through 9

7-segment display

Display

Loading part…

How it works

1

Prepare segment pins

Pins 2–8 become outputs — one wire per segment A through G.

pinMode(SEG_A + i, OUTPUT);
2

Loop through digits

The for loop tries 0, then 1, then 2… up to 9 — a counter variable built into the loop.

for (int digit = 0; digit <= 9; digit++)
3

Light the pattern

DIGITS[digit] holds a bitmask; showDigit() turns each segment ON or OFF.

showDigit(digit);
4

Hold and repeat

Each digit stays visible for 800 ms, then the for loop shows the next one.

delay(800);

Then loop back to step 2

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 7-segment display

    Place the 7-segment display (seg1) on the breadboard.

    Loading part…
  3. 3

    Connect Arduino pin 2 to 7-segment display (seg1) anode (+)

    Connect Arduino pin 2 to 7-segment display (seg1) anode (+).

  4. 4

    Connect Arduino pin 3 to 7-segment display (seg1) B

    Connect Arduino pin 3 to 7-segment display (seg1) B.

  5. 5

    Connect Arduino pin 4 to 7-segment display (seg1) cathode (-)

    Connect Arduino pin 4 to 7-segment display (seg1) cathode (-).

  6. 6

    Connect Arduino pin 5 to 7-segment display (seg1) D

    Connect Arduino pin 5 to 7-segment display (seg1) D.

  7. 7

    Connect Arduino pin 6 to 7-segment display (seg1) E

    Connect Arduino pin 6 to 7-segment display (seg1) E.

  8. 8

    Connect Arduino pin 7 to 7-segment display (seg1) F

    Connect Arduino pin 7 to 7-segment display (seg1) F.

  9. 9

    Connect Arduino pin 8 to 7-segment display (seg1) G

    Connect Arduino pin 8 to 7-segment display (seg1) G.

  10. 10

    Connect 7-segment display (seg1) COM.2 to Arduino GND

    Connect 7-segment display (seg1) COM.2 to Arduino GND.

  11. 11

    Connect 7-segment display (seg1) COM.1 to Arduino GND

    Connect 7-segment display (seg1) COM.1 to Arduino GND.

Try it

  • Press Run — watch the display count 0, 1, 2… 9.
  • Serial Monitor prints each digit too!

Peek at code

Segment patterns table

const uint8_t DIGITS[10] = {
  0b0111111,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
  0b0000111,
  0b1111111,
  0b1101111,
};

DIGITS[] stores which segments glow for 0–9 — binary patterns like 0b0111111 for zero.

showDigit() helper

void showDigit(int digit) {
  uint8_t pattern = DIGITS[digit];
  for (int seg = 0; seg < 7; seg++) {
    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);
  }
}

Loops seven segments and uses bit math to set each pin HIGH or LOW.

Count 0 to 9

void loop() {
  for (int digit = 0; digit <= 9; digit++) {
    showDigit(digit);
    Serial.println(digit);
    delay(800);
  }
}

loop() runs the for loop forever — display cycles through every digit.

Show full sketch (7segment.ino)
const int SEG_A = 2;
const uint8_t DIGITS[10] = {
  0b0111111,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
  0b0000111,
  0b1111111,
  0b1101111,
};
void showDigit(int digit) {
  uint8_t pattern = DIGITS[digit];
  for (int seg = 0; seg < 7; seg++) {
    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);
  }
}
void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(SEG_A + i, OUTPUT);
    digitalWrite(SEG_A + i, LOW);
  }
  Serial.begin(9600);
  Serial.println("7-Segment Counter Demo");
}
void loop() {
  for (int digit = 0; digit <= 9; digit++) {
    showDigit(digit);
    Serial.println(digit);
    delay(800);
  }
}

Quick quiz

Q1. Where does repeating work belong?

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

Q2. What does the for loop variable digit do?

  • A. Counts 0 through 9 to pick which number to show
  • B. Turns off Serial Monitor
  • C. Only runs in setup()
Why: Correct — digit is the counter that picks which pattern to display.

Code lab — try on your own

  1. Show each digit longer — change delay(800) to delay(1200) in loop().

    Hint: Line 32 inside the for loop.

  2. Add a comment on the pattern line explaining it comes from DIGITS[].

    Hint: Line 15 inside showDigit().

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

Count 0-9 on a common-cathode 7-segment display controlled via 7 digital pins.

Why here

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

const int SEG_A = 2;
const uint8_t DIGITS[10] = {
  0b0111111,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
  0b0000111,
  0b1111111,
  0b1101111,
};
void showDigit(int digit) {
  uint8_t pattern = DIGITS[digit];
  for (int seg = 0; seg < 7; seg++) {
    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);
  }
}

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for 7-Segment Display.

Why here

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

void setup() {
  for (int i = 0; i < 7; i++) {
    pinMode(SEG_A + i, OUTPUT);
    digitalWrite(SEG_A + i, LOW);
  }
  Serial.begin(9600);
  Serial.println("7-Segment Counter Demo");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in 7-Segment Display.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  for (int digit = 0; digit <= 9; digit++) {
    showDigit(digit);
    Serial.println(digit);
    delay(800);
  }
}

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 7-Segment Display circuit ready in the simulator.

Why here

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

    pinMode(SEG_A + i, OUTPUT);

digitalWrite

Technical

Turns a pin ON or OFF.

In this project

Controls lights, motors, or buzzers in 7-Segment Display.

Why here

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

    digitalWrite(SEG_A + seg, (pattern >> seg) & 1);

delay

Technical

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

In this project

Controls speed so you can see 7-Segment Display in the simulator.

Why here

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

    delay(800);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets 7-Segment Display print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for 7-Segment Display.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("7-Segment Counter Demo");
Mission 23 · Stage 3

Stopwatch

millis() arithmetic for elapsed time

Store elapsed time in variables using millis() and print seconds on Serial.

Stopwatch circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

See it

Start and stop a real timer!

Press the button to toggle running — millis() and variables track elapsed seconds.

Sports timers and kitchen timers store start time and compute elapsed time the same way.

The story

The problem

You need to remember when the timer started and whether it is running.

Think of it like

Like pressing start on a phone stopwatch — it remembers the start moment for you.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Detect button press

When you press, we flip the running variable and record or print time.

if (digitalRead(BUTTON_PIN) == LOW)
2

Toggle running state

running is a bool variable — true means counting, false means stopped. startMs stores when we began.

running = !running;
if (running) startMs = millis();
3

Show live elapsed time

While running, subtract startMs from now to get seconds — updates every half second.

unsigned long elapsed = millis() - startMs;
Serial.print(elapsed / 1000.0, 2);

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…

Try it

  • Press once to start — Serial shows counting seconds.
  • Press again to stop and see your final time.

Peek at code

Timer variables

const int BUTTON_PIN = 2;
unsigned long startMs = 0;
bool running = false;

startMs remembers the start moment; running tells us if the clock is ticking.

Start / stop on press

    if (running) {
      startMs = millis();
      Serial.println("Started!");
    } else {
      unsigned long elapsed = millis() - startMs;
      Serial.print("Stopped at ");
      Serial.print(elapsed / 1000.0, 2);
      Serial.println(" seconds");
    }
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
  }
  if (running) {

Button press toggles running and either saves startMs or prints final elapsed time.

Live updates

    Serial.print("Time: ");
    Serial.print(elapsed / 1000.0, 2);
    Serial.println(" s");
    delay(500);
  }
  delay(20);
}

When running is true, loop() keeps printing the growing elapsed time.

Show full sketch (stopwatch.ino)
const int BUTTON_PIN = 2;
unsigned long startMs = 0;
bool running = false;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Stopwatch — press button to start/stop");
}
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    running = !running;
    if (running) {
      startMs = millis();
      Serial.println("Started!");
    } else {
      unsigned long elapsed = millis() - startMs;
      Serial.print("Stopped at ");
      Serial.print(elapsed / 1000.0, 2);
      Serial.println(" seconds");
    }
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
  }
  if (running) {
    unsigned long elapsed = millis() - startMs;
    Serial.print("Time: ");
    Serial.print(elapsed / 1000.0, 2);
    Serial.println(" s");
    delay(500);
  }
  delay(20);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What does startMs = millis() save?

  • A. The moment the stopwatch started
  • B. The final score
  • C. The button pin number
Why: Correct — startMs is the timestamp when running turned on.

Code lab — try on your own

  1. Print time more often — change delay(500) to delay(250) in the running block.

    Hint: Line 32 inside if (running).

  2. Add a comment on running = !running explaining it flips start/stop.

    Hint: Line 13.

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

Store elapsed time in variables using millis() and print seconds on Serial.

Why here

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

const int BUTTON_PIN = 2;
unsigned long startMs = 0;
bool running = false;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Stopwatch.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Stopwatch — press button to start/stop");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Stopwatch.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    running = !running;
    if (running) {
      startMs = millis();
      Serial.println("Started!");
    } else {
      unsigned long elapsed = millis() - startMs;
      Serial.print("Stopped at ");
      Serial.print(elapsed / 1000.0, 2);
      Serial.println(" seconds");
    }
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
  }
  if (running) {
    unsigned long elapsed = millis() - startMs;
    Serial.print("Time: ");
    Serial.print(elapsed / 1000.0, 2);
    Serial.println(" s");
    delay(500);
  }
  delay(20);
}

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 Stopwatch circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Stopwatch.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(BUTTON_PIN) == LOW) {

delay

Technical

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

In this project

Controls speed so you can see Stopwatch in the simulator.

Why here

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

      delay(20);

millis

Technical

Counts milliseconds since the board started.

In this project

Tracks time in Stopwatch without using delay().

Why here

In loop() when you need to know how much time passed.

      startMs = millis();

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Stopwatch print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Stopwatch on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

      Serial.print("Stopped at ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Stopwatch.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Stopwatch — press button to start/stop");
Mission 24 · Stage 3

Countdown Timer

decrement a variable over time

Count down from 10 to 0 using an int variable and Serial output.

Countdown Timer circuit diagram

See it

T-minus… Liftoff!

secondsLeft counts down from 10 — each second prints until blastoff.

Rocket launches, game timers, and oven timers all count down a variable to zero.

The story

The problem

You need a number that shrinks over time until something exciting happens.

Think of it like

Like counting backwards before hide-and-seek — 10, 9, 8… then GO!

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Show current count

Every loop pass prints the value stored in secondsLeft.

Serial.print("T-minus ");
Serial.println(secondsLeft);
2

Check for liftoff

When the variable hits zero, we celebrate and stop counting down.

if (secondsLeft <= 0) {
  Serial.println("Liftoff!");
}
3

Subtract one second

After waiting one real second, the variable drops by 1 — then loop() prints again.

secondsLeft = secondsLeft - 1;
delay(1000);

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…

Try it

  • Open Serial Monitor and watch T-minus 10, 9, 8…
  • Wait for Liftoff! at the end.

Peek at code

Countdown variable

int secondsLeft = 10;

secondsLeft starts at 10 — an int variable that will shrink each second.

Liftoff check

    while (true) {
      delay(1000);
    }
  }
  secondsLeft = secondsLeft - 1;

When secondsLeft reaches 0, print Liftoff! and stay in an endless wait.

Decrement each second

}

Subtract 1 from the variable, then delay(1000) for one real second.

Show full sketch (countdown-timer.ino)
int secondsLeft = 10;
void setup() {
  Serial.begin(9600);
  Serial.println("Countdown starting...");
}
void loop() {
  Serial.print("T-minus ");
  Serial.println(secondsLeft);
  if (secondsLeft <= 0) {
    Serial.println("Liftoff!");
    while (true) {
      delay(1000);
    }
  }
  secondsLeft = secondsLeft - 1;
  delay(1000);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What happens when secondsLeft reaches 0?

  • A. Serial prints Liftoff! and stops counting
  • B. The variable resets to 10
  • C. setup() runs again
Why: Correct — countdown ends with Liftoff! when secondsLeft <= 0.

Code lab — try on your own

  1. Start from 15 — change int secondsLeft = 10 to int secondsLeft = 15.

    Hint: Line 1 at the top.

  2. Count down faster — change delay(1000) to delay(500) at the end of loop().

    Hint: Line 18.

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

Count down from 10 to 0 using an int variable and Serial output.

Why here

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

int secondsLeft = 10;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Countdown Timer.

Why here

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

void setup() {
  Serial.begin(9600);
  Serial.println("Countdown starting...");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Countdown Timer.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  Serial.print("T-minus ");
  Serial.println(secondsLeft);
  if (secondsLeft <= 0) {
    Serial.println("Liftoff!");
    while (true) {
      delay(1000);
    }
  }
  secondsLeft = secondsLeft - 1;
  delay(1000);
}

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

delay

Technical

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

In this project

Controls speed so you can see Countdown Timer in the simulator.

Why here

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

      delay(1000);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Countdown Timer print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Countdown Timer on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

  Serial.print("T-minus ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Countdown Timer.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Countdown starting...");
Mission 25 · Stage 3

Score Keeper

increment with ++ and --

Increment a score variable with a button press and print on Serial.

Score Keeper circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

See it

Press to add points!

Every button press adds 1 to score — your first variable that grows from input.

Arcade games, quiz apps, and sports scoreboards increment a score variable on each event.

The story

The problem

The program must remember how many points you have earned so far.

Think of it like

Like putting a marble in a jar each time you score — the jar holds the total.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Watch for button

Only when you press does the score change — no press means no change.

if (digitalRead(BUTTON_PIN) == LOW)
2

Add a point

Read the old score, add 1, store it back — the variable remembers between presses.

score = score + 1;
3

Show new total

Print the updated score, wait for release, then loop() listens again.

Serial.print("Score: ");
Serial.println(score);

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…

Try it

  • Tap the button — Score: 1, 2, 3… on Serial.
  • Each press adds exactly one point.

Peek at code

Score variable

const int BUTTON_PIN = 2;
int score = 0;

score starts at 0 — an int that grows each time you press the button.

Button setup

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Score Keeper — press to add a point");
}
void loop() {

INPUT_PULLUP on the button pin; Serial is ready to print scores.

Increment on press

    score = score + 1;
    Serial.print("Score: ");
    Serial.println(score);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(150);
  }
  delay(20);
}

Press detected → add 1 → print → debounce → loop() waits for next press.

Show full sketch (score-keeper.ino)
const int BUTTON_PIN = 2;
int score = 0;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Score Keeper — press to add a point");
}
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    score = score + 1;
    Serial.print("Score: ");
    Serial.println(score);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(150);
  }
  delay(20);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. Why store the score in a variable instead of just printing 1 each time?

  • A. So the total grows and is remembered between presses
  • B. Variables make the LED brighter
  • C. Serial only works with variables
Why: Correct — the variable remembers the total across loop() passes.

Code lab — try on your own

  1. Start at 10 points — change int score = 0 to int score = 10.

    Hint: Line 2.

  2. Add a comment on the score = score + 1 line explaining “add one point”.

    Hint: Line 12 inside the if block.

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

Increment a score variable with a button press and print on Serial.

Why here

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

const int BUTTON_PIN = 2;
int score = 0;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Score Keeper.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Score Keeper — press to add a point");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Score Keeper.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    score = score + 1;
    Serial.print("Score: ");
    Serial.println(score);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(150);
  }
  delay(20);
}

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 Score Keeper circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Score Keeper.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(BUTTON_PIN) == LOW) {

delay

Technical

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

In this project

Controls speed so you can see Score Keeper in the simulator.

Why here

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

      delay(20);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Score Keeper print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Score Keeper on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

    Serial.print("Score: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Score Keeper.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Score Keeper — press to add a point");
Mission 26 · Stage 3

Lap Counter

multiple counter variables

Track total presses and laps with two int variables.

Lap Counter circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

Pushbutton

pin 1

Arduino

pin 3

Pushbutton

pin 2

Arduino

GND

See it

Two counters, one race!

lapNumber and totalPresses are separate variables — green adds a lap, red resets both.

Running apps track lap number and total steps with different variables.

The story

The problem

Sometimes you need more than one number — lap count and total presses are different ideas.

Think of it like

Like lap boards at a track: one shows “lap 3” and another shows “30 total steps”.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Check reset button

Red button clears both variables back to zero — a fresh start.

if (digitalRead(RESET_BUTTON) == LOW) {
  totalPresses = 0;
  lapNumber = 0;
}
2

Check lap button

Green button means record another lap.

if (digitalRead(LAP_BUTTON) == LOW)
3

Increment both counters

Both variables grow by 1 — lapNumber is the lap, totalPresses counts every press.

totalPresses = totalPresses + 1;
lapNumber = lapNumber + 1;
4

Print and wait

Show both numbers on Serial, then loop() checks buttons again.

Serial.print("Lap ");
Serial.println(totalPresses);

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…

Try it

  • Green button = new lap. Red button = reset both counters.
  • Watch Serial for Lap 1 | Total presses: 1

Peek at code

Two counter variables

const int LAP_BUTTON = 2;
const int RESET_BUTTON = 3;
int totalPresses = 0;
int lapNumber = 0;

totalPresses and lapNumber are separate int boxes — reset clears both.

Reset handler

    lapNumber = 0;
    Serial.println("Counters reset!");
    while (digitalRead(RESET_BUTTON) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(LAP_BUTTON) == LOW) {

Red button sets both variables to 0 and prints Counters reset!

Lap handler

    lapNumber = lapNumber + 1;
    Serial.print("Lap ");
    Serial.print(lapNumber);
    Serial.print(" | Total presses: ");
    Serial.println(totalPresses);
    while (digitalRead(LAP_BUTTON) == LOW) {
      delay(20);
    }
  }
  delay(20);

Green button adds 1 to each variable and prints Lap N | Total presses: N.

Show full sketch (lap-counter.ino)
const int LAP_BUTTON = 2;
const int RESET_BUTTON = 3;
int totalPresses = 0;
int lapNumber = 0;
void setup() {
  pinMode(LAP_BUTTON, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Lap Counter — green = lap, red = reset");
}
void loop() {
  if (digitalRead(RESET_BUTTON) == LOW) {
    totalPresses = 0;
    lapNumber = 0;
    Serial.println("Counters reset!");
    while (digitalRead(RESET_BUTTON) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(LAP_BUTTON) == LOW) {
    totalPresses = totalPresses + 1;
    lapNumber = lapNumber + 1;
    Serial.print("Lap ");
    Serial.print(lapNumber);
    Serial.print(" | Total presses: ");
    Serial.println(totalPresses);
    while (digitalRead(LAP_BUTTON) == LOW) {
      delay(20);
    }
  }
  delay(20);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What happens when you press the red reset button?

  • A. Both totalPresses and lapNumber go back to 0
  • B. Only lapNumber resets
  • C. The Arduino restarts
Why: Correct — red button clears totalPresses and lapNumber together.

Code lab — try on your own

  1. Start at lap 3 — change int lapNumber = 0 to int lapNumber = 3.

    Hint: Line 4.

  2. Add a comment on lapNumber = lapNumber + 1 explaining “next lap”.

    Hint: Line 24.

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

Track total presses and laps with two int variables.

Why here

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

const int LAP_BUTTON = 2;
const int RESET_BUTTON = 3;
int totalPresses = 0;
int lapNumber = 0;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Lap Counter.

Why here

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

void setup() {
  pinMode(LAP_BUTTON, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Lap Counter — green = lap, red = reset");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Lap Counter.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(RESET_BUTTON) == LOW) {
    totalPresses = 0;
    lapNumber = 0;
    Serial.println("Counters reset!");
    while (digitalRead(RESET_BUTTON) == LOW) {
      delay(20);
    }
  }
  if (digitalRead(LAP_BUTTON) == LOW) {
    totalPresses = totalPresses + 1;
    lapNumber = lapNumber + 1;
    Serial.print("Lap ");
    Serial.print(lapNumber);
    Serial.print(" | Total presses: ");
    Serial.println(totalPresses);
    while (digitalRead(LAP_BUTTON) == LOW) {
      delay(20);
    }
  }
  delay(20);
}

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 Lap Counter circuit ready in the simulator.

Why here

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

  pinMode(LAP_BUTTON, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Lap Counter.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(RESET_BUTTON) == LOW) {

delay

Technical

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

In this project

Controls speed so you can see Lap Counter in the simulator.

Why here

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

      delay(20);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Lap Counter print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Lap Counter on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

    Serial.print("Lap ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Lap Counter.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Lap Counter — green = lap, red = reset");
Mission 27 · Stage 3

Visitor Counter

count events from a sensor

Count visitors with a button; store the total in a variable.

Visitor Counter circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

See it

Count every visitor!

Each button press adds 1 to visitors — like a shop door counter.

Stores count foot traffic; museums track daily visitors with the same idea.

The story

The problem

You need a running total that only goes up when someone arrives.

Think of it like

Like clicking a counter each time someone walks through the door.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Detect arrival

Button stands in for a door sensor — press simulates one visitor.

if (digitalRead(SENSOR_PIN) == LOW)
2

Add one visitor

++ is shorthand for visitors = visitors + 1 — same idea, shorter code.

visitors++;
3

Show daily total

Print the running total, debounce, then loop() waits for the next visitor.

Serial.print("Visitors today: ");
Serial.println(visitors);

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…

Try it

  • Each button press adds one visitor.
  • Serial shows Visitors today: 1, 2, 3…

Peek at code

Visitor total variable

const int SENSOR_PIN = 2;
int visitors = 0;

visitors starts at 0 and only increases — it remembers all arrivals today.

Sensor setup

  pinMode(SENSOR_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Visitor counter ready");
}
void loop() {

Pin 2 is INPUT_PULLUP — ready to detect each new visitor.

Increment on arrival

    visitors++;
    Serial.print("Visitors today: ");
    Serial.println(visitors);
    while (digitalRead(SENSOR_PIN) == LOW) {
      delay(30);
    }
    delay(200);
  }
  delay(20);
}

Press → visitors++ → print → wait for release → loop() again.

Show full sketch (visitor-counter.ino)
const int SENSOR_PIN = 2;
int visitors = 0;
void setup() {
  pinMode(SENSOR_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Visitor counter ready");
}
void loop() {
  if (digitalRead(SENSOR_PIN) == LOW) {
    visitors++;
    Serial.print("Visitors today: ");
    Serial.println(visitors);
    while (digitalRead(SENSOR_PIN) == LOW) {
      delay(30);
    }
    delay(200);
  }
  delay(20);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What does visitors++ do?

  • A. Adds 1 to the visitors variable
  • B. Resets visitors to zero
  • C. Turns off the sensor
Why: Correct — visitors++ increments the stored total by one.

Code lab — try on your own

  1. Start with 5 visitors already — change int visitors = 0 to int visitors = 5.

    Hint: Line 2.

  2. Wait longer between counts — change delay(200) to delay(400) after a visitor.

    Hint: Line 18 inside the if block.

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

Count visitors with a button; store the total in a variable.

Why here

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

const int SENSOR_PIN = 2;
int visitors = 0;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Visitor Counter.

Why here

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

void setup() {
  pinMode(SENSOR_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  Serial.println("Visitor counter ready");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Visitor Counter.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(SENSOR_PIN) == LOW) {
    visitors++;
    Serial.print("Visitors today: ");
    Serial.println(visitors);
    while (digitalRead(SENSOR_PIN) == LOW) {
      delay(30);
    }
    delay(200);
  }
  delay(20);
}

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 Visitor Counter circuit ready in the simulator.

Why here

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

  pinMode(SENSOR_PIN, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Visitor Counter.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(SENSOR_PIN) == LOW) {

delay

Technical

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

In this project

Controls speed so you can see Visitor Counter in the simulator.

Why here

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

      delay(30);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Visitor Counter print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Visitor Counter on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

    Serial.print("Visitors today: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Visitor Counter.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Visitor counter ready");
Mission 28 · Stage 3

Random Number Generator

random() within a range

Pick random numbers in a range with random() and store in a variable.

Random Number Generator circuit diagram

See it

Surprise numbers every time!

random() picks a new value between minValue and maxValue — stored in pick each loop.

Games, lotteries, and simulations use random() so outcomes are unpredictable.

The story

The problem

Fixed numbers are boring — you want a fresh surprise on every print.

Think of it like

Like drawing a card from a shuffled deck — you cannot guess the next one.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Seed the generator

setup() uses noisy analog pin 0 so each run gets different random numbers.

randomSeed(analogRead(0));
2

Pick a random value

pick is a new variable each loop — random() uses minValue and maxValue as bounds.

int pick = random(minValue, maxValue + 1);
3

Print and wait

Show the surprise number, wait 1.5 seconds, then pick again.

Serial.println(pick);
delay(1500);

Then loop back to step 2

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…

Try it

  • Watch Serial — Random: changes every 1.5 seconds.
  • Try changing minValue and maxValue in Code lab!

Peek at code

Range variables

int minValue = 1;
int maxValue = 100;

minValue and maxValue define the range — change them to widen or narrow picks.

Seed in setup()

  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Random generator 1–100");
}

randomSeed() in setup() makes each power-on produce a different sequence.

Pick and print

  Serial.print("Random: ");
  Serial.println(pick);
  delay(1500);
}

loop() creates pick, prints Random: N, waits, repeats.

Show full sketch (random-generator.ino)
int minValue = 1;
int maxValue = 100;
void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Random generator 1–100");
}
void loop() {
  int pick = random(minValue, maxValue + 1);
  Serial.print("Random: ");
  Serial.println(pick);
  delay(1500);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. Why call randomSeed(analogRead(0)) in setup()?

  • A. So each run produces a different sequence of random numbers
  • B. To make random() always return 42
  • C. To turn on Serial Monitor
Why: Correct — analog noise gives a different seed each time you upload or reset.

Code lab — try on your own

  1. Pick smaller numbers — change int maxValue = 100 to int maxValue = 50.

    Hint: Line 2.

  2. Print new numbers faster — change delay(1500) to delay(800).

    Hint: Line 14 in loop().

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

Pick random numbers in a range with random() and store in a variable.

Why here

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

int minValue = 1;
int maxValue = 100;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Random Number Generator.

Why here

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

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Random generator 1–100");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Random Number Generator.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  int pick = random(minValue, maxValue + 1);
  Serial.print("Random: ");
  Serial.println(pick);
  delay(1500);
}

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

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Random Number Generator.

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 Random Number Generator in the simulator.

Why here

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

  delay(1500);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Random Number Generator.

Why here

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

  int pick = random(minValue, maxValue + 1);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Random Number Generator print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Random Number Generator on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

  Serial.print("Random: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Random Number Generator.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Random generator 1–100");
Mission 29 · Stage 3

Dice Simulator

variables plus random for dice

Roll a die 1–6 with random() stored in a variable when you press a button.

Dice Simulator circuit diagram

Pin connections

Part 1Part 2

Pushbutton

pin 1

Arduino

pin 2

Pushbutton

pin 2

Arduino

GND

See it

Roll the dice!

Press the button — lastRoll stores random(1, 7) for a fresh 1–6 each time.

Board games and apps store the latest roll in a variable before showing it.

The story

The problem

You want a die face that changes only when the player asks to roll.

Think of it like

Like shaking a dice cup — the number stays until you roll again.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Wait for roll button

Nothing changes until you press — lastRoll keeps the previous value.

if (digitalRead(BUTTON_PIN) == LOW)
2

Pick random die face

random(1, 7) gives 1 through 6 — stored in lastRoll until the next roll.

lastRoll = random(1, 7);
3

Show result

Print the stored roll, debounce the button, then loop() waits again.

Serial.print("You rolled: ");
Serial.println(lastRoll);

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…

Try it

  • Press the button — You rolled: 1–6 on Serial.
  • Press again for a new random face!

Peek at code

lastRoll variable

const int BUTTON_PIN = 2;
int lastRoll = 1;

lastRoll remembers the most recent die face between button presses.

Setup and seed

  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Dice — press button to roll");
}

Button pin, Serial, and randomSeed() prepare for fair rolls.

Roll on press

    lastRoll = random(1, 7);
    Serial.print("You rolled: ");
    Serial.println(lastRoll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(200);
  }
  delay(20);
}

Press → random roll → store in lastRoll → print → wait for next press.

Show full sketch (dice-simulator.ino)
const int BUTTON_PIN = 2;
int lastRoll = 1;
void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Dice — press button to roll");
}
void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    lastRoll = random(1, 7);
    Serial.print("You rolled: ");
    Serial.println(lastRoll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(200);
  }
  delay(20);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. What does random(1, 7) return?

  • A. 1 to 6 inclusive
  • B. 1 to 7 inclusive
  • C. Always 1
Why: Correct — you get 1, 2, 3, 4, 5, or 6 — perfect for a die.

Code lab — try on your own

  1. Pretend last roll was 6 — change int lastRoll = 1 to int lastRoll = 6.

    Hint: Line 2.

  2. Add a comment on lastRoll = random(1, 7) explaining “new die face”.

    Hint: Line 13 inside the if block.

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

Roll a die 1–6 with random() stored in a variable when you press a button.

Why here

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

const int BUTTON_PIN = 2;
int lastRoll = 1;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Dice Simulator.

Why here

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

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Dice — press button to roll");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Dice Simulator.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  if (digitalRead(BUTTON_PIN) == LOW) {
    lastRoll = random(1, 7);
    Serial.print("You rolled: ");
    Serial.println(lastRoll);
    while (digitalRead(BUTTON_PIN) == LOW) {
      delay(20);
    }
    delay(200);
  }
  delay(20);
}

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 Dice Simulator circuit ready in the simulator.

Why here

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

  pinMode(BUTTON_PIN, INPUT_PULLUP);

digitalRead

Technical

Checks if a pin is ON or OFF.

In this project

Reads buttons or sensors in Dice Simulator.

Why here

Goes in loop() so we can react when something changes.

  if (digitalRead(BUTTON_PIN) == LOW) {

analogRead

Technical

Reads a sensor number from 0 to 1023.

In this project

Turns a sensor signal into a number for Dice Simulator.

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 Dice Simulator in the simulator.

Why here

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

      delay(20);

random

Technical

Picks a random number in a range.

In this project

Adds randomness in Dice Simulator.

Why here

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

    lastRoll = random(1, 7);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Dice Simulator print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Dice Simulator on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

    Serial.print("You rolled: ");

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Dice Simulator.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Dice — press button to roll");
Mission 30 · Stage 3

Simple Calculator

arithmetic expressions with variables

Add two int variables and print the sum on Serial.

Simple Calculator circuit diagram

See it

Variables do math!

numberA and numberB are stored values — sum = numberA + numberB prints the answer.

Every calculator app stores numbers in variables before adding, subtracting, or multiplying.

The story

The problem

You want to change two numbers and see the result update automatically.

Think of it like

Like two labeled jars of marbles — pour them together and count the total.

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

How it works

1

Two input variables

numberA and numberB hold the two numbers you want to add — change them in Code lab.

int numberA = 12;
int numberB = 8;
2

Compute the sum

sum is a third variable that stores the result of the addition.

sum = numberA + numberB;
3

Print the equation

Shows 12 + 8 = 20 on Serial, waits 2 seconds, then loop() recalculates.

Serial.print(numberA);
Serial.print(" + ");
Serial.println(sum);

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…

Try it

  • Serial shows 12 + 8 = 20 every 2 seconds.
  • Change numberA or numberB in Code lab and re-run!

Peek at code

Input variables

int numberA = 12;
int numberB = 8;
int sum = 0;

numberA, numberB, and sum are three int boxes — A and B are inputs, sum holds the answer.

Addition in loop()

  Serial.print(numberA);
  Serial.print(" + ");
  Serial.print(numberB);
  Serial.print(" = ");
  Serial.println(sum);
  delay(2000);
}

Each loop pass recalculates sum and prints the full equation on Serial.

Show full sketch (simple-calculator.ino)
int numberA = 12;
int numberB = 8;
int sum = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("Simple Calculator — change A and B in Code lab");
}
void loop() {
  sum = numberA + numberB;
  Serial.print(numberA);
  Serial.print(" + ");
  Serial.print(numberB);
  Serial.print(" = ");
  Serial.println(sum);
  delay(2000);
}

Quick quiz

Q1. What is a variable?

  • A. A named place to store a value
  • B. A type of wire
  • C. Only delay()
Why: Correct — int score = 0; creates a box named score.

Q2. Why use a sum variable instead of only printing numberA + numberB once?

  • A. So the result is stored and can be reused or printed clearly
  • B. Variables cannot do addition
  • C. Serial only prints variables named sum
Why: Correct — sum stores the computed result in a named place.

Code lab — try on your own

  1. Try different math — change int numberA = 12 to int numberA = 20.

    Hint: Line 1.

  2. Change the second number — set int numberB = 8 to int numberB = 15.

    Hint: Line 2.

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

Add two int variables and print the sum on Serial.

Why here

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

int numberA = 12;
int numberB = 8;
int sum = 0;

setup()

Technical

Runs one time when the board turns on.

In this project

Sets up pins and libraries for Simple Calculator.

Why here

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

void setup() {
  Serial.begin(9600);
  Serial.println("Simple Calculator — change A and B in Code lab");
}

loop()

Technical

Runs again and again after setup() is done.

In this project

This is the main action you see in Simple Calculator.

Why here

Repeating work (blink, read sensors) goes here.

void loop() {
  sum = numberA + numberB;
  Serial.print(numberA);
  Serial.print(" + ");
  Serial.print(numberB);
  Serial.print(" = ");
  Serial.println(sum);
  delay(2000);
}

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

delay

Technical

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

In this project

Controls speed so you can see Simple Calculator in the simulator.

Why here

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

  delay(2000);

begin

Technical

Starts talking to the computer screen (serial monitor).

In this project

Lets Simple Calculator print debug messages.

Why here

Goes in setup() once before any Serial.print.

  Serial.begin(9600);

print

Technical

Sends text to the serial monitor without a new line.

In this project

Shows values from Simple Calculator on the screen.

Why here

In loop() or setup() when you want to see what the board is doing.

  Serial.print(numberA);

println

Technical

Sends text to the serial monitor and starts a new line.

In this project

Prints one line of output for Simple Calculator.

Why here

In loop() when each reading should appear on its own line.

  Serial.println("Simple Calculator — change A and B in Code lab");