03 / Templates · beginner
7-SEGMENT COUNTER
Open in PlaygroundCounts 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
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);
}
}
- wokwi-arduino-uno01
- wokwi-7segment02
| Component Pin | Arduino Pin |
|---|---|
| Segment A | Pin 2 |
| Segment B | Pin 3 |
| Segment C | Pin 4 |
| Segment D | Pin 5 |
| Segment E | Pin 6 |
| Segment F | Pin 7 |
| Segment G | Pin 8 |
| COM.1 & COM.2 | GND |