Arduino 7-Segment Display Counter in Wokwi (Complete Beginner Guide)

4-Digit 7-Segment Display Counter with Arduino | Wokwi Tutorial | MakeMindz

What Are We Building?

Build a 4-Digit 7-Segment Display Counter using Arduino UNO in the Wokwi Simulator. This hands-on embedded systems project helps students understand multiplexing, button control, counter logic, and digital display interfacing — all without physical hardware.

📈

Count Up

0000 → 9999 with automatic direction switching

📉

Count Down

9999 → 0000 on boundary or button press

👉

Manual Control

Increment, decrement, and reset with push buttons

Auto Mode

Automatic bidirectional counting, toggleable

Startup Animation

Visual countdown flash on power-up

🔌

Multiplexing

4 digits driven by one set of segment pins

Digital output control
7-segment display mapping
Multiplexing technique
Button debouncing
State change detection
Embedded counter logic
Serial debugging

What You Need (Virtual in Wokwi)

ComponentQtyRole
Arduino UNO×1Controls display & reads buttons
4-Digit 7-Segment Display (Common Cathode)×1Shows 4-digit numbers 0000–9999
220Ω Resistors×7Segment current protection
Push Buttons (UP, DOWN, RESET, AUTO)×4Manual counter control
Jumper Wires×~20Connections between parts

🌐 Using Wokwi? Click + Add Component, search for "7segment", set digits: 4 and common: cathode. Then add 4 pushbuttons and resistors from the parts panel.

Button Controls

UP
Pin A0
Increment counter by 1. Disables auto mode.
DOWN
Pin A1
Decrement counter by 1. Disables auto mode.
RESET
Pin A2
Resets counter to 0000. Disables auto mode.
AUTO
Pin A3
Toggle automatic bidirectional counting.

💡 Buttons use INPUT_PULLUP — they read HIGH normally and LOW when pressed. Connect one side to the Arduino pin and the other to GND.

Pin Connections

🔌 Segment Pins (a–g, dp) → Arduino via 220Ω

SegmentArduino PinWire Color
a2Red
b3Orange
c4Yellow
d5Green
e6Blue
f7Purple
g8Gray
dp9White

🔢 Digit Select Pins → Arduino

DigitPositionArduino Pin
DIG1Thousands10
DIG2Hundreds11
DIG3Tens12
DIG4Ones13

⚡ Power, Ground & Buttons

COMGND on Arduino
UP buttonPin A0 & GND (INPUT_PULLUP)
DOWN buttonPin A1 & GND (INPUT_PULLUP)
RESET buttonPin A2 & GND (INPUT_PULLUP)
AUTO buttonPin A3 & GND (INPUT_PULLUP)

The Logic Behind It

⚡ Multiplexing

Only one digit turns ON at a time, but switching happens so fast (every 2ms) that all four digits appear lit simultaneously. The Arduino cycles through all four digit select pins rapidly.

🔁 Auto Mode Logic

When AUTO is ON, the counter increments every second up to 9999, then automatically reverses direction and counts down to 0000, then repeats indefinitely.

👉 Button Debouncing

Each button uses a 50ms delay() debounce to prevent multiple triggers from a single press. State change detection (HIGH→LOW transition) ensures one action per press.

1

Startup Animation

On power-up, all 4 digits cycle through 0–9 simultaneously (showing 0000 → 1111 → 2222 → … → 9999), then reset to 0000.

2

Check Buttons

checkButtons() reads all 4 pins on every loop iteration. A press is detected only on a HIGH→LOW transition, with 50ms debounce.

3

Update Counter

The counter variable is incremented/decremented and wraps at boundaries (0 and 9999). Serial.println() logs the new value for debugging.

4

Extract Digits

displayNumber() splits the counter into thousands, hundreds, tens, and ones using division and modulo operations.

5

Multiplex Display

Each digit is shown for 2ms in sequence. The segment pattern array is used to set pins HIGH/LOW, then the digit select pin is pulled LOW (ON) for common cathode.

diagram.json

Paste this into your Wokwi project's diagram.json file to instantly wire all components — no manual connections needed.

diagram.json — Wokwi Circuit
{
  "version": 1,
  "author": "7-Segment Display Counter",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-7segment", "id": "seg1",
      "top": -150, "left": 100,
      "attrs": { "digits": "4", "common": "cathode" }
    },
    { "type": "wokwi-pushbutton", "id": "btn_up",    "top": 100, "left": -100, "attrs": { "color": "green",  "label": "UP"    } },
    { "type": "wokwi-pushbutton", "id": "btn_down",  "top": 150, "left": -100, "attrs": { "color": "blue",   "label": "DOWN"  } },
    { "type": "wokwi-pushbutton", "id": "btn_reset", "top": 200, "left": -100, "attrs": { "color": "red",    "label": "RESET" } },
    { "type": "wokwi-pushbutton", "id": "btn_mode",  "top": 250, "left": -100, "attrs": { "color": "yellow", "label": "AUTO"  } }
  ],
  "connections": [
    [ "seg1:A",   "uno:2",       "red",     ["v0"] ],
    [ "seg1:B",   "uno:3",       "orange",  ["v0"] ],
    [ "seg1:C",   "uno:4",       "yellow",  ["v0"] ],
    [ "seg1:D",   "uno:5",       "green",   ["v0"] ],
    [ "seg1:E",   "uno:6",       "blue",    ["v0"] ],
    [ "seg1:F",   "uno:7",       "purple",  ["v0"] ],
    [ "seg1:G",   "uno:8",       "gray",    ["v0"] ],
    [ "seg1:DP",  "uno:9",       "white",   ["v0"] ],
    [ "seg1:DIG1","uno:10",      "brown",   ["v0"] ],
    [ "seg1:DIG2","uno:11",      "pink",    ["v0"] ],
    [ "seg1:DIG3","uno:12",      "cyan",    ["v0"] ],
    [ "seg1:DIG4","uno:13",      "magenta", ["v0"] ],
    [ "seg1:COM", "uno:GND.1",   "black",   ["v0"] ],
    [ "btn_up:1.l",   "uno:A0",   "green",   ["v0"] ],
    [ "btn_up:1.r",   "uno:GND.2","black",   ["v0"] ],
    [ "btn_down:1.l", "uno:A1",   "blue",    ["v0"] ],
    [ "btn_down:1.r", "uno:GND.2","black",   ["v0"] ],
    [ "btn_reset:1.l","uno:A2",   "red",     ["v0"] ],
    [ "btn_reset:1.r","uno:GND.2","black",   ["v0"] ],
    [ "btn_mode:1.l", "uno:A3",   "yellow",  ["v0"] ],
    [ "btn_mode:1.r", "uno:GND.2","black",   ["v0"] ]
  ],
  "dependencies": {}
}

The Program

Paste this into Wokwi's code editor. The code handles multiplexing, button debouncing, auto-counting, and a startup animation.

seven_segment_counter.ino
/*
 * 4-Digit 7-Segment Display Counter
 * Arduino UNO + Wokwi Simulator
 * MakeMindz Summer Class — Intermediate Project
 *
 * Features: Count up/down 0000-9999, manual & auto mode,
 *           multiplexing, button debounce, startup animation
 */

// ── Segment Pin Definitions ──────────────────
#define SEG_A   2   // Top horizontal
#define SEG_B   3   // Top-right vertical
#define SEG_C   4   // Bottom-right vertical
#define SEG_D   5   // Bottom horizontal
#define SEG_E   6   // Bottom-left vertical
#define SEG_F   7   // Top-left vertical
#define SEG_G   8   // Middle horizontal
#define SEG_DP  9   // Decimal point (optional)

// ── Digit Select Pins ────────────────────────
#define DIGIT_1  10  // Thousands
#define DIGIT_2  11  // Hundreds
#define DIGIT_3  12  // Tens
#define DIGIT_4  13  // Ones

// ── Button Pins (INPUT_PULLUP) ───────────────
#define BTN_UP    A0  // Increment
#define BTN_DOWN  A1  // Decrement
#define BTN_RESET A2  // Reset to 0
#define BTN_MODE  A3  // Toggle auto count

// ── Counter State ────────────────────────────
int counter     = 0;
int maxCount    = 9999;
int minCount    = 0;

// ── Button State Tracking ────────────────────
bool lastUpState    = HIGH;
bool lastDownState  = HIGH;
bool lastResetState = HIGH;
bool lastModeState  = HIGH;

// ── Auto Count Mode ──────────────────────────
bool autoCountMode   = false;
bool countUp         = true;
unsigned long lastAutoCount = 0;
int  autoCountDelay  = 1000; // 1 second per step

// ── Segment patterns [a,b,c,d,e,f,g] ─────────
byte digitPatterns[10][7] = {
  {1,1,1,1,1,1,0}, // 0
  {0,1,1,0,0,0,0}, // 1
  {1,1,0,1,1,0,1}, // 2
  {1,1,1,1,0,0,1}, // 3
  {0,1,1,0,0,1,1}, // 4
  {1,0,1,1,0,1,1}, // 5
  {1,0,1,1,1,1,1}, // 6
  {1,1,1,0,0,0,0}, // 7
  {1,1,1,1,1,1,1}, // 8
  {1,1,1,1,0,1,1}  // 9
};

int segmentPins[8] = {SEG_A,SEG_B,SEG_C,SEG_D,SEG_E,SEG_F,SEG_G,SEG_DP};
int digitPins[4]   = {DIGIT_1,DIGIT_2,DIGIT_3,DIGIT_4};

void setup() {
  Serial.begin(9600);
  Serial.println(F("7-Segment Counter Initialized"));
  Serial.println(F("Controls:"));
  Serial.println(F("  BTN_UP (A0): Increment"));
  Serial.println(F("  BTN_DOWN (A1): Decrement"));
  Serial.println(F("  BTN_RESET (A2): Reset to 0"));
  Serial.println(F("  BTN_MODE (A3): Auto Count Mode"));

  for (int i = 0; i < 8; i++) {
    pinMode(segmentPins[i], OUTPUT);
    digitalWrite(segmentPins[i], LOW);
  }
  for (int i = 0; i < 4; i++) {
    pinMode(digitPins[i], OUTPUT);
    digitalWrite(digitPins[i], HIGH); // Cathode: HIGH = OFF
  }
  pinMode(BTN_UP,    INPUT_PULLUP);
  pinMode(BTN_DOWN,  INPUT_PULLUP);
  pinMode(BTN_RESET, INPUT_PULLUP);
  pinMode(BTN_MODE,  INPUT_PULLUP);

  startupAnimation();
}

void loop() {
  checkButtons();
  if (autoCountMode) autoCount();
  refreshDisplay();
}

void startupAnimation() {
  for (int i = 0; i <= 9; i++) {
    unsigned long t = millis();
    while (millis() - t < 100) displayNumber(i * 1111);
  }
  counter = 0;
}

void checkButtons() {
  bool upState    = digitalRead(BTN_UP);
  bool downState  = digitalRead(BTN_DOWN);
  bool resetState = digitalRead(BTN_RESET);
  bool modeState  = digitalRead(BTN_MODE);

  if (upState == LOW && lastUpState == HIGH) {
    delay(50);
    if (digitalRead(BTN_UP) == LOW) { incrementCounter(); autoCountMode = false; }
  }
  lastUpState = upState;

  if (downState == LOW && lastDownState == HIGH) {
    delay(50);
    if (digitalRead(BTN_DOWN) == LOW) { decrementCounter(); autoCountMode = false; }
  }
  lastDownState = downState;

  if (resetState == LOW && lastResetState == HIGH) {
    delay(50);
    if (digitalRead(BTN_RESET) == LOW) { resetCounter(); autoCountMode = false; }
  }
  lastResetState = resetState;

  if (modeState == LOW && lastModeState == HIGH) {
    delay(50);
    if (digitalRead(BTN_MODE) == LOW) toggleAutoMode();
  }
  lastModeState = modeState;
}

void incrementCounter() {
  counter = (counter >= maxCount) ? minCount : counter + 1;
  Serial.print(F("Counter: ")); Serial.println(counter);
}

void decrementCounter() {
  counter = (counter <= minCount) ? maxCount : counter - 1;
  Serial.print(F("Counter: ")); Serial.println(counter);
}

void resetCounter() {
  counter = 0;
  Serial.println(F("Counter Reset!"));
}

void toggleAutoMode() {
  autoCountMode = !autoCountMode;
  Serial.println(autoCountMode ? F("Auto Mode: ON") : F("Auto Mode: OFF"));
}

void autoCount() {
  unsigned long now = millis();
  if (now - lastAutoCount >= autoCountDelay) {
    if (countUp) {
      incrementCounter();
      if (counter >= maxCount) countUp = false;
    } else {
      decrementCounter();
      if (counter <= minCount) countUp = true;
    }
    lastAutoCount = now;
  }
}

void displayNumber(int number) {
  displayDigit(0, (number / 1000) % 10); delay(2);
  displayDigit(1, (number / 100)  % 10); delay(2);
  displayDigit(2, (number / 10)   % 10); delay(2);
  displayDigit(3,  number          % 10); delay(2);
}

void displayDigit(int pos, int digit) {
  for (int i = 0; i < 4; i++) digitalWrite(digitPins[i], HIGH); // All OFF
  for (int i = 0; i < 7; i++) digitalWrite(segmentPins[i], digitPatterns[digit][i]);
  digitalWrite(digitPins[pos], LOW); // Selected digit ON
}

void refreshDisplay() { displayNumber(counter); }
7-Segment Counter Initialized
Controls:
BTN_UP (A0): Increment
BTN_DOWN (A1): Decrement
BTN_RESET (A2): Reset to 0
BTN_MODE (A3): Auto Count Mode
Counter: 1
Counter: 2
Auto Mode: ON
Counter: 3 ...

Simulate in Wokwi

The complete circuit is available in Wokwi. Click Start Simulation, then use the button controls to test counting, auto mode, and reset.

Open in Wokwi Simulator

Pre-wired circuit with all 4 buttons and a 4-digit display. No hardware needed — test everything in your browser instantly.

▶ Launch Wokwi ↗
🧪 Challenges to try in the simulator:
  1. Click Start Simulation — watch the startup animation 0000 → 9999.
  2. Press the GREEN (UP) button repeatedly — counter increments.
  3. Press the YELLOW (AUTO) button — watch it count automatically.
  4. Let it reach 9999 — confirm it switches direction and counts down.
  5. Press RED (RESET) — confirm display returns to 0000.
  6. Challenge: Change autoCountDelay from 1000 to 200 for 5× faster counting.
  7. Advanced: Modify the code to stop at 9999 instead of reversing direction.

Comments

try for free