Lab: Ultrasonic Distance Sensor

Introduction

The HC-SR04 distance sensor is an inexpensive and ubiquitous distance sensor that gives reasonably reliable distance readings in the 2cm – 4m range. In this lab, you’ll learn how to use this sensor with an Arduino microcontroller. There are dozens of similar tutorials for this sensor all over the web.

What You’ll Need to Know

To get the most out of this Lab, you should be familiar with the following concepts beforehand. If you’re not, review the links below:

Things You’ll Need

Figures 1-4 show the parts you’ll need for this exercise. Click on any image for a larger view.

Photo of an Arduino Nano 33 IoT module. The USB connector is at the top of the image, and the physical pins are numbered in a U-shape from top left to bottom left, then from bottom right to top right.
Figure 1. Microcontroller. Shown here is an Arduino Nano 33 IoT, but an Uno will work as well
Photo of flexible jumper wires
Figure 2. Jumper wires. You can also use pre-cut solid-core jumper wires.
Photo of a solderless breadboard
Figure 3. A solderless breadboard
Ultrasonic sensor model HC-SR04
Figure 4. An Ultrasonic sensor, model HC-SR04. The sensor has two cylindrical transducers, and four pins at the bottom of the board, labeled from left to right: Vcc, Trig., Echo, Ground.

How the Sensor operates

The HC-SR04 sensor operates by sending out a 40KHz ultrasonic signal and waiting for it to bounce off the subject and return to the sensor. Since the speed of sound in air is reasonably constant, you can estimate the distance to the subject by reading the time taken for the sound to return.

To operate the sensor, you send a 10-microsecond low-to-high pulse on the sensor’s trigger pin. This causes the sensor to send out the ultrasonic signal. Then you measure length of the pulse on the echo pin to know how long the signal took to return.

The Circuit

This sensor operates on 5V. With the Uno, that’s the default supply voltage of the board. If you are using a 3.3V board like the Nano 33 IoT, you’ll need to make sure you’re powering it with 5V. You can get that from the USB input, or from the external voltage input if you are using a 5V source. You’ll need to attach the sensor’s voltage input to the VUSB pin, which should output 5V when attached to USB, or to the Vin pin if you are powering the Nano 33 IoT with 5V.

Figures 5 and 6 show the schematic diagram and breadboard layout of the sensor attached to an Arduino Nano 33 IoT.

Ultrasonic sensor attached to an Arduino Nano 33 IoT, breadboard view
Figure 5. Breadboard view of an Arduino Nano 33 IoT attached to an HC-SR04 ultrasonic sensor. The sensor’s Trigger pin is attached to the Arduino’s digital pin 9, and the sensor’s Echo pin is attached to the Arduino’s digital pin 10. The sensor’s ground is connected to the Arduino’s ground, and the Vcc is attached to either the Arduino’s Vin pin (if the Arduino is powered by USB or other 5V source), or the VUSB pin (if the Arduino is powered by a higher voltage source).
Figure 6. Schematic view of an Arduino Nano 33 IoT attached to an HC-SR04 ultrasonic sensor.

Figures 7 and 8 show the schematic diagram and breadboard layout of the sensor attached to an Arduino Uno. Since the Uno operates on 5V, you can use the +5V output pin from the Uno to power the sensor.

Breadboard view of an Arduino Uno attached to an HC-SR04 ultrasonic sensor.
Figure 7. Breadboard view of an Arduino Uno attached to an HC-SR04 ultrasonic sensor. The sensor’s Trigger pin is attached to the Arduino’s digital pin 9, and the sensor’s Echo pin is attached to the Arduino’s digital pin 10. The sensor’s ground is connected to the Arduino’s ground, and the Vcc is attached to the Arduino’s +5V out pin.
Schematic view of an Arduino Uno attached to an HC-SR04 ultrasonic sensor.
Figure 8. Schematic view of an Arduino Uno attached to an HC-SR04 ultrasonic sensor. The sensor’s pins are connected to the microcontroller as described in Figure 7.

The Code

The sketch to read the sensor follows the instructions described above. First you take the trigger pin low. Then you take it high to initiate the trigger pulse, then wait ten microseconds. Then you take it low again, ending the trigger pulse. Then you use the pulseIn() command to measure the length of the pulse on the echo pin. After that, you do the math to convert the pulse time to centimeters, and you’re done.

// set up pin numbers for echo pin and trigger pins:
const int trigPin = 9;
const int echoPin = 10;

void setup() {
  // set the modes for the trigger pin and echo pin:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);

}

void loop() {
  // take the trigger pin low to start a pulse:
  digitalWrite(trigPin, LOW);
  // delay 2 microseconds:
  delayMicroseconds(2);
  // take the trigger pin high:
  digitalWrite(trigPin, HIGH);
  // delay 10 microseconds:
  delayMicroseconds(10);
  // take the trigger pin low again to complete the pulse:
  digitalWrite(trigPin, LOW);

  // listen for a pulse on the echo pin:
  long duration = pulseIn(echoPin, HIGH);
  // calculate the distance in cm.
  //Sound travels approx.0.0343 microseconds per cm.,
  // and it's going to the target and back (hence the /2):
  int distance = (duration * 0.0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  // a short delay between readings:
  delay(10);
}

Clear the Sensing Zone

All distance sensors send out their signal and listen for a response in a particular sensing field of view. Figure 9 shows a distance sensor’s typical field of view. The field moves out from the sensor in a cone. It is smaller nearest the sensor, and gets wider as distance from the sensor gets larger. The nearest object in the field of view is the one detected. A person standing outside the field of view cannot be detected by the sensor.

A distance sensor shown from above, with the field of view drawn in.
Figure 9. A distance sensor shown from above, with the field of view drawn in. A near person outside the field of view is not detected, while a farther person inside the field of view is detected.

Similarly, an object in the field of view can be detected whether you intend it to or not. Figure 10 shows an ultrasonic sensor sitting on a table. The field of view of the sensor extends out from the sensor, and intersects the table a few centimeters from the sensor. This stops the sensor from picking up more distant targets.

 A photo of an ultrasonic sensor sitting on a table.
Figure 10. A photo of an ultrasonic sensor sitting on a table. The field of view of the sensor is blocked by the table a few centimeters from the sensor.

For more on distance sensors, see Distance Sensors: the Basics.