Parking Distance Sensor & Alarm
I’ve been trying to build a tracking system for the localisation of objects (don’t ask). I’ve been experimenting with a heap of different methods for doing this -- and using a Sharp IR distance sensor is one way of achieving this.
Unfortunately I couldn’t do exactly what I wanted with the sensor, but the offshoot was that I was able to create a neat little distance sensor and alarm -- the kind you find in the back of cars to prevent you from banging into something. Although it won’t be used for my project, I thought it a good idea to write it up to hopefully inspire others thinking of getting involved in digital electronics.
I will preface this blog by saying it’s going to get rather geeky, so if you have a penchant for up-turned collars then this one isn’t for you.
Shopping List
The bits you’ll need for this tutorial are:
- Arduino micro controller
- Sharp GP2yOA IR sensor and cables
- 16x2 LCD Screen
- Piezoelectric buzzer
- Some wire
All up you’re looking at around $70AUD.
I’m using the open source Arduino micro-controller because they’re readily available, cheap and because they’re open source, there is a huge community of people using them. That said, any microcontroller could be used, but you’ll have to adjust the source code accordingly.
Wire it up.
The first thing to do is connect your bits together. If you’ve never used an Arduino before, I suggest having a go at the Arduino basic tutorials first so you can get the hang of it.
Connect up the Sharp IR Sensor to your Arduino, connect the sensor GND (black) to Arduino GND, VCC (red) to +5V and Vo (yellow) to Analog input pin 0.
Connect the LCD screen following the diagram over at Arduino.cc and finally, connect the buzzer with the red lead to digital output pin 6 and black lead to GND.
You should have something that looks like the image below.
The Sharp IR sensor
Sharp IR Sensors are very popular with robotics and electronics enthusiasts because they’re simple to use. There are quite a few sensors available to choose from with different range characteristics, so pick one based on your application. Acroname Robotics has a great article on the Sharp IR sensor including a handy chart to work out which one to use. I picked the GP2Y0A2YK sensor which has the attached Datasheet.
Unfortunately, sharp sensors are inherently noisy. There’s a good discussion about hardware ways to combat this noise by adding a Low Pass RC filter over at the society of robots, but we’re going to simply average out the errors in code.
Calibration
To calibrate the sensor, hook everything up and download the Arduino sketch AnalogInSerial which came bundled with your Arduino installer. This simply prints the raw reading from the Analog pin direct to the serial port. Now position your sensor at one end of a table, slightly raised (on a matchbox for example) and take analog readings every 2cm from 0 to 2m and plug them into excel and plot a graph. You should get something like this:
This is great, but the problem is arduinos don’t like floating point maths and therefore working with powers is a bit messy. We need to work out a linear relationship which will work with integer maths. Acroname robotics has a great article on how to do this. The values I calculated were:
m' = 16667
b' = 15
k = 10
Putting it together
Now that we have a calibrated Sharp Sensor we can go about building the User Experience. Like with a software project, a hardware project user interface is vital to ensuring it’s usability. I only have 32 characters to play with on my LCD screen so I decided to use the first row to display the distance in cm, and the second row to be a visual bar graph of the distance. Below I’ve attached my full source code for you to use:
Conclusion
This project is a great way to start playing with the Arduino sensor and goes to show that inspiration for projects can come from anywhere.
6 Comments to “Parking Distance Sensor & Alarm”
Leave a Reply



Even I can follow this one (what a novelty!) – but one question still nags: what’s the ‘greater good’ for this project? (your first sentence) Do you need a PC to trapise along behind or is it standalone once the chip is coded up?
Thanks for the comment! The PC is only used to supply 5V – once coded up it’s completely stand alone. You could even run it from a 9V battery without too much trouble. For the greater good, well, we can all have secrets!
Hi there. could you email me the code for this project. I’ve secured most of the parts and am anxious to give this a go. thanks!
T.
Hi Toma, thanks for commenting! There’s a link for the source code at the bottom of the article or you can click here. Just paste it into the Arduino environment.
Hi, how much better is the ir sensor then the ultra sonic ranger finder(parallax?)?
Much better, the IR sensor has a about a 7deg divergence, ultrasound is much higher (more than 30deg). The problem with the IR sensor is when you use a couple of them together, there is a minimum distance of about 10cm that you need to keep between them (for ranges around 1-2m).
Wiki has a good summary of the pros-and-cons.