In this tutorial, we will illustrate the installation and the use of simple, low-cost parking assist car sensors. We will use two HC-SR04 ultrasonic sensors and four Piezo buzzers to progressively alert the driver about the proximity of nearby cars behind the driver’s car while parking with an acoustic beeping sound. We will also solve practical problems like waterproofing and deploying of wires within a car’s cabin.
Let’s start by preparing the electronics shown below so we can test the system before actually installing car sensors in the car.
Figure 1: Required hardware components – Arduino, plastic case, ultrasonic sensors, Piezo buzzers, wires, and a power adapter
In this project, we will only be using two sensors for the rear: one on the left side of the bumper, and the other on the right side of the bumper. Assistive alert systems in most new cars have up to six sensors for more precise positioning.
Figure 2 below shows a diagram on how to wire the car sensors and buzzers to the Arduino.
Figure 2: Wire diagram of the Arduino, ultrasonic sensors, and Piezo buzzers
Step 2: Programming the Arduino
Let’s start writing a program. In general, for any microcontroller firmware we can find the following four elements:
Definition of global values and libraries
Unlike in software development, when programming microcontrollers we normally have some limitations as to how large our code can be. My Arduino Uno comes with an Atmega328, which has 32 KB memory. For this particular application this is more than enough.
Define global variables
#define trigPin1 13
#define echoPin1 12
#define buzzerPin1 6
#define trigPin2 11
#define echoPin2 10
#define buzzerPin2 7
This code will run once at the beginning
pinMode(2,OUTPUT);// We will use the pin 2 as ground. We need to make sure it
digitalWrite(2,LOW);// is in LOW position so it works as a ground.
The functions pinMode() and digitalWrite() are convenient ways of dealing with the microcontroller’s pins. We will use the pinMode() function to set the direction of the specified pin(s). The direction can be either input or output. Once you set the direction, the pin will only work in that direction. We will use digitalWrite() function to set the specified digital pin to HIGH or LOW. Here, I set pin 2 as another extra ground, which I needed in order to connect the GND pin of one of the devices.
This will iterate as long as there is power applied to the Arduino
calculateDistance(echoPin1,trigPin1,buzzerPin1);//Get the distance for the left
calculateDistance(echoPin2,trigPin2,buzzerPin2);//Get the distance for the right
For the sake of simplicity, I have defined a function (calculateDistance (echo, trigger, buzzer) that will be used in one of the sensors first, and in the other sensor second.
digitalWrite(trigger,LOW);// Set the trigger pin of the HC-SR04 to LOW
delayMicroseconds(2);// Wait a little to stabilize the sensor
digitalWrite(trigger,HIGH);// Set the trigger HIGH and send a pulse
delayMicroseconds(10);// Wait for 10us until the wave comes back
digitalWrite(trigger,LOW);// Set the trigger pin of the HC-SR04 to LOW again
duration=pulseIn(echo,HIGH);// Arduino’s built in function pulseIn reads a pulse
// (either HIGH or LOW) on a pin. Returns the length of
// the pulse in microseconds or 0 if no complete pulse
// was received within the timeout.
distance=(duration/2)/29.1;// The speed of sound is 340 m/s or 29 microseconds
// per centimeter.
// The ping travels out and back, so to find the distance
// if the object we take half of the distance travelled.
Serial.println("Out of range");// Use some feedback in the serial port for
tone(buzzer,2000,25);// Arduino’s built in function tone generates a square
// wave of the specified frequency (2000Hz) for the
// indication time (25 ms)
delay(distance*10);// The delay gives feedback beeping faster the closer
// we get to an object
Serial.print(distance);// Use some feedback in the serial port for
We first activate a pin (trigger) because it will take a while to send us back information about another pin (echo). With this time interval we can make some assumptions and calculate a distance (the speed of sound is 340 m/s or 29 microseconds per centimeter). This distance will serve as a parameter to establish how often to emit a beep of 2000 Hz (I found this by trial and error, feel free to change it to your preferred pitch).
I used several built-in functions, such as tone (pin, frequency, duration), digitalWrite (pin), delayMicroseconds(duration), and pulseIn(pin, value).
Step 3: Preparing a good strategy for assembly
Find a waterproof area in the rear body of your car. Sensors will be installed here. In my car I had several options:
Inside the tail light covers
Near the license plate bulbs
On the rear bumpers
If you were to place the car sensors either inside the tail light covers or on the rear bumpers, you must drill holes to allow ultrasound waves to come in and out. Unfortunately, these holes would also allow water to come in (rain, splashes, etc.) unless they were very well made. Waterproofing them with sealant is not an option because the ultrasound waves will not make it out. In addition, you will have the added difficulty of calibrating the position of the holes with respect to the sensor in order to avoid diffraction effects.
To learn more about diffraction waves, please click here.
So, I chose to place the car sensors near the license plate because the gap underneath the trunk door/handle is large enough, and it spares me the trouble of drilling holes. All I need to do is slightly adjust the orientation of the sensors to aim towards the corners of the car.
Figure 4: Finding the right location for the sensors
Step 4: Deploying the hardware inside the car
Find a place for the Arduino and the Piezo buzzers. For this step we will need to un-mount the car door panels and find a safe, empty place to attach the electronics.
Let the fun begin! The inside panels reveal a whole new dimension of your car. What we have there is a lot of space where we can attach many devices (maybe for future posts!). There are also a number of internal wires that supply power to different elements of the car related to safety. Make sure you don’t touch any important wires.
YOU MUST BE VERY CAREFUL WITH THE INTERNAL PARTS OF THE CAR.
We will connect the ultrasonic sensors from the outside of the car to the Arduino through the holes that hold the license plate. I had to make those holes a little bit larger to have all eight wires pass through and still leave room for the supporting screws.
The Piezo buzzers were arranged with simple two-sided adhesive tape.
Figure 5: Assembly of the HC-SR04 on the right side of the license plate
Figure 6: Assembly of one of the Piezo buzzers with two-sided tape
Figure 7: Locations of the modules on trunk door (inside view)
Figure 8: Locations of the modules (rear view)
Altogether, we will need about 6 meters of wire for the sensors and the buzzers in order for the system to operate comfortably. It is important to color code the wires to prevent confusion in the future.
Step 5: Deploying the wires inside the car
Once the hardware is in place we need to connect all the wires. The sensors need eight cables (two echoes, two triggers, two grounds and two VCCs) that need to go through the holes behind the license plate. Let’s verify the connections:
Right sensor trigger / Arduino pin 13
Right sensor echo / Arduino pin 12
Right sensor GND / Arduino GND
Right sensor VCC / Arduino VCC
Right buzzer + / Arduino pin 6
Left sensor trigger / Arduino pin 11
Left sensor echo / Arduino pin 10
Left sensor GND / Arduino GND
Left sensor VCC / Arduino Vin
Left buzzer + / Arduino pin 7
The order of the VCCs and the GNDs is irrelevant as long as they are all connected. My Arduino Uno exposes up to three grounds and I am using Vin pin as a power supply for one of the sensors, supplying the other sensor from the 5V pin.
Other Arduino versions are more or less limited in pins (i.e. Arduino Micro only exposes two grounds), so I decided to make an extra ground by setting the pin 2 to LOW.
Figure 9: Connecting the ultrasound modules and the power
Figure 10: Connecting the modules, the power and the buzzers
Step 6: Powering up the system
The power supply in automobiles is tricky. Although the car battery provides 12 DC Volts (the voltage regulator may overheat and damage the Arduino board if you are using more than 12V), it is connected to an alternator. When starting up the engine the alternator can create huge current peaks and fry any electronic device connected to it. It is called load dump. You can read more about it here.
For this reason, it is best to use an intermediate protective circuit between your Arduino and the car’s power supply. One option is the DIY assembly of a voltage regulator, but its design surpasses the scope of this article, so I opted to recycle an old phone adapter.
I used a USB female adapter cable. I had to replace the end of the USB adapter and attach it to the female adapter. Alternatively, I could have removed the Arduino’s female pin headers and attached all wires.
The question here is: Which one of the four wires goes where? Please see the image below.
Figure 11: USB type B. When supplying power through the USB only two wires are needed.
In my Arduino Uno the USB connection is type B. Other models have other types of USB connections, so you will have to check yours and adjust to your needs.
Finally, we need to find a power source for our adapter. There are several options:
Cigarette lighter receptacle (located a little too far away)
Reversing indicator lamp (very convenient but a bit complicated given the operation of my door)
Rear windshield wiper (less elegant than the reverse indicator, but also convenient in case I want to disable it)
I was lucky enough to find a connector in between all the wires that ran behind the plastic panel. I checked the voltage and …Voila!
Figure 12: Finding the power and polarity in a stray connector within my car
In this tutorial, we developed a fairly simple, cost-effective Arduino-based assistive parking system with car sensors that alert a driver when they get too close to the cars behind them (rear side). Using two ultrasonic proximity sensors and two Piezo buzzers, the system warns the driver with acoustic sounds, with the frequency between the sounds indicating the distance from an obstacle.
We created a real-life application using Arduino and explored the boundaries between the proof-of-concept prototype, the user experience prototype and the functional prototype. In a future tutorial, we may expand on the concepts we discussed here today to build an improved version of the assistive parking system with better features and functionalities. What do you think?
If you have any comments or questions, please leave them for us at Google +. Follow us there; we will be posting the next tutorial soon.
Rab holds PhD in Computational Physics and has been a dedicated researcher in the areas involving computing, automation and complex systems. Rab also has experience in electronics, robotics and digital manufacturing.