CNAP
03 / Templates · beginner

7-SEGMENT COUNTER

Open in Playground

Counts 0–9 on a single 7-segment display, updating once per second. Each segment A–G is driven directly from a digital pin. In real hardware, add a 220 Ω resistor in series with each segment pin.

beginner7-segmentdisplaycounter
Circuit
Code
sketch.inoArduino C++
44 lines
// 7-Segment Counter — counts 0 to 9 with a 1-second pause
// Pins: A=2, B=3, C=4, D=5, E=6, F=7, G=8
// Segment layout:
//   A
//  F B
//   G
//  E C
//   D

const int SEG_A = 2, SEG_B = 3, SEG_C = 4, SEG_D = 5;
const int SEG_E = 6, SEG_F = 7, SEG_G = 8;

// Each row: [A, B, C, D, E, F, G]
const bool DIGITS[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
};

const int PINS[7] = {SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G};

void showDigit(int d) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(PINS[i], DIGITS[d][i]);
  }
}

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

void loop() {
  for (int d = 0; d <= 9; d++) {
    showDigit(d);
    delay(1000);
  }
}
Components
  • wokwi-arduino-uno
    01
  • wokwi-7segment
    02
Wiring
Component PinArduino Pin
Segment APin 2
Segment BPin 3
Segment CPin 4
Segment DPin 5
Segment EPin 6
Segment FPin 7
Segment GPin 8
COM.1 & COM.2GND