Skip to content
Home » Embedded Systems » Sensors » Ultrasonic Sensor Working Principle: HC-SR04 Timing and Code

Ultrasonic Sensor Working Principle: HC-SR04 Timing and Code

HC-SR04 ultrasonic sensor working principle diagram showing transmitter emitting 40kHz sound waves toward an object and receiver capturing the reflected echo signal
Sensors for Embedded Systems
Part 16 of 18View Full Path →

KEY TAKEAWAYS

  • Ultrasonic sensors measure distance by sending a sound pulse and measuring the echo return time
  • The HC-SR04 sends a 40 kHz pulse on the trigger pin and returns the echo duration on the echo pin
  • Distance is calculated as: distance = (echo_time * speed_of_sound) / 2
  • Maximum range is typically 2-4 meters with accuracy of about 3mm

Ultrasonic sensor’s are those sensors which is mainly used to detect object by measuring the distance of the targeted object by sending sound waves of high frequency which is not audible for humans and can be easily found in automobile safety systems i.e. anti-collision system

What is Ultrasonic Sensor

  • Ultrasonic sensor is an electronic device which generates ultrasonic sound waves to recognize the object or to estimate the distance between itself and the object
  • Ultrasonic sound waves are those waves whose frequencies are beyond audible range ( more than 20kilohertz). This ultrasonic sensors uses ultrasonic sound waves because its traveling speed in the air media is more than that of the speed of the audible sound waves
  • Ultrasonic sensors have two transducers one is transmitter and another one is for receiver attached in one head, that transmit back information about objects nearness
  • Transmitter converts electrical signal waves into ultrasonic sound waves whereas receiver converts the ultrasonic sound waves into the electrical signal waves

Working of Ultrasonic Sensor

Now let’s see figure 1  and understand the working step by step:

 
  • HC-SR04 (ultrasonic sensor module) has two transducers one is transmitter and other one is receiver
  • The transmitter firstly generates the ultrasonic sound waves of 40khz with the help of piezoelectric crystal and releases the original signal in the air medium
  • While traveling through the media,(here air)when the ultrasonic sound waves encounters an obstacle or object it bounces back
  • And, then the reflected signal is absorbed by the receiver site of the sensor
NAMEFUNCTIONS
 VCC(PIN-1)
  • Use to power ON the ultrasonic sensor
  • Usually given with 5volt
  • It consumes very less current to operate thus can be easily powered by onboard power supply such as arduino or raspberry pi
 TRIG(PIN-2)
  • This is the input trigger pin of sensor
  • Firstly it is been fed with 10microsecond pulses to start the ranging
  • Then HC-SR04 module emits 8 ultrasonic pulses at 40khz
      •  
 ECHO(PIN-3)
  • When the original signal hits the object the output Echo pin is set in HIGH state
  • And, once the reflected signal gets absorbed at the receiver site after facing an obstacle this echo pin goes low thus it is used to calculate the distance

We can also say ECHO PIN DURATION=TOTAL PROPAGATION TIME

  GND(PIN-4)
  • This is the ground pin
  • It has to be connected to the on board’s ground

Calculation of Distance

To calculate the actual distance between the sensor and the object we will use the formula DISTANCE=(SPEED*TIME)/2 here at 20 degree Celsius the speed of sound is 340m/sec  as the medium of transmission is air or  we can also say :-We are dividing it by 2 because as we are traveling the same distance twice one is the original signal traveling towards the object and other one is the reflected signal traveling from the object

Timing Diagram and it’s explanation

SR04 Timing Diagram

FROM THE ABOVE TIMING DIAGRAM OF HCSR04 LETS LOOK INTO TWO CASES:-

1. When there is no object encountered while ultrasonic wave traveling:-

  • First we will set trig on a HIGH state and send 10us pulse and transmit eight ultrasonic pulses
  • And, as no object therefore echo is 38ms (38ms because this is the time out period of echo pin in module HC-SR04 if no object detected)

2. When there is object encountered while ultrasonic wave traveling:-

  • Here again when we send 10us pulse and transmit eight ultrasonic pulses
  • And, as object detected therefore signal gets reflected back and comes back with the timing of 100us-18ms ,which is then used to determine the distance

Applications of Ultrasonic Sensor

  • Vehicle detection for car wash
Vehicle detection for car wash [Fig-3]
  • People detection such as counting
  • Robotic sensing in drones
  • Presence detection
  • Tank level

Ultrasonic sensor water tank level measurement diagram showing sensor mounted at top, incident and reflected sound waves, total height, water holding capacity, and overflow drain tube

Advantages of Ultrasonic Sensor

  • Low cost
  • Not affected by color or transparency of the object
  • Not affected by the dust
  • Greater accuracy
  • Better sensing capability for all material types

Limitations of Ultrasonic Sensor

  • Very sensitive in different temperature and humidity as traveling speed varies accordingly
  • Difficult to interpret readings from soft and curved objects
  • Needs an media to send ultrasonic waves
  • Have a limited range

Program code on Arduino for HC-SR04

Understanding how ultrasonic sensor works is essential for distance measurement and object detection projects.

const int trigPin = 9; //defining the trigger pin 
const int echoPin = 10; //defining the echo pin 
float duration; //‘duration’ will hold the length of sound wave 
float distance; //‘distance’ will store data of distance of object

void setup()
{
    pinMode(trigPin, OUTPUT); //declaring trig pin as output pin
    pinMode(echoPin, INPUT); // declaring echo pin as the input pin
    Serial.begin(9600); //serial communication takes place
}

void loop()
{
    digitalWrite(trigPin, LOW); //setting the trig pin in LOW state
    delayMicroseconds(2); //for 2microseconds
    digitalWrite(trigPin, HIGH);//setting the trig pin in HIGH state
    delayMicroseconds(10); //for 10us which emits 8pulses from the transmitter and bounces back once hit by an object in the receiver site
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);//duration is a variable that starts timing once the echo pin is high by using arduino function pulseIn()
    distance = (duration*.0343)/2; //calculating distance by putting the value of duration
    Serial.print("Distance: "); //printing the results of the distance
    Serial.println(distance);
    delay(100);
}

Related on this site

Frequently Asked Questions

How does an HC-SR04 ultrasonic sensor work?

The trigger pin is pulsed high for 10 microseconds, the sensor emits an 8-cycle 40 kHz burst, and the echo pin is held high until the burst returns. The high-time of the echo pin equals the round-trip travel time of the ultrasound pulse.

📖 Related: How to improve readings of Ultrasonic Sensor – Temperature and Humidity Compensation

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *