breadboard
docs

Example project · Displays

4-digit counter (7-segment)

The classic 12-pin 4-digit seven-segment module: seven shared segment lines (a–g) through 220 Ω resistors, with the decimal point (dp) unwired and 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. Code 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 R3

How it's wired

  • Arduino Uno R3 · d2→Resistor · a
  • Resistor · b→7-Segment (4 digits) · a
  • Arduino Uno R3 · d3→Resistor · a
  • Resistor · b→7-Segment (4 digits) · b
  • Arduino Uno R3 · d4→Resistor · a
  • Resistor · b→7-Segment (4 digits) · c
  • Arduino Uno R3 · d5→Resistor · a
  • Resistor · b→7-Segment (4 digits) · d
  • Arduino Uno R3 · d6→Resistor · a
  • Resistor · b→7-Segment (4 digits) · e
  • Arduino Uno R3 · d7→Resistor · a
  • Resistor · b→7-Segment (4 digits) · f
  • Arduino Uno R3 · d8→Resistor · a
  • Resistor · b→7-Segment (4 digits) · g
  • Arduino Uno R3 · d10→7-Segment (4 digits) · dig1
  • Arduino Uno R3 · d11→7-Segment (4 digits) · dig2

…and 2 more connections. Open it in the simulator to see every wire.

The code

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

// 4-digit 7-segment multiplex counter. The seven a–g segment lines are SHARED
// by all four digits; dp is unwired. 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