GENERALNov 8, 2026Trần Thế Phúc
PRACTICE: 4 LEDs COUNTING FROM 1 TO 15
#Arduino#Binary#LED Project
SPECIFICATION & TECHNICAL NOTES
PROBLEM INTRODUCTION
In this practical exercise, we will use 4 LEDs to represent numbers from 1 to 15 in binary. This is an excellent way to understand how computers process data and how to control multiple outputs simultaneously.
CIRCUIT CONNECTIONS
Connections:
- LED 1 (LSB): Digital Pin 2
- LED 2: Digital Pin 3
- LED 3: Digital Pin 4
- LED 4 (MSB): Digital Pin 5
- Connect the shorter leg (cathode) of each LED to GND through a 220Ω resistor.
BINARY REPRESENTATION TABLE (1–15)
Decimal Number — LED 4 (8) — LED 3 (4) — LED 2 (2) — LED 1 (1)
1
LED 4: OFF
LED 3: OFF
LED 2: OFF
LED 1: ON
2
LED 4: OFF
LED 3: OFF
LED 2: ON
LED 1: OFF
...
...
15
LED 4: ON
LED 3: ON
LED 2: ON
LED 1: ON
SOURCE CODE
void setup() {
for (int i = 2; i <= 5; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
for (int count = 1; count <= 15; count++) {
displayBinary(count);
delay(1000);
}
}
void displayBinary(int num) {
for (int i = 0; i < 4; i++) {
// Check each bit of the number
if (bitRead(num, i)) {
digitalWrite(i + 2, HIGH);
} else {
digitalWrite(i + 2, LOW);
}
}
}
Verified by FTC 31113 Technical LeadID: b3c812fd-9749-4a0a-8c2e-104698bf7d08
