Distance Measuring using Ultra-sonic Sensor HC-SR04 using Arduino Uno Microcontroller
Description
Used for measuring distance, object sensing, motion sensing etc.
The module sends eight 40Khz square wave pulses and automatically detects whether it receives the returning signal. If there is a signal returning, a high level pulse is sent on the echo pin. The length of this pulse is the time it took the signal from first triggering to the return echo.
Also Check – Pins Description and Hardware Components on Arduino Uno
Pins
Vcc pin is given to + 5v
Trigger pin is for control
Echo is for receiving
Ground is connected to negative
Features of HC-SR04
- Working voltage is 5V DC.
- Static current is about less than 2mA.
- Output signal is in the form of Electric frequency signal, high level 5V, low level 0V.
- Sensor angle is nottl more than 15 degrees.
- Detection distance is in between 2cm-450cm.
- High precision Up to 3mm.
- Stable performance.
- Accurate distance measurement.
- High-density.
To see more about sensor : Click here
The connections are as follows:
- Vcc to 5V Pin of the Arduino.
- Gnd to Gnd Pin of the Arduino.
- Echo to Digital Pin 6.
- Trig to Digital Pin 7.

Code for Circuit Diagram
const int echoPin = 6; // defining the pins
const int trigPin = 7;
// defining variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print(“Distance: “);
Serial.println(distance);
}
If you still have any questions related to this article, then you can message me on my social media accounts.