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 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.
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
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 Voltage | DC 5V |
Operating Current | 15mA |
Operating Frequency | 40KHz |
Maximum Measurement | 4m |
Minimum Measurement | 2cm |
Measuring Range | 3cm |
Measuring Angle | 15 degree |
Trigger Input Signal | 10ยตS TTL |
Size | 45x20x15mm |
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
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! ๐