How to use HC-SR04 Ultrasonic Sensor with Arduino

The HC-SR04 Ultrasonic Sensor is one of the most popular and affordable tools to measure distance without physical contact. Whether you’re building a robot that avoids obstacles or a smart parking assistant, this sensor is your go-to choice. In this blog, we’ll explore how to use the HC-SR04 ultrasonic sensor with Arduino, learn how it works, how to wire it, and how to write Arduino code to accurately measure distances in centimeters and inches.

How to use Ultrasonic Sensor HC-SR04 with Arduino

How Does HC-SR04 UltraSonic Sensor Work?

Ultrasonic sensor is use to calculate the distance. It use ultrasonic waves to measure the distance. The ultrasonic wave can hit the object and and come back, ultrasonic sensor can use time to calculate the distance. HC-04 consists of two ultrasonic transducers. One can transmit ultrasonic waves and the other acts as receiver.

Working of HC-SR04 Ultrasonic Sensor
  • The Trig pin sends a short 10µs pulse.
  • This triggers the sensor to emit an ultrasonic wave at 40kHz.
  • When the sound wave hits an object, it reflects back.
  • The Echo pin goes HIGH and the time it stays HIGH corresponds to the distance.

Formula to Calculate Distance:

Distance (cm) = Duration (µs) / 58

Specifications of HC-SR04 UltraSonic Sensor:

FeatureValue
Operating Voltage5V DC
Measuring Range2 cm to 400 cm
Measuring Angle~15°
Accuracy±3 mm
Interface4-pin (VCC, GND, Trig, Echo)

Components Required for connecting Ultrasonic Sensor with Arduino:

  • Arduino UNO (or any compatible board)
  • HC-SR04 Ultrasonic Sensor
  • Breadboard
  • Jumper Wires
  • Optional: 16×2 I2C LCD for display

Pinout of HC-SR04 Ultrasonic Sensor:

Pinout of HC-SR04 Ultrasonic Sensor
  • Vcc –> 5V
  • Gnd –> Gnd
  • Trigger –> 2
  • Echo –> 3

Circuit Diagram for Connecting Ultrasonic Sensor with Arduino:

HC-SR04 PinArduino Pin
VCC5V
GNDGND
TrigPin 3
EchoPin 2
Circuit Diagram for Connecting HC-SR04 Ultrasonic Sensor with Arduino

Code for Using Ultrasonic Sensor HC-SR04 with Arduino:

#define trigPin 2
#define echoPin 3
long dur;
int dist = 0;

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

}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  dur = pulseIn(echoPin, HIGH);
  dist = dur * 0.034 / 2;
  Serial.print("Distance : ");
  Serial.print(dist);
  Serial.println("cm");

}

Video Tutorial for Using Ultrasonic Sensor HC-SR04 with Arduino:

Applications Ultrasonic Sensor:

  • Smart Car Parking Systems
  • Obstacle Avoidance Robots
  • Automatic Doors
  • Distance Measurement Tools
  • Level Sensing (water, grains, etc.)

Troubleshooting of Ultrasonic Sensor with Arduino:

ProblemSolution
Always reads 0 cmCheck wiring, especially Trig/Echo
Random valuesAdd delay between readings
Too slow or unstableUse capacitor for VCC or shield wires
Echo time too longObject might be too far or soft

Conclusion:

The HC-SR04 ultrasonic sensor is an incredibly useful module for any Arduino enthusiast. It helps you bring interactive and intelligent functionality to your projects with accurate distance measurement using nothing but sound.

From robotics to smart automation systems, it’s a must-have sensor in your toolkit!

Need Help in Setup of Ultrasonic Sensor with Arduino?

If you need any Help or Assistance for Setup of Ultrasonic Sensor with Arduino, with Modifications or Customization then you can contact us through WhatsApp. We can deliver you this Project in the Following Ways.

Learn More about the services we offer.

1 Comment

Leave a Reply

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

Facebook
YouTube