Add three LEDs that act like a traffic light for distance: green when it is far, yellow when it gets close and red when it is near.
| Part | How many |
|---|---|
| Wiring from Lesson 1 | |
| Red, yellow and green LED | 1 each |
| 220 Ω resistors (red-red-brown) | 3 |
| Male-to-male jumper wires | about 6 |
Nothing extra - just the kit and your computer.
Each LED has its own pin: red on 5, yellow on 6, green on 7. HIGH turns an LED on and LOW turns it off.
Each LED has its own 220 Ω resistor. Without one, the LED takes too much current and can damage itself or the UNO pin. Sharing one resistor between LEDs makes them glow unevenly.
loop() measures once, then picks one zone: under 20 cm red, under 50 cm yellow, otherwise green. Only one LED is on at a time.
If no echo comes back, the sketch does not know the distance. It blinks yellow instead of showing green: unknown must never look like safe.
Every item is in the kit — nothing extra to buy.
All 9 parts in one box, with this project and 8 more to build from them.
One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. red/yellow/green LEDs: included in the kit, ask us for it loose. Delivery is AED 20, free on orders AED 100+.
Unplug USB before you change any wire. Pin names are the labels printed on the board.
| Part / pin | Connect to | Note |
|---|---|---|
| Red LED long leg (+) | 220 Ω → UNO pin 5 | One resistor for each LED |
| Yellow LED long leg (+) | 220 Ω → UNO pin 6 | |
| Green LED long leg (+) | 220 Ω → UNO pin 7 | |
| All three short legs (−) | − rail (GND) | |
| HC-SR04 | As in Lesson 1 | TRIG pin 9, ECHO pin 10 |
One thing at a time. Tick nothing off until it matches the diagram.
Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.
/*
DIY KIT - Arduino Ultrasonic Distance & Parking Learning Kit
Lesson 2 of 9: Traffic-Light Distance Indicator
WIRING (unplug the USB cable before you wire)
HC-SR04 as in Lesson 1: VCC 5V, TRIG pin 9, ECHO pin 10, GND.
Red LED long leg -> 220 ohm resistor -> UNO pin 5
Yellow LED long leg -> 220 ohm resistor -> UNO pin 6
Green LED long leg -> 220 ohm resistor -> UNO pin 7
All three short legs -> GND (- rail)
WHAT SHOULD HAPPEN
Green = 50 cm or more (CLEAR)
Yellow = 20 to 49 cm (CAUTION)
Red = under 20 cm (NEAR)
Yellow blinking = no echo: the sensor does not know. Unknown is never shown as green.
The Serial Monitor (9600 baud) prints the zone and the distance.
*/
const int RED_LED = 5;
const int YELLOW_LED = 6;
const int GREEN_LED = 7;
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int CLEAR_CM = 50; // this far or more: green
const int NEAR_CM = 20; // closer than this: red
const unsigned long ECHO_TIMEOUT_US = 30000;
const int MIN_CM = 2;
const int MAX_CM = 400;
long readOnceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (us == 0) return 0; // no echo
long cm = us / 58;
if (cm < MIN_CM || cm > MAX_CM) return 0;
return cm;
}
// Turn each LED on (true) or off (false).
void showLights(bool red, bool yellow, bool green) {
digitalWrite(RED_LED, red ? HIGH : LOW);
digitalWrite(YELLOW_LED, yellow ? HIGH : LOW);
digitalWrite(GREEN_LED, green ? HIGH : LOW);
}
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
Serial.println("Lesson 2: Traffic-Light Distance Indicator");
// Light test: red, yellow, green, one after another.
showLights(true, false, false); delay(300);
showLights(false, true, false); delay(300);
showLights(false, false, true); delay(300);
showLights(false, false, false);
}
void loop() {
long cm = readOnceCm();
if (cm == 0) {
// No echo: blink yellow - on for half a second, off for half a second.
unsigned long halfSeconds = millis() / 500; // half-seconds since the UNO started
bool blinkOn = (halfSeconds % 2 == 0); // even: on, odd: off
showLights(false, blinkOn, false);
Serial.println("NO_ECHO");
} else if (cm < NEAR_CM) {
showLights(true, false, false);
Serial.print("NEAR ");
Serial.println(cm);
} else if (cm < CLEAR_CM) {
showLights(false, true, false);
Serial.print("CAUTION ");
Serial.println(cm);
} else {
showLights(false, false, true);
Serial.print("CLEAR ");
Serial.println(cm);
}
delay(100);
}
| In the code | What it does |
|---|---|
| const int CLEAR_CM = 50; const int NEAR_CM = 20; | The zone edges. Change them here and the whole sketch follows. |
| showLights(red, yellow, green) | Our own function: true switches an LED on, false switches it off. |
| if (cm == 0) { ... } else if (cm < NEAR_CM) ... | The decision: no echo first, then red, then yellow, otherwise green. |
| halfSeconds % 2 == 0 | Counts half-seconds since start. Even numbers: yellow on, odd: off. That makes the blink. |
| Problem | What to check |
|---|---|
| One LED never lights, not even in the light test | Unplug USB and turn that LED round (long leg towards the resistor). Check its wire goes to the right pin. |
| The colours light in the wrong zones | Two pin wires are swapped. Red must be on 5, yellow on 6, green on 7. |
| The LEDs light in the test but never change | The sensor wiring moved. Check it against Lesson 1. |
| An LED is very dim | Check the resistor bands: red-red-brown is 220 Ω. Brown-black-orange is 10 kΩ, far too big. |
| It always says no echo | Unplug USB. Check VCC to the + rail, GND to the − rail, TRIG to pin 9 and ECHO to pin 10 - by the labels on the sensor, not their position. Point it at a big flat object 20-50 cm away. |
| The upload fails | See Start here, step 6: "If the upload fails". |
Find the exact distance where yellow changes to red. Move the book 1 cm at a time. Is it always at the same place, or does it flicker at the edge?
To limit the current so the LED and the UNO pin are not damaged; sharing one makes them glow unevenly.
Yellow (20-49 cm).
No echo means the distance is unknown, and unknown must never look like "all clear".
Most problems are one wire in the wrong hole. Check these first:
Open the troubleshooting table for this step →
Hi DIY KIT, I'm stuck on the Arduino Ultrasonic Distance & Parking Learning Kit. Lesson: __ Step number: __ What should happen: __ What happens instead: __ Error message (copied exactly): __ I'll attach a photo of my wiring.
Wire the HC-SR04 and read distances in the Serial Monitor. Compare them with a ruler to see how accurate the sensor really is. Do Start here first.
Like a car's parking sensor on your desk: slow beeps under 20 cm and fast beeps under 10 cm, while the LEDs show the zone. A learning model only.
Find the sensor's limits: too close, too far and off to the side. The LEDs and running counts show good readings, "too close" and "no echo".
We use cookies that the shop needs to work — signing in, your cart, form security. We also measure how the shop is used so we can stock the right parts and fix what is broken. Choose “Only essential” and we will not load Google Analytics or the Meta pixel, and will not record your visit. Read more.