PinoutSearch

7-Segment Digit Counter (Arduino)

Count 0–9 on a single 7-segment display. Learn how digits are formed from individual segments.

Beginner25 min

What you need

Wiring

Hover a pin or a wire to trace the connection.

Code

seven_segment.ino
// Count 0-9 on a common-cathode 7-segment display.
// Segment pins a,b,c,d,e,f,g on D2..D8.
const int seg[7] = {2, 3, 4, 5, 6, 7, 8};

// Bit order a..g (1 = lit). Common-cathode.
const byte digits[10] = {
  0b1111110, // 0
  0b0110000, // 1
  0b1101101, // 2
  0b1111001, // 3
  0b0110011, // 4
  0b1011011, // 5
  0b1011111, // 6
  0b1110000, // 7
  0b1111111, // 8
  0b1111011  // 9
};

void show(byte pattern) {
  for (int i = 0; i < 7; i++) digitalWrite(seg[i], (pattern >> (6 - i)) & 1);
}

void setup() {
  for (int i = 0; i < 7; i++) pinMode(seg[i], OUTPUT);
}

void loop() {
  for (int d = 0; d < 10; d++) { show(digits[d]); delay(800); }
}

Steps

  1. Wire segments a–g to D2–D8, each through a 220Ω resistor.
  2. Connect the common pin to GND (common-cathode display).
  3. Upload — it counts 0 through 9 and repeats.
  4. If segments are wrong, double-check the a–g pin mapping for your display.