4-Digit 7-Segment
Display Counter
Build a 0000–9999 counter with UP, DOWN, RESET & AUTO modes using Arduino UNO and multiplexing — simulated entirely in Wokwi.
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
What You Need (Virtual in Wokwi)
| Component | Qty | Role |
|---|---|---|
| Arduino UNO | ×1 | Controls display & reads buttons |
| 4-Digit 7-Segment Display (Common Cathode) | ×1 | Shows 4-digit numbers 0000–9999 |
| 220Ω Resistors | ×7 | Segment current protection |
| Push Buttons (UP, DOWN, RESET, AUTO) | ×4 | Manual counter control |
| Jumper Wires | ×~20 | Connections 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
💡 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Ω
| Segment | Arduino Pin | Wire Color |
|---|---|---|
| a | 2 | Red |
| b | 3 | Orange |
| c | 4 | Yellow |
| d | 5 | Green |
| e | 6 | Blue |
| f | 7 | Purple |
| g | 8 | Gray |
| dp | 9 | White |
🔢 Digit Select Pins → Arduino
| Digit | Position | Arduino Pin |
|---|---|---|
| DIG1 | Thousands | 10 |
| DIG2 | Hundreds | 11 |
| DIG3 | Tens | 12 |
| DIG4 | Ones | 13 |
⚡ Power, Ground & Buttons
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.
Startup Animation
On power-up, all 4 digits cycle through 0–9 simultaneously (showing 0000 → 1111 → 2222 → … → 9999), then reset to 0000.
Check Buttons
checkButtons() reads all 4 pins on every loop iteration. A press is detected only on a HIGH→LOW transition, with 50ms debounce.
Update Counter
The counter variable is incremented/decremented and wraps at boundaries (0 and 9999). Serial.println() logs the new value for debugging.
Extract Digits
displayNumber() splits the counter into thousands, hundreds, tens, and ones using division and modulo operations.
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.
{
"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.
/* * 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); }
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.
- Click Start Simulation — watch the startup animation 0000 → 9999.
- Press the GREEN (UP) button repeatedly — counter increments.
- Press the YELLOW (AUTO) button — watch it count automatically.
- Let it reach 9999 — confirm it switches direction and counts down.
- Press RED (RESET) — confirm display returns to 0000.
- Challenge: Change
autoCountDelayfrom 1000 to 200 for 5× faster counting. - Advanced: Modify the code to stop at 9999 instead of reversing direction.
Full MakeMindz Curriculum
This project is part of a structured Arduino learning path. Explore all projects below — from blinking LEDs to biometric access systems.
Comments
Post a Comment