7-Segment Display Counter using Arduino in Wokwi Simulator

 

Build a 7-Segment Display Counter in Wokwi Simulator using Arduino UNO. This beginner-friendly project displays numbers from 0 to 9 automatically and is perfect for students learning digital electronics and embedded systems.


Why Use Wokwi Simulator?

The Wokwi Arduino Simulator allows you to:

  • Build Arduino circuits online

  • Test 7-segment display projects without hardware

  • Debug Arduino code easily

  • Learn electronics safely and quickly


 Components (Add in Wokwi)

  • Arduino UNO

  • 7-Segment Display (Common Cathode)

  • 7 × 220Ω Resistors

  • Jumper wires


 Step-by-Step Instructions in Wokwi

Diagram.json:
{
  "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": {}
}


 Step 1: Create New Project

  1. Go to wokwi.com

  2. Click New Project

  3. Select Arduino UNO

 Step 2: Add Components

  • Click + (Add Component)

  • Search and add:

    • “7 Segment Display”

    • Add 7 resistors (220Ω)

 Step 3: Wiring Connections

7-Segment PinArduino Pin
a2
b3
c4
d5
e6
f7
g8
  • Connect both common cathode pins → GND

  • Connect each segment through a 220Ω resistor

Code:
/*
 * 7-Segment Display Counter with Arduino Uno
 *
 * This project demonstrates a digital counter using:
 * - 4-digit 7-segment display (common cathode)
 * - Push buttons for control (Up, Down, Reset)
 * - Automatic counting modes
 *
 * Features:
 * - Count up: 0000 to 9999
 * - Count down: 9999 to 0000
 * - Manual increment/decrement with buttons
 * - Reset to 0000
 * - Auto counting mode
 * - Speed control
 * - Display multiplexing
 */

// 7-Segment Display Pin Definitions
// Segment pins (a, b, c, d, e, f, g, dp)
#define SEG_A  2
#define SEG_B  3
#define SEG_C  4
#define SEG_D  5
#define SEG_E  6
#define SEG_F  7
#define SEG_G  8
#define SEG_DP 9  // Decimal point (optional)

// Digit select pins (D1, D2, D3, D4)
#define DIGIT_1  10  // Thousands
#define DIGIT_2  11  // Hundreds
#define DIGIT_3  12  // Tens
#define DIGIT_4  13  // Ones

// Button pins
#define BTN_UP    A0  // Increment counter
#define BTN_DOWN  A1  // Decrement counter
#define BTN_RESET A2  // Reset to 0
#define BTN_MODE  A3  // Toggle auto count mode

// Counter variables
int counter = 0;
int maxCount = 9999;
int minCount = 0;

// Button states
bool lastUpState = HIGH;
bool lastDownState = HIGH;
bool lastResetState = HIGH;
bool lastModeState = HIGH;

// Auto count mode
bool autoCountMode = false;
bool countUp = true;  // true = count up, false = count down
unsigned long lastAutoCount = 0;
int autoCountDelay = 1000;  // 1 second delay

// Display refresh
unsigned long lastRefresh = 0;
int currentDigit = 0;

// 7-segment digit patterns (0-9)
// Array represents: 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
};

// Segment pins array for easy access
int segmentPins[8] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G, SEG_DP};

// Digit pins array
int digitPins[4] = {DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4};

void setup() {
  // Initialize Serial for debugging
  Serial.begin(9600);
  Serial.println(F("7-Segment Counter Initialized"));
  Serial.println(F("=============================="));
  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"));
  Serial.println(F(""));
 
  // Configure segment pins as outputs
  for (int i = 0; i < 8; i++) {
    pinMode(segmentPins[i], OUTPUT);
    digitalWrite(segmentPins[i], LOW);
  }
 
  // Configure digit pins as outputs
  for (int i = 0; i < 4; i++) {
    pinMode(digitPins[i], OUTPUT);
    digitalWrite(digitPins[i], HIGH);  // Common cathode: HIGH = OFF
  }
 
  // Configure button pins as inputs with pull-up resistors
  pinMode(BTN_UP, INPUT_PULLUP);
  pinMode(BTN_DOWN, INPUT_PULLUP);
  pinMode(BTN_RESET, INPUT_PULLUP);
  pinMode(BTN_MODE, INPUT_PULLUP);
 
  // Display startup animation
  startupAnimation();
}

void loop() {
  // Check button states
  checkButtons();
 
  // Auto counting mode
  if (autoCountMode) {
    autoCount();
  }
 
  // Refresh display (multiplexing)
  refreshDisplay();
}

// Startup animation - count from 0 to 9 on all digits
void startupAnimation() {
  for (int i = 0; i <= 9; i++) {
    unsigned long startTime = millis();
    while (millis() - startTime < 100) {
      displayNumber(i * 1111);  // Show same digit on all positions
    }
  }
  counter = 0;  // Reset to 0 after animation
}

// Check all button states
void checkButtons() {
  // Read button states
  bool upState = digitalRead(BTN_UP);
  bool downState = digitalRead(BTN_DOWN);
  bool resetState = digitalRead(BTN_RESET);
  bool modeState = digitalRead(BTN_MODE);
 
  // Up button - Increment counter
  if (upState == LOW && lastUpState == HIGH) {
    delay(50);  // Debounce
    if (digitalRead(BTN_UP) == LOW) {
      incrementCounter();
      autoCountMode = false;  // Disable auto mode on manual input
    }
  }
  lastUpState = upState;
 
  // Down button - Decrement counter
  if (downState == LOW && lastDownState == HIGH) {
    delay(50);  // Debounce
    if (digitalRead(BTN_DOWN) == LOW) {
      decrementCounter();
      autoCountMode = false;  // Disable auto mode on manual input
    }
  }
  lastDownState = downState;
 
  // Reset button - Reset to 0
  if (resetState == LOW && lastResetState == HIGH) {
    delay(50);  // Debounce
    if (digitalRead(BTN_RESET) == LOW) {
      resetCounter();
      autoCountMode = false;  // Disable auto mode on reset
    }
  }
  lastResetState = resetState;
 
  // Mode button - Toggle auto count
  if (modeState == LOW && lastModeState == HIGH) {
    delay(50);  // Debounce
    if (digitalRead(BTN_MODE) == LOW) {
      toggleAutoMode();
    }
  }
  lastModeState = modeState;
}

// Increment counter
void incrementCounter() {
  counter++;
  if (counter > maxCount) {
    counter = minCount;  // Wrap around
  }
  Serial.print(F("Counter: "));
  Serial.println(counter);
}

// Decrement counter
void decrementCounter() {
  counter--;
  if (counter < minCount) {
    counter = maxCount;  // Wrap around
  }
  Serial.print(F("Counter: "));
  Serial.println(counter);
}

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

// Toggle auto counting mode
void toggleAutoMode() {
  autoCountMode = !autoCountMode;
 
  if (autoCountMode) {
    Serial.println(F("Auto Count Mode: ON"));
    Serial.print(F("Direction: "));
    Serial.println(countUp ? "UP" : "DOWN");
  } else {
    Serial.println(F("Auto Count Mode: OFF"));
  }
}

// Auto counting function
void autoCount() {
  unsigned long currentTime = millis();
 
  if (currentTime - lastAutoCount >= autoCountDelay) {
    if (countUp) {
      incrementCounter();
      // Switch to count down when reaching max
      if (counter >= maxCount) {
        countUp = false;
      }
    } else {
      decrementCounter();
      // Switch to count up when reaching min
      if (counter <= minCount) {
        countUp = true;
      }
    }
    lastAutoCount = currentTime;
  }
}

// Display a 4-digit number
void displayNumber(int number) {
  // Extract individual digits
  int digit1 = (number / 1000) % 10;  // Thousands
  int digit2 = (number / 100) % 10;   // Hundreds
  int digit3 = (number / 10) % 10;    // Tens
  int digit4 = number % 10;           // Ones
 
  // Display each digit in sequence (multiplexing)
  displayDigit(0, digit1);
  delay(2);
  displayDigit(1, digit2);
  delay(2);
  displayDigit(2, digit3);
  delay(2);
  displayDigit(3, digit4);
  delay(2);
}

// Display a single digit at a specific position
void displayDigit(int position, int digit) {
  // Turn off all digits
  for (int i = 0; i < 4; i++) {
    digitalWrite(digitPins[i], HIGH);  // Common cathode: HIGH = OFF
  }
 
  // Set segments for the digit
  for (int i = 0; i < 7; i++) {
    digitalWrite(segmentPins[i], digitPatterns[digit][i]);
  }
 
  // Turn on the selected digit
  digitalWrite(digitPins[position], LOW);  // Common cathode: LOW = ON
}

// Refresh display with current counter value
void refreshDisplay() {
  displayNumber(counter);
}

// Function to display a specific pattern (for testing)
void testPattern() {
  // Display "8888" - all segments on
  for (int i = 0; i < 100; i++) {
    displayNumber(8888);
  }
}

// Function to blank the display
void blankDisplay() {
  for (int i = 0; i < 4; i++) {
    digitalWrite(digitPins[i], HIGH);
  }
  for (int i = 0; i < 8; i++) {
    digitalWrite(segmentPins[i], LOW);
  }
}

// Function to display custom message (optional)
void displayCustom(int d1, int d2, int d3, int d4) {
  for (int i = 0; i < 50; i++) {
    displayDigit(0, d1);
    delay(2);
    displayDigit(1, d2);
    delay(2);
    displayDigit(2, d3);
    delay(2);
    displayDigit(3, d4);
    delay(2);
  }
}

  •  Step 4: Run Simulation

    1. Click Start Simulation

    2. Watch the display count from 0 to 9

    3. The counter repeats continuously


     Learning Benefits

    • Understanding 7-segment display working

    • Arduino digital output control

    • Embedded counter logic

    • Wokwi circuit simulation skills


Comments