728x90 AdSpace

­
Latest News
Sunday, February 1, 2015

Using an ultrasonic sensor

attacknid
Ultrasonic sensor modules are cheap, they use little power and with their bugged-eyed appearance, they’re the perfect accompaniment to almost any Arduino robot. Using short-burst high-frequency ‘chirps’, an ultrasonic sensor module can measure distances or ‘range find’ up to four metres and measure right down to two centimetres.
The HC-SR04 module we’re using here has two sensor components – a transmitter and a receiver. The transmitter sends out a short-burst 40kHz-tone pulse, with a microchip built into the module monitoring the time delay for the pulse to reflect back and be picked up by the receiving sensor. Based on that delay, we can work out the distance between the module sensor and the object in front of it.
Here’s how that works – sound travels at approximately 340.3 metres per second at sea level. If the module sends out a pulse and receives the echo after 11.75 milliseconds, we know the tone pulse has travelled 340.3m/sec x 0.01175secs = 4 metres. Remembering that the time delay is for the pulse to travel both directions (there and back), we can halve that result and know there is an object two metres in front of the sensor.

Simple Build

To test this out, you can build a very simple circuit by plugging the sensor module into an Arduino Uno or Freetronics Eleven board using four male-to-female dupont wires that have pins on one end (male) and pin sockets on the other (female). You can buy these on eBay for around $2 or so. Plug the sensor module into the female ends and carefully plug the male ends into the Arduino so that the module’s Vcc pin goes to the Arduino 5V pin and the GND pin goes to the Arduino’s GND pin – this will supply the module with power (the coloured-coded wires in the photo should help with this).
The module’s two centre pins are trigger (TRIG) and echo (ECHO) – connect these to the Arduino’s A0 and A1 pins, respectively. If we set A0 as an output, we can trigger the module to send a ‘chirp’. At the same time, the module’s echo pin goes digital-high (5V). When the receiver sensor picks up the return echo, the echo pin is pulled to digital-low level (0V). With this ‘echo’ output connected to the Arduino’s A1 pin set as an input, we can detect the change of voltage and calculate the delay time – and the distance. (Grab some 3M double-sided padded tape like we’ve used here to anchor the sensor module to the Arduino board and make it easier to hold while you’re testing.)
graph

Software Sketch

That’s the hardware covered, now for the software. To program the Arduino Uno, we need to write the source code or ‘sketch’ as it’s known in Arduino-world. Grab hold of the Arduino integrated development environment (IDE) fromhttp://arduino.cc/en/main/software (you just need version 1.0.5 for an Arduino Uno/Freetronics Eleven) and download my sketch from our Arduino page atwww.apcmag.com/magstuff. Download it, unzip it and copy the contents of the libraries subfolder to the same in your Arduino IDE folder. Restart the IDE and load in the ab01_ultrasonic_v1.ino sketch file. Plug the ultrasonic sensor and the USB cable into the Arduino Uno board, then flash the sketch to the board. (If using a Freetronics Eleven board, get the driver software first fromhttp://tinyurl.com/elevenusb.)
We’ve programmed the sketch to use the Serial Monitor interface – this allows the Arduino Uno board to send back data we can see within the IDE window. When you’ve flashed the sketch into the Arduino board, click on the magnifying glass at the top right of the IDE to launch the Serial Monitor.
As soon as the new window appears, set the baud rate on the bottom right to read ‘57600’. This auto-restarts the Arduino board and within a second or two, you should start seeing readings from the sensor. I’ve also coded it so that you get a graph next to the raw reading, giving you a sonar-output of the distance being measured. This is coded in the sketch using the for() loop, which counts up to the value of the distance in centimetres divided by five and prints a hyphen (-) for each count along the line. It gives us a proportional graph of what the sensor ‘sees’. We also use the special ‘sprintf’ function to fix the number of characters printed for the raw data, so we keep a straight graph axis.
hcsr04

wires

How Arduino Code Works

Have a look at the Arduino sketch code and you’ll notice there are two main sections or ‘methods’ – setup() and loop(). The Arduino chip is pre-programmed to recognise these two methods in a sketch – the setup() method runs once and the loop() method loops continuously thereafter. You can have any number of other methods, but the sketch must have setup() and loop() methods, otherwise you’ll get errors. By having our range-finding code inside the loop() method, we know we’ll get a continuous stream of measurements. We use the delay(250) command to provide a 250-millisecond delay each time the loop() method runs, enabling us to roughly regulate the module to make four measurements per second. (Reduce the delay time here to take more frequent measurements).

NewPing Library

What makes the sensor module so easy to use is the NewPing library, written by Tim Eckel. It wraps up all the complex calculations into an easy-to-use set of commands. In fact, once we’ve created the sonar object at the beginning of the sketch, there’s only one command we need to grab the distance and that’s distance=sonar.ping_cm(). It returns the distance in centimetres and we store the result in an integer variable called distance. We’ve included the NewPing library inside the zip file, so make sure you copy it to your Arduino IDE’s libraries subfolder and restart the IDE before you begin coding.
ide

Reading Dropouts

Like all echo-return style rangefinders, the HC-SR04 loses sensitivity the greater the distance you’re trying to measure. What’s more, they’re all reliant on the reflectivity of the object. If there’s no echo, there’s nothing to measure and you get a zero distance. Rather than display that, I’ve coded the sketch so that it only displays valid measurements (essentially, anything greater than zero). This lack of sensitivity and distance is one of the reasons why Google’s driverless car and other similar projects use high-sensitivity ‘LiDAR’ laser measurement systems instead, but while they’re infinitely more sophisticated, they’re almost infinitely more expensive too!

Try It Out

Ultrasonic sensor modules are incredibly cheap – you can find them on eBay for under $2 including shipping from Hong Kong. They’re also an easy way to provide sensory feedback to your Arduino projects. A free-roaming Attacknid, anyone?


  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: Using an ultrasonic sensor Rating: 5 Reviewed By: Unknown