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!");