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

Draw numbers with light!

Seven tiny bars light up in different shapes to show every number from 0 to 9.

Microwave timers, elevator floor numbers, and digital clocks all use these number displays.

The story

The problem

One light can only turn on or off. To show a number we need seven little bars working together.

Think of it like

It's like drawing numbers with seven sticks — each number has its own shape.

Meet the parts

It decides which bars to light for each number.

Arduino

The brain

Loading part…

Seven little bars that light up to make any number 0 to 9.

7-segment display

The number screen

Loading part…

How it works

1

Get the bars ready

Pins 2 to 8 get ready — one wire for each of the seven bars.

pinMode(SEG_A + i, OUTPUT);
2

Count up the numbers

This loop tries 0, then 1, then 2… all the way up to 9.

for (int digit = 0; digit <= 9; digit++)
3

Light the right bars

This helper looks up the number's shape and lights the right bars.

showDigit(digit);
4

Hold, then next number

Each number stays on screen for a moment, then the next one shows.

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

    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… all the way to 9.
  • The message screen prints each number too!

Peek at code

The number shapes

const uint8_t DIGITS[10] = {
  0b0111111,
  0b0000110,
  0b1011011,
  0b1001111,
  0b1100110,
  0b1101101,
  0b1111101,
  0b0000111,
  0b1111111,
  0b1101111,
};

DIGITS[] is a list of shapes — one pattern of bars for each number 0 to 9.

The 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);
  }
}

This little helper looks at the shape and turns each of the seven bars on or off.

Count 0 to 9

void loop() {
  for (int digit = 0; digit <= 9; digit++) {
    showDigit(digit);
    Serial.println(digit);
    delay(800);
  }
}

loop() shows 0, then 1, then 2… up to 9, again and again.

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: Yes! loop() runs again and again.

Q2. What does the for loop's digit do?

  • A. Counts 0 through 9 to pick which number to show
  • B. Turns off the message screen
  • C. Only runs in setup()
Why: Yes! digit is the counter that picks which number to show.

Code lab — try on your own

  1. Show each number longer — change delay(800) to delay(1200) in loop().

    Hint: Line 32 inside the for loop.

  2. Add a note (comment) on the pattern line that says it comes from DIGITS[].

    Hint: Line 15 inside showDigit().

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 light up the seven little bars on a number display to show the numbers 0 to 9.

Tip

Read from the top to the bottom. Tap any word or line if you need 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()

Big idea

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

In this project

It gets pins 2 to 8 ready, one for each bar, and opens the message screen.

Why here

Things we do only once go inside setup().

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()

Big idea

loop() runs again and again, forever.

In this project

This is where the display counts 0, 1, 2… up to 9 and starts over.

Why here

Things that repeat go inside loop().

void loop() {
  for (int digit = 0; digit <= 9; digit++) {
    showDigit(digit);
    Serial.println(digit);
    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 gets each bar's pin ready to push power out.

Why here

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

    pinMode(SEG_A + i, OUTPUT);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

ON lights one little bar, OFF leaves it dark — together they draw a number.

Why here

It goes inside the helper so each bar can change.

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

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It keeps each number on screen long enough to read.

Why here

Right after we show a number.

    delay(800);

begin

Big idea

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

In this project

It lets us print each number too.

Why here

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

  Serial.begin(9600);

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("7-Segment Counter Demo");