Distance Sensing using the Arduino and HC-SR04 Ultrasonic Sensor

In this tutorial, we will see how to use the Arduino along with and HC-SR04 ultrasonic/sonar sensor for distance measurement and log the measured distance on the Serial Monitor. This can be used in a variety of projects, such as a parking sensor, burglar alarm, automatic door and obstacle avoiding robots. We will see the code for using an ultrasonic sensor and its circuit with the Arduino. Let’s get started!

What is it and how does it work?

Image result for HCSRo4

An HC-SR04 is a SONAR sensor (SOund NAvigation and Ranging, click here to know more) that uses ultrasonic sound waves of a known frequency to measure distance. It achieves by producing a directional pulse of sound from the transmitter that starts traveling forward. When this sound reaches an object, it gets reflected back and reaches the receiver. The time taken for this to happen is recorder by the Arduino, which then uses it calculate the distance (since the speed of sound is known). This is similar to how a bat or a submarine measures distance/observes its surroundings. Shown below is a diagram that represents this process.

HC-SR04 Sensor Measuring Distance

Apart from SONAR sensors (that use sound), distance measurement is also done using light (LIDAR) and radio waves (RADAR). While these are faster, they require more expensive controllers and more expensive themselves. They might also not work in certain light conditions (most IR sensors begin causing trouble under sunlight). To read more about different types of distance sensors and which one is right for you, click here.

Even in ultrasonic sensors, there are many different types of sensors available, but what makes the HC-SR04 stand out is its form factor, simple interface and cost. To operate it, a ten microsecond HIGH pulse is sent to the trigger (labelled trigg) pin. A timer than starts counting the amount of time it takes for the echo (labelled echo) pin to produce a HIGH pulse. The distance can be calculating using this time interval.

The sensor has a Field of view of 30 degrees (15 degrees on either side) and a range of 2 to 450 cm, which is shown below.

HC-SR04 Range and FOV

Let us now see how to build the circuit and code the Arduino to use this sensor.

Gathering the parts

In order to complete this project, you will need the following parts.

  • Arduino UNO or equivalent (along with programming cable)
Image result for arduino uno
Arduino UNO or equivalent
  • HCSR04 sensor
HCSR04 sensor
  • Connecting Wires
Jumper Wires
  • Breadboard (optional)
Breadboard

Building the Circuit

In this step, we will see how to connect the HC-SR04 to the Arduino. Start by connecting the VCC and GND on the sensor to 5v and GND on the Arduino. Then connect the trigger and echo pins of the sensor to digital pins 5 and 4 on the Arduino. You can any other digital pins, just make sure to substitute them in the code. The connections are shown below.

HC-SR04 Circuit with Arduino

This completes the circuit. We can now move on to programming the Arduino.

Programming the HC-SR04 code

In this step, we will see how to program the Arduino to control the HC-SR04. If you are not familiar/comfortable with the Arduino IDE, click here to see a quick guide.

Start by defining the trigger and echo pin as shown below.

#define TRIG    5
#define ECHO    4

Next, set the pin modes of the trigger and echo pins, and initializing the Serial Interface in the setup function. If you are not familiar with the Serial monitor, click here.

void setup() {
    pinMode(TRIG, OUTPUT);
    pinMode(ECHO, INPUT);

    Serial.begin(9600);
}

Finally, in the loop, create a 10 microsecond HIGH pulse on the trigger. To measure the time taken for the pulse to return, we will use the pulseIn function that is built into the Arduino Language. This takes a pin to wait on (in our case, the echo pin), the state to wait for (in our case, HIGH) and returns the number of microseconds before the pin reaches the desired state. Assuming the speed of sound to be 34,000 centimeters per second, we can multiple this by 0.034 to get the two-way distance in cm. We can further divide this by 2 to get the one-way distance.

void loop() {
    digitalWrite(TRIG, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG, LOW);

    long dist = (pulseIn(ECHO, HIGH)/29)/2;

    Serial.print(dist);
    Serial.println(" cm");
}

Note that in the above code, I have divided the duration by 29 instead of multiplying it by 0.034. These operations are the same. The completed code should look as follows.

#define TRIG    5
#define ECHO    4

void setup() {
    pinMode(TRIG, OUTPUT);
    pinMode(ECHO, INPUT);

    Serial.begin(9600);
}

void loop() {
    digitalWrite(TRIG, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG, LOW);

    long dist = (pulseIn(ECHO, HIGH)/29)/2;

    Serial.print("Distance: ");
    Serial.println(dist);
}

You can verify and upload this to your Arduino and open the Serial Monitor once this is done to see the measured distance. To verify this, you can keep a ruler in front of the sensor (with the 0 mark right below the sensor) and keep an object at known distances and seeing the output. You can move the object forward and backward and see the distance change as well.

Additional Resources

Wikipedia article on sonar

Getting started with Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s