Wiring HC-SR04 Ultrasonic Sensor with Arduino

Introduction

In this project, we are learning Wiring HC-SR04 Ultrasonic Sensor with Arduino, The HC-SR04 ultrasonic sensor is a popular sensor that is used to measure distance. It works by sending out a sound wave at a frequency above the range of human hearing and then timing how long it takes for the sound wave to bounce back. By knowing the speed of sound and the time it took for the sound wave to bounce back, the distance between the sensor and the object can be calculated. In this tutorial, we will show you how to use the HC-SR04 ultrasonic sensor with an Arduino.

Hardware Required

To learn How to Wiring HC-SR04 Ultrasonic Sensor with Arduino, you will need the following components

Components#Buy From Amazon
Arduino UNO1Buy Now
HC-SR04 Ultrasonic Sensor1Buy Now
Jumper Wires6Buy Now
Breadboard1Buy Now

What is HC-SR04 Ultrasonic Sensor

The HC-SR04 Ultrasonic Sensor is a popular distance-measuring sensor that uses sound waves to determine the distance between the sensor and an object. The sensor composes of an ultrasonic transmitter, which sends out a high-frequency sound wave, and a receiver, which receives the sound wave after it bounces off an object.

The HC-SR04 sensor widely uses in robotics, automation, and other projects where distance measurements require. It is easy to use, affordable, and provides accurate distance measurements.

The sensor has a range of up to 400 cm, and it operates at a frequency of 40 kHz. It requires a minimum of four pins to connect to an Arduino board: two for power (VCC and GND) and two for communication (Trig and Echo). When the Trig pin is set to high, the sensor emits an ultrasonic pulse, which bounces off an object and returns to the sensor. The Echo pin then receives the reflected sound wave, and the sensor calculates the distance based on the time it took for the sound wave to travel back and forth.

Pinout

Ultrasonic-Distance-Sensor-HC-SR04-Pinout

Pin Description

Pin NameDescription
VccThe Vcc pin powers the sensor, typically with +5V
TriggerTrigger pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending US wave.
EchoEcho pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back to the sensor.
GroundThis pin is connected to the Ground of the system.

Specifications

  • Operating Voltage: 5V DC
  • Quiescent Current: <2mA
  • Working Current: 15mA
  • Detection Distance: 2cm – 4m
  • Working Frequency: 40Hz

Features

  • Low cost
  • Easy to use
  • High accuracy
  • Wide range of detection
  • Compact size

Circuit Diagram

Circuit diagram of Wiring HC-SR04 Ultrasonic Sensor with Arduino is given below

How-to-Measure-distance-using-HC-SR04-ultrasonic-sensor-arduino-circuit

Circuit Connections

ArduinoUltrasonic Sensor
5VVCC
GNDGND
D8Trig
D9Echo

Working Explanations

The HC-SR04 ultrasonic sensor has four pins: VCC, GND, Trig, and Echo. The VCC and GND pins are connected to the 5V and GND pins of the Arduino respectively. The Trig pin is connected to a digital output pin on the Arduino, and the Echo pin is connected to a digital input pin on the Arduino.

To use the sensor, the Arduino sends a 10-microsecond pulse to the Trig pin. This pulse triggers the sensor to send out an ultrasonic sound wave. The sound wave travels through the air and bounces off of an object. The Echo pin on the sensor then sends a signal back to the Arduino. The time it takes for the sound wave to travel to the object and back is calculated by the Arduino. This time is then used to calculate the distance between the sensor and the object using the following formula:

distance = (time * speed of sound) / 2

where time is the time it took for the sound wave to travel to the object and back, and the speed of sound is approximately 343 meters per second.

Installing Arduino IDE Software

First, you will require to Download the updated version of Arduino IDE Software and Install it on your PC or laptop. if you Learn How to install the Arduino step-by-step guide then click on how to install Arduino Button given Blow

Code

//For more Projects: www.arduinocircuit.com

const int TriggerPin = 8; //Trig pin
const int EchoPin = 9; //Echo pin
long Duration = 0;

void setup(){
pinMode(TriggerPin,OUTPUT); // Trigger is an output pin
pinMode(EchoPin,INPUT); // Echo is an input pin
Serial.begin(9600); // Serial Output
}
void loop(){
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH); // Trigger pin to HIGH
delayMicroseconds(10); // 10us high
digitalWrite(TriggerPin, LOW); // Trigger pin to HIGH

Duration = pulseIn(EchoPin,HIGH); // Waits for the echo pin to get high
// returns the Duration in microseconds
long Distance_mm = Distance(Duration); // Use function to calculate the distance

Serial.print("Distance = "); // Output to serial
Serial.print(Distance_mm);
Serial.println(" mm");

delay(1000); // Wait to do next measurement
}

long Distance(long time)
{
// Calculates the Distance in mm
// ((time)*(Speed of sound))/ toward and backward of object) * 10

long DistanceCalc; // Calculation variable
DistanceCalc = ((time /2.9) / 2); // Actual calculation in mm
//DistanceCalc = time / 74 / 2; // Actual calculation in inches
return DistanceCalc; // return calculated value
}

Serial Monitor

The following Screenshot is a result of the serial monitor in the Arduino IDE. Of this Project.

Measure-distance-using-HC-SR04-ultrasonic-sensor-Serial-Monitor

Applications

  1. Obstacle Avoidance: Ultrasonic sensors are widely used in robots and robot cars to avoid obstacles in their path.
  2. Car Parking Sensor: Ultrasonic sensors can be used as a car parking sensor to detect the distance between the car and the obstacle while parking.
  3. Burglar Alarms: Ultrasonic sensors can detect any movement in a room and trigger an alarm to alert the occupants of a potential break-in.
  4. Depth Measurement: Ultrasonic sensors uses to measure the depth of water in a tank or a well.
  5. Proximity Detection: Ultrasonic sensors uses to detect the presence of an object in close proximity and can be used in industrial applications such as conveyor belt systems.
  6. Liquid Level Control: Ultrasonic sensors uses to monitor and control the level of liquids in tanks, which is particularly useful in the chemical and pharmaceutical industries.
  7. Distance Measurement: Ultrasonic sensors uses to measure the distance between objects accurately, making them useful in applications such as robotics and automation.
  8. Object Tracking: Ultrasonic sensors use in a radar system to detect and track the movement of nearby objects by rotating the sensor. This application is particularly useful in military and surveillance systems.

Conclusion

The HC-SR04 ultrasonic sensor is a useful sensor for measuring distance in a variety of projects. With its low cost and ease of use, it has become a popular sensor among hobbyists and professionals alike. By following the steps outlined in this tutorial, you can easily use the HC-SR04 ultrasonic sensor with an Arduino to measure distances and detect objects.

Leave a Comment


error: