ūüöó 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.
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
  • Breadboard

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:
For convenience, I made a Parktronic case. Download the 3D model by clicking this link.
It turned out not the most compact, and probably not everyone wants to hang such a box on their cars. So here I leave for you open space for creativity.
Options for improving the mass: you can write an algorithm in which the piezo-dynamics will make a sound in pulses with different frequency, depending on the distance; LEDs may not switch, but always illuminate if the threshold is exceeded.
In unmanned vehicles, in fact, ultrasonic sensors along the sides are also used as part of the short-range navigation system. At long distances, lidars usually work. The onboard computer of the car adds data from all sensors to create a picture of the current location and to create a safe route.
Use these devices in real life with extreme caution. Ideally, such homemade parking sensors are best tested along with a commercial counterpart, to eliminate the possibility of false positives, and the lack of response when approaching objects of different forms.

Leave a comment

Name .
.
Message .

Please note, comments must be approved before they are published