# 🚗 DIY parking assistiant with arduino

In modern cars, parking sensors are becoming part of the equipment, because it’s really convenient. In the more expensive models, there is also a video camera, which displays an image during a backward stroke, on which the proximity indicator superimposes.

In modern cars, parking sensors are becoming part of the equipment, because it’s really convenient. In the more expensive models, there is also a video camera, which displays an image during a backward stroke, on which the proximity indicator superimposes.
I decided to make the simplest implementation of parking sensors with sound and light indication.

Matireals required:

• Arduino (Uno, Mega, Nano, …)
• HC-SR04 (ultrasonic sensor)
• 3x LED’s (red, yellow, green)
• 3x 220Ohm resistors
• Wires
• Buzzer

The task is to measure the distance between the object and the sensor and, if the range is less than 20 cm, turn on the red LED and sound on the buzzer. If the distance is more than 100 cm turn on the green LED, if from 20 cm to 100 – yellow.

I got this connection scheme:

More details about what pins of the board I connected to:
Distance sensor:

• TrigPin – 12;
• EchoPin – 13;
• VCC – 5V;
• GND – GND;

LEDs and buzzer:

• Green LED (led) – 2;
• Yellow LED (led2) – 3;
• Red LED (led3) – 4;
• Buzzer (piezo dynamic) – 11;

Above shows the assembled physical scheme. You need only to download the sketch and the parking sensor is ready. We transfer the distance sensor to echolocation mode with a short pulse, at which ultrasonic waves are sent to the surrounding space. We determine the pulse duration using the function pulseln, which shows us the time elapsed before the signal level changes. After receiving the time we translate it into the distance in centimeters by dividing by a constant (for sensor SR04 it is 29.1). Since the time passed by ultrasonic waves to the target is the distance there and back, everything must be divided by 2. Do not forget about the delay between measurements for a particular job.

#define trigPin 12
#define echoPin 13
#define led 2
#define led2 3
#define led3 4
#define buzzer 11
int sound = 250;

void setup() {
Serial.begin (9600);
//define pinmodes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
//generate 2mcs impulse
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
//HIGH signal , wait 10 mcs
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
//time on sensor
duration = pulseIn(echoPin, HIGH);
//time to distance
distance = (duration / 2) / 29.1;
if (distance > 100) {
digitalWrite(led, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);

noTone(buzzer);
}
if (distance < 20) {
digitalWrite(led3, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led, LOW);
sound = 250;
tone(buzzer, sound);
}
if ((distance>=20)and(distance<100)){
digitalWrite(led2, HIGH);
digitalWrite(led, LOW);
digitalWrite(led3, LOW);
sound = 0;
noTone(buzzer);
}
delay(500); //delay between mesurement
Serial.print(distance);
Serial.println(” cm”);
}
Here is what you should get: