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.
The bits you’ll need for this tutorial are:
- Arduino micro controller
- Sharp GP2yOA IR sensor and cables
- 16×2 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.
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:
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.