Ultrasonic Sensor

Find distance to the nearest obstacle

In the previous program, we saw how a combination of Infrared Transmitter and a Receiver LED is used to check if a line is black or white. In this section, we will see how to read values from an Ultrasonic sensor.

To use an ultrasonic sensor, first connect a HC-SR04 Ultrasonic Sensor provided in the kit. Align and insert into the four pin female header which is soldered in front of the board. HC-SR04 is the most popular sensor for measuring distance and making obstacle avoiding robots.

HC-SR04 is an easy to use distance measuring sensor which has a range from 3cm to 400cm. The sensor is composed of two ultrasonic transducers. Transmitter outputs ultrasonic sound pulses and the receiver listens to reflected waves. It’s basically a mini SONAR which is used in submarines for detecting underwater objects.

The sensor has 4 pins: VCC, GND, Trigger and Echo. The Trigger pin sends the ultrasound wave from the transmitter, and Echo pin listens to the reflected waves.

Ultrasonic Sensor emits an ultrasound at 40 000 Hz (40 kHz) which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound we can calculate the distance.

In order to generate ultrasound we need to set the Trig pin on a High State for 10 µs. That will send out an 8 cycle ultrasonic burst which will travel at the speed of sound. The Echo pins goes high right after that 8 cycle ultrasonic burst is sent, and it starts listening to that wave to be reflected from an object.

If there is no object or reflected pulse, the Echo pin will time-out after 38ms and get back to low state. If there is an obstacle, the Echo pin will go high sooner than 38ms and depending on the amount of time it was high, we can calculate the distance from the nearest obstacle.

Lets implement this in a program and check how it works:

/*
  Ultrasonic Sensor: Sends a ping and calculates distance to obstacle in cms

  At VEEROBOT, we invest time and resources providing this open source code,
  Please support VEEROBOT and open-source hardware by purchasing products
  from us @ http://veerobot.com
  -----------------------------------------------------------------------------
  You are free to redistribute it and/or modify it under the terms of the GNU
  Lesser General Public License as  published by the Free Software Foundation,
  either version 3 of the License, or (at your option) any later version.

  This Code is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  See <http://www.gnu.org/licenses/>
*/

#include "config.h"

long duration;  // duration of sound wave travel
int distance;   // distance measurement

void setup() {
  Serial.begin(9600);
  pinMode(TRIG, OUTPUT); // Sets the trigPin as an Output
  pinMode(ECHO, INPUT); // Sets the echoPin as an Input
}

void loop() {
  int obdistance = pingInCm();
  Serial.print("Distance in CM : ");
  if (obdistance > 40) Serial.println("Out of bound");
  else Serial.println(obdistance);
  delay(300);
}

int pingInCm() {
  // Clears the trigPin condition
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(ECHO, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (to and fro)
  return distance;
}

Last updated