Hello everyone, in this blog, I will tell you what HC-SR04 is, its features, and detailed use. Come easy! 😁

What is and How Does It Work

HC-SR04 is an ultrasonic sensor, i.e. Sound Navigation and Ranging. Sonar is a sensor that calculates the distance to the opposite object or object using communication. Sonar allows us to calculate the distance to the object using sound waves. It can measure distances between approximately 2cm and 400cm.

Here’s how it works: When a pulse is applied to the trigger pin with a duration of at least 10μS (10 microseconds), the signal starts. In response, the sensor transmits a burst of sound consisting of 8 pulse waves at 40KHz. These 8 pulse waves come out with the device’s unique sound signature, allowing the receiver to distinguish incoming special sound waves from ambient noise.

After the 8 ultrasonic sound waves exit the transmitter and hit the object and come back and reach the ECHO pin, the ECHO pin becomes HIGH to start forming the beginning of the signal. If the outgoing sound waves do not come back, the ECHO signal times out after 38mS (38 milliseconds) and decreases. Thus, a pulse wave of 38mS indicates that there are no obstructions in the sensor range. You can review the gif below to understand it better. 🙂

If you don't see the image, please let know in the comments!

If these pulses are reflected back, the echo wave decreases as soon as it received the signal. This produces a pulse ranging in width from 150μS (150 microseconds) to 25mS depending on the time it takes for the signal to be received. You can see it in the figure below.

If you don't see the image, please let know in the comments!

The time elapsed between the departure and arrival time of the received sound wave pulses is used to calculate the distance to the object.

Features and Pin Outputs

If you don't see the image, please let know in the comments!

5V: It provides power for the HC-SR04 Ultrasonic distance sensor, to which we connect the 5V pin to the Arduino.

Tring: It is used to trigger ultrasonic sound signals.

Echo: It produces a BPM when the reflected signal is received. The length of the pulse is proportional to the time taken to detect the transmitted signal.

GND: It is grounding line.

Operating VoltageDC 5V
Operating Current15mA
Operating Frequency40KHz
Maximum Measurement4m
Minimum Measurement2cm
Measuring Range3cm
Measuring Angle15 degree
Trigger Input Signal10µS TTL
Size45x20x15mm

HCSR-04 Datasheet

You can view the datasheet here.

Let’s move on to the Arduino connection shape without going into the more detailed technical part.

Connection HCSR-04

If you don't see the image, please let know in the comments!

Coding

int trigPin = 13;
int echoPin = 12;
long distance;
long duration;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.println(distance);
}

Once you’ve installed your code, open the serial monitor from the IDE and set it to 9600 and the values will start coming in! 😊

You can make your opinions, suggestions, and ideas in the comments section! Good code! 😉