top of page

HC-SR04 Ultrasonic Sensor Guide with Arduino Interfacing

Updated: Mar 4


Overview of ultrasonic sensor


The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object.


An ultrasonic sensor generates high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver as shown in below figure.




By measuring the time required for the echo to reach to the receiver, we can calculate the distance. This is the basic working principle of Ultrasonic module to measure distance.

 

Ultrasonic HC-SR-04 Module




HC-SR-04 has an ultrasonic transmitter, receiver and control circuit.


In the ultrasonic module HCSR04, we have to give trigger pulse, so that it will generate ultrasound of frequency 40 kHz. After generating ultrasound i.e. 8 pulses of 40 kHz, it makes echo pin high. Echo pin remains high until it does not get the echo sound back. So the width of echo pin will be the time for sound to travel to the object and return back. Once we get the time we can calculate distance, as we know the speed of sound.


Ultrasonic Sensor HC-SR04 can measure up to range from 2 cm - 400 cm.

 

HC-SR04 Pin Description




 

VCC: +5 V supply


TRIG: Trigger input of sensor. Microcontroller applies 10 us trigger pulse to the HC-SR04 ultrasonic module.


ECHO: Echo output of sensor. Microcontroller reads/monitors this pin to detect the obstacle or to find the distance.


GND: Ground

 

Ultrasonic Sensor HC-SR04 Working Principle



 

  1. We need to transmit trigger pulse of at least 10 us to the HC-SR04 Trig Pin.

  2. Then the HC-SR04 automatically sends Eight 40 kHz sound wave and wait for rising edge output at Echo pin.

  3. When the rising edge capture occurs at Echo pin, start the Timer and wait for falling edge on Echo pin.

  4. As soon as the falling edge is captured at the Echo pin, read the count of the Timer. This time count is the time required by the sensor to detect an object and return back from an object.

 

How to calculate distance for Ultrasonic HC-SR04 Module?


We know that,



The speed of sound waves is 343 m/s.


So,



Total distance is divided by 2 because signal travels from HC-SR04 to object and returns to the module HC-SR-04.

 

Alternate options for HC-SR04 Ultrasonic Sensor 


  • Maxbotix Ultrasonic Sensors: These sensors offer a wider range of detection and higher accuracy compared to the HC-SR04. They are available in different models with varying ranges and beam patterns.

  • Sharp Infrared Sensors: These sensors use infrared light to measure distances and are available in a range of detection distances. They are suitable for use in low light conditions and have a faster response time compared to ultrasonic sensors.

  • Lidar Sensors: These sensors use laser light to measure distances and provide high accuracy and precision. They are commonly used in robotics and automation applications and are available in different models with varying ranges and resolutions.

  • Time-of-Flight (ToF) Sensors: These sensors use infrared light to measure distances and are suitable for use in applications that require high accuracy and fast response times. They are available in different models with varying ranges and resolutions.

  • Ultrasonic Ranging Module HC-SR05: This is a similar module to the HC-SR04 with minor differences, it has a wider detection range of up to 4.5 meters and a higher accuracy.

 

HC-SR04 Ultrasonic Sensor interface with Arduino



 

HC-SR04 Ultrasonic Sensor code for Arduino


/*
  Ping))) Sensor

  This sketch reads a PING))) ultrasonic rangefinder and returns the distance
  to the closest object in range. To do this, it sends a pulse to the sensor to
  initiate a reading, then listens for a pulse to return. The length of the
  returning pulse is proportional to the distance of the object from the sensor.

  The circuit:
            - +V connection of the PING))) attached to +5V
            - GND connection of the PING))) attached to ground
            - SIG connection of the PING))) attached to digital pin 7

  created 3 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Ping
*/

// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH pulse
  // whose duration is the time (in microseconds) from the sending of the ping
  // to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;
}

 

The output of the code displays the distance of an object from the sensor in both inches and centimeters in the serial monitor. 


 

Examples of Ultrasonic Module interfacing



3 views0 comments

Comments


bottom of page