⭐⭐⭐ Challenge
⏱️ 30 minutes
Deluxe parking sensor
The deluxe version of the parking sensor! It beeps faster when you get closer, and the OLED screen shows the distance in centimeters. With a little bar that gets longer when you get closer.
🧰 What do you need?
🔌
USB cable + Jumper wires🗺️ Wiring diagram
Follow the wires from the Arduino to each part. You can choose the wire colors yourself.
| From | To |
|---|---|
Arduino UNO · D9 |
Distance sensor HC-SR04 · Trig |
Arduino UNO · D10 |
Distance sensor HC-SR04 · Echo |
Arduino UNO · 5V |
Distance sensor HC-SR04 · Vcc |
Arduino UNO · GND |
Distance sensor HC-SR04 · Gnd |
Arduino UNO · D8 |
Active buzzer · S |
Arduino UNO · 5V |
Active buzzer · + |
Arduino UNO · GND |
Active buzzer · - |
Arduino UNO · A4 |
OLED screen 0.91" · SDA |
Arduino UNO · A5 |
OLED screen 0.91" · SCL |
Arduino UNO · 5V |
OLED screen 0.91" · VCC |
Arduino UNO · GND |
OLED screen 0.91" · GND |
🛠️ This is how you build it
- Connect the HC-SR04: Trig to pin 9, Echo to pin 10, Vcc to 5V and Gnd to GND.
- Connect the buzzer: S to pin 8, + to 5V and − to GND.
- Connect the OLED screen: SDA to A4, SCL to A5, VCC to 5V and GND to GND.
- Three parts need 5V and GND. Use the + and − strips on your breadboard.
💻 The code
// Deluxe parking sensor
// Just like the normal parking sensor, but now you also see the distance on the OLED screen.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int TRIG_PIN = 9; // Trig of the HC-SR04 on pin 9
const int ECHO_PIN = 10; // Echo of the HC-SR04 on pin 10
const int BUZZER_PIN = 8; // S of the buzzer on pin 8
Adafruit_SSD1306 display(128, 32, &Wire, -1); // OLED: SDA = A4, SCL = A5
// Measure the distance in centimeters
long measureDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long time = pulseIn(ECHO_PIN, HIGH, 30000);
if (time == 0) return 999; // No echo: nothing nearby
return time / 58;
}
// Put the distance big on the screen, with a little bar below it
void showDistance(long distance) {
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
if (distance >= 999) {
display.print(F("---"));
} else {
display.print(distance);
display.print(F(" cm"));
}
// Little bar: the closer, the longer
int length = map(constrain(distance, 0, 100), 0, 100, 128, 0);
display.fillRect(0, 24, length, 8, SSD1306_WHITE);
display.display();
}
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(SSD1306_WHITE);
}
void loop() {
long distance = measureDistance();
showDistance(distance);
if (distance < 5) {
digitalWrite(BUZZER_PIN, HIGH); // Very close: one long tone
delay(100);
} else if (distance < 50) {
digitalWrite(BUZZER_PIN, HIGH); // Short beep
delay(50);
digitalWrite(BUZZER_PIN, LOW);
delay(distance * 10); // Closer = beeps faster
} else {
digitalWrite(BUZZER_PIN, LOW);
delay(100);
}
}
🧠 What does the code do?
measureDistance()measures the distance in centimeters with the HC-SR04.showDistance()puts the number big on the screen and draws a little bar withfillRect().- The buzzer beeps faster as the distance gets smaller.
🎯 Try this…
- Show "STOP!" on the screen when you are closer than 5 cm
- Show the distance in meters when something is more than 1 meter away
- Add the LED ring that changes from green to red
🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/parking-sensor-deluxe and click MAKE THIS. Then the code and the wiring diagram are ready right away.
Codey Online is a product of OTRONIC