Breadboard
docs
Example project · Displays

4-digit counter (7-segment)

The classic 12-pin 4-digit seven-segment module, wired the honest way: eight shared segment lines through 220 Ω resistors, four digit commons straight to GPIOs. The Uno multiplexes — one digit lit at a time for 2 ms, faster than your eye — to count 0000–9999. Watch the commons on the scope to see the strobing the display hides. The Code tab holds the same logic as an editable Arduino sketch.

The 4-digit counter (7-segment) circuit as rendered by the simulator

What's on the bench

  • 7× Resistor
  • 7-Segment (4 digits)
  • Arduino Uno
  • Battery

How it's wired

  • Battery · posArduino Uno · 5v
  • Battery · negArduino Uno · gnd
  • Arduino Uno · d2Resistor · a
  • Resistor · b7-Segment (4 digits) · a
  • Arduino Uno · d3Resistor · a
  • Resistor · b7-Segment (4 digits) · b
  • Arduino Uno · d4Resistor · a
  • Resistor · b7-Segment (4 digits) · c
  • Arduino Uno · d5Resistor · a
  • Resistor · b7-Segment (4 digits) · d
  • Arduino Uno · d6Resistor · a
  • Resistor · b7-Segment (4 digits) · e
  • Arduino Uno · d7Resistor · a
  • Resistor · b7-Segment (4 digits) · f
  • Arduino Uno · d8Resistor · a
  • Resistor · b7-Segment (4 digits) · g

…and 4 more connections — open it in the simulator to see every wire.

The code

This Arduino C++ sketch lives in the Code tab; sign in and press Compile & upload to build real firmware for the emulated board.

// 4-digit 7-segment multiplex counter. The 8 segment lines are SHARED
// by all four digits; each digit has its own common-cathode pin. Only
// one common may be LOW at a time — light the digits in turn faster
// than the eye can see (persistence of vision).
const uint8_t SEG_PINS[7] = {2, 3, 4, 5, 6, 7, 8};   // a b c d e f g
const uint8_t DIG_PINS[4] = {10, 11, 12, 13};        // dig1..dig4, active LOW
const uint8_t FONT[10] = {  // bit0 = a .. bit6 = g
  0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110,
  0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1101111,
};
unsigned int count = 0;
unsigned long last = 0;

void showDigit(uint8_t value, uint8_t digit) {
  for (uint8_t s = 0; s < 7; s++) digitalWrite(SEG_PINS[s], (FONT[value] >> s) & 1);
  digitalWrite(DIG_PINS[digit], LOW);   // only this digit's common low
  delay(2);                             // ~125 Hz full-display refresh
  digitalWrite(DIG_PINS[digit], HIGH);
}

void setup() {
  for (uint8_t s = 0; s < 7; s++) pinMode(SEG_PINS[s], OUTPUT);
  for (uint8_t d = 0; d < 4; d++) {
    pinMode(DIG_PINS[d], OUTPUT);
    digitalWrite(DIG_PINS[d], HIGH);    // all digits off
  }
}

void loop() {
  showDigit(count / 1000 % 10, 0);
  showDigit(count / 100 % 10, 1);
  showDigit(count / 10 % 10, 2);
  showDigit(count % 10, 3);
  if (millis() - last >= 250) {
    last += 250;
    count = (count + 1) % 10000;
  }
}

Related projects