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

Watch a number grow!

A little box called loopCount adds 1 every time — and the light blinks for each count!

Step counters, scoreboards, and video game points all count up just like this.

The story

The problem

We want the Arduino to remember a number and keep adding to it, even as the loop runs over and over.

Think of it like

It's like a clicker at a door — every click adds one more to the total.

Meet the parts

It counts the numbers and blinks the light.

Arduino

The brain

Loading part…

How it works

1

Add one more

Every time loop() runs, we add 1 to loopCount. The box remembers the new total.

loopCount = loopCount + 1;
2

Show it on the screen

The screen prints the count so you can watch it go up: 1, 2, 3…

Serial.print("Count: ");
Serial.println(loopCount);
3

Blink the light

A quick blink celebrates each new count before we start over.

digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
4

Wait, then do it again

Pause so you can read the number, then loop() adds one more.

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 — ready to count!

    Loading part…

Try it

  • Press Run and open the message screen — watch Count: 1, 2, 3…
  • The light blinks once for every new number!

Peek at code

The counting box

const int LED_PIN = 13;
int loopCount = 0;

loopCount is a box that holds a whole number. It keeps its value and grows each loop.

Get ready (setup)

  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("LED Counter — watch the count grow!");
}
void loop() {

setup() runs once: it gets pin 13 ready for the light and opens the message screen.

Count and blink (loop)

  Serial.print("Count: ");
  Serial.println(loopCount);
  digitalWrite(LED_PIN, HIGH);
  delay(200);
  digitalWrite(LED_PIN, LOW);
  delay(800);
}

loop() adds 1, shows the count, blinks the light, waits — then does it all again!

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 box that stores a number
  • B. A kind of wire
  • C. Just a wait
Why: Yes! int loopCount = 0; makes a box named loopCount.

Q2. What does loopCount = loopCount + 1 do?

  • A. Adds 1 to the number in the box
  • B. Sets the count back to zero
  • C. Turns off the light forever
Why: Yes! The box grows by one every time the loop runs.

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.

  2. Make it count faster — change delay(800) to delay(400) at the end of loop().

    Hint: The last delay in loop(), line 15.

Code walkthrough

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

Program overview

Big idea

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

In this project

We keep a number in a box, add 1 each time, show it on the screen, and blink a light.

Tip

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

const int LED_PIN = 13;
int loopCount = 0;

setup()

Big idea

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

In this project

It gets pin 13 ready for the light and opens the message screen.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("LED Counter — watch the count grow!");
}

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where the number grows, the screen shows it, and the light blinks.

Why here

Things that repeat go inside loop().

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 a number inside loop(), then press Run to see what happens.

pinMode

Big idea

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

In this project

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

Why here

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

  pinMode(LED_PIN, OUTPUT);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

ON lights up the LED, OFF turns it dark.

Why here

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

  digitalWrite(LED_PIN, HIGH);

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It keeps the light on, and pauses so you can read each number.

Why here

Right after we turn the light on or off.

  delay(200);

begin

Big idea

Serial.begin opens a message screen so the board can talk to the computer.

In this project

It lets us send the count to the screen.

Why here

It goes in setup() once, before we print anything.

  Serial.begin(9600);

print

Big idea

Serial.print writes words or a number on the screen and stays on the same line.

In this project

It writes "Count: " right before the number.

Why here

In loop() so we can see each new count.

  Serial.print("Count: ");

println

Big idea

Serial.println writes on the screen and then jumps to a new line.

In this project

It prints a friendly hello when the board turns on.

Why here

So each message gets its own line.

  Serial.println("LED Counter — watch the count grow!");