empowering creative people

How to Use Ultrasonic Sensors

ECHO…echo…echo…echo…

We’ve all had fun shouting into a cave or large space and hearing our voice coming back to us at some point during our childhood. And if you’re like me, a fully grown adult who still does it now and then, it’s still pretty fun. The reason behind this is fairly simple. A sound is merely a wave of pressure moving through the air at approximately 340 metres/second (m/s). It’s a mechanical wave, which means that it will bounce off objects. The further away from you the objects are, the longer it takes for your voice to reach them, and then to bounce back to your ears. This principle has been understood for a long time now, and many things around us are built on that same principle. Including ultrasonic sensors.

Ultrasonic sensors, whilst fancy and high-tech, are fairly simple devices. They emit sounds waves, wait for them to bounce off objects, then measure the time it took for the wave to be picked up by the receiver. If you half that time (it has to go out and back), you get the time it took for the wave to reach the object you’re measuring. By using the fact that sound travels through air at 340m/s, you can accurately measure the distance from an object.

Ultrasonic range detectionIf you’re observant, you may remember that ultrasonic is a term for sound waves that are higher in frequency than the human ear can detect. So whilst these sensors use sound wave, you can’t hear it at all.

So that’s how they work, now let’s take a look at using them. Today we’ll be going through how to use the venerable HC-SR04 ultrasonic sensor with an Arduino or Arduino-compatible board. The HC-SR04 is a low-cost sensor ($5) which has a range of 2cm-500cm and a resolution of 0.3cm. It’s not the most precise sensor of its type with some ultrasonic sensors being upwards of $100, but for most applications, it works pretty well.

Ultrasonic sensors are superior to IR sensors because they aren’t affected by smoke or black materials, however, soft materials which don’t reflect the sonar (ultrasonic) waves very well may cause issues. It’s not a perfect system, but it’s pretty good.

Downloading the NewPing Library

You can find the library reference information, along with a download link here. It’s incredibly easy to add libraries to your IDE. Just go to the ‘Sketch’ drop down menu > Include Library > Add .ZIP Library and locate the .zip file that you downloaded.

The Gear

To follow this tutorial you’ll require the following:

We’ll be using the LEDs and resistors to create a simple project with the HC-SR04, however, if you want to use the sensor for your own project, feel free to omit those.

The Circuit

The circuit setup for this process is incredibly simple. Follow the breadboard layout below, and connect the pins to any pins on your favourite Arduino board, and make sure that you change the pin labels in the code.

HC-SR04 ultrasonic sensor circuit

The Code

The code required to use the HC-SR04 is quite simple. Different sensors may operate slightly differently, however, you should be able to find a library available for different ultrasonic sensors. Most microcontroller platforms (Particle, Raspberry Pi etc.) will have libraries available too.

The best Arduino library to use right now is the NewPing library which makes the most of the HC-SR04 and gives measurements accurate enough for most projects. However bear in mind that the product specifications state a 15 degree angle of operation which means that the sensor isn’t a beam style sensor, it has a cone of operation which it will pick up objects inside, so if you’ve got the sensor mounted flat on a desk, it’s going to measure the desk, rather than something else further away.

#include <NewPing.h>
 
#define TRIGGER_PIN  2
#define ECHO_PIN     3
#define MAX_DISTANCE 500

const int ledB = 4;
const int ledR = 5;
const int lowDist = 10;
 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
 
void setup()
{
  Serial.begin(9600);
  pinMode(ledB, OUTPUT);
  pinMode(ledR, OUTPUT);
}
 
void loop()
{
  if(sonar.ping_cm() >= lowDist || sonar.ping_cm() == 0)
  {
    digitalWrite(ledB, HIGH);
    digitalWrite(ledR, LOW);
  }
  if(sonar.ping_cm() < lowDist && sonar.ping_cm())
  {
    digitalWrite(ledB, LOW);
    digitalWrite(ledR, HIGH);
  }
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
  delay(50);
}

And that’s all there is to it. There are plenty of applications for ultrasonic sensors such as simple object/collision detection, to rangefinders. Remember that whilst we discussed using the Arduino library NewPing, it will also work on other platforms with other libraries. Happy making!

ECHO…echo…echo…echo… We’ve all had fun shouting into a cave or large space and hearing o...

Have a question? Ask the Author of this guide today!