Car parking sensorTinkerPieCircuit tutorial 19 of 24 · Advanced · 25 minutes
Combine an ultrasonic sensor, an RGB LED and a buzzer into a car parking assistant.
Open this circuit in TinkerPieCircuit (free, runs in your browser)
What you will learn
- Using if / else if / else with sensor data
- Showing status with colours
- Building a real-world project
Parts
Steps
Wire the sensor
VCC →
5V, GND →GND, TRIG →P10, ECHO →P11.Wire the RGB LED
S1,S2,S3through 220 Ω resistors to R, G and B. Long leg toGND.Set the zones
More than 40 cm is green. 15 to 40 cm is orange with a slow beep. Under 15 cm is red with a steady tone.
Test
Press ▶ Start and drag the distance slider.
The code
function colour(r, g, b) {
analogWrite(S1, r);
analogWrite(S2, g);
analogWrite(S3, b);
}
function loop() {
const cm = readDistance(P10, P11);
if (cm > 40) {
colour(0, 255, 0);
noTone(BUZZER);
} else if (cm > 15) {
colour(255, 140, 0);
tone(BUZZER, 600);
delay(60);
noTone(BUZZER);
delay(300);
} else {
colour(255, 0, 0);
tone(BUZZER, 1000);
}
delay(100);
}
Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.
How it works
The program reads the distance and picks a zone with if / else if. Each zone sets a different colour and sound. Real cars use exactly this idea.
← Distance meter with an ultrasonic sensor · All 24 circuit tutorials · IR sensor people counter →