Learn How HC-SR04 Ultrasonic Sensor Interfacing With Arduino Uno

The HC-SR04 Ultrasonic sensor is your secret weapon to measure the distance to any object in front of it, so Let’s learn how to interface HC-SR04 Ultrasonic sensor with Arduino Uno.

Further, you will learn everything you need to know about the HC-SR04 Ultrasonic sensor, how it works, how we calculate the distance, pinout, specifications, datasheet, and the code at the end of this tutorial.

So, are you ready for this learning journey?

HC-SR04 Ultrasonic Sensor

The HC-SR04 Ultrasonic Sensor is an accurate distance measurement Sensor that consists of two ultrasonic transducers, one of them working as a transmitter to send the ultrasonic pulses, and the other one (receiver) which is receiving these reflected waves to measure the distance. 

HC-SR04 Ultrasonic Sensor
HC-SR04 Ultrasonic Sensor

Well, what is Ultrasound? It’s high-pitched sound waves that are greater than 20kHz and humans can’t hear it (humans can hear sounds just in frequency between (20-20kHz).

HC-SR04 Ultrasonic Sensor can detect any object that is between 2cm to 4m away from it with 3mm accuracy and it can be operated with 5V power supply, which is perfect for many robotics projects that move and need to avoid colliding with any object. 

HC-SR04 Ultrasonic Sensor Specifications 

These are the technical specifications for the HC-SR04 Ultrasonic Sensor, take a look at the table down below.

Operating VoltageDC 5V
Operating Current15mA
Operating Frequency40KHz
Min Range2cm
Max Range4m
Accuracy3mm
Measuring Angle15-degree
Trigger Input Signal10µS TTL pulse
Dimension45 x 20 x 15mm
HC-SR04 Ultrasonic Sensor Specifications

HC-SR04 Ultrasonic Sensor Datasheet

Do you want to learn more details? you can download HC-SR04 ultrasonic sensor datasheet file down below.

HC-SR04 Ultrasonic Sensor Pinout

Let’s take a look at the HC-SR04 ultrasonic sensor pinout in this picture down below.

HC-SR04 Ultrasonic Sensor Pinout
HC-SR04 Ultrasonic Sensor Pinout

VCC: This pin is used to supply the HC-SR04 ultrasonic sensor, you can connect it with the 5V pin in the Arduino.

GND: This is a common GND pin, we will connect it with the GND pin in the Arduino.

Trig: This pin triggers the ultrasonic pulses by being kept at (High) for 10µs

Echo: This pin stays at (High) when the ultrasonic pulses are sent and it changes to (Low) when then the sensor receives the reflected signal, so we can calculate the distance by measuring the times that the Echo pin stays (High).

How Does HC-SR04 Ultrasonic Sensor Work? 

In the beginning, the trigger pin will be set at (High) for 10µs, as a result, the HC-SR04 ultrasonic sensor will transmit eight pulses of the ultrasonic burst at the speed of sound all of them at (40kHz), and the echo pin goes (High) waiting for the transmitted signal to be reflected.

If there is no object detected and the pulses don’t reflect back, then the Echo pin will be time out after (38ms) and change to the (Low).

HC-SR04 Ultrasonic Sensor Working When No Object Detected
HC-SR04 Ultrasonic Sensor Working When No Object Detected

But if the sensor receives the transmit pulses, the echo pin will be changed to the (Low) after receiving the signal immediately, then the echo pin will generate a pulse width between (150µs-25ms) depending on the taken time to receive this signal, which makes us able to determine the distance between the object and the sensor.

HC-SR04 Ultrasonic Sensor Working When Object Detected
HC-SR04 Ultrasonic Sensor Working When Object Detected

How to Calculate the Distance?

Now you need to calculate the distance between the sensor and the object, well, how are we going to do that? Don’t worry this is very simple, we will use the Speed Equation:

Distance = Speed x Time

  • Distance: This is the distance between the sensor and the object that we need to calculate.
  • Speed: The speed that we use in this equation is the speed of sound which is constant, c = 343m/s, m/ms -> c = 34.3m/ms, cm/µs -> c = 0.0343cm/µs.  
  • Time: This is the time it takes for the signal to reach the object, which is half of the time that the echo pin was (High) because the signal was sent and reflected back to the receiver, so we need to divide the pulse width for the echo pin by 2 to get the actual time, T = Δt/2.

D = (Δt/2) x c

Example (1): Let’s just say there is an object facing the sensor at a distance that we don’t know, and the pulse width that we received from the echo pin is 750µs. calculate the distance between the object and the sensor.

HC-SR04 Ultrasonic Sensor Calculating The Distance Example 1
HC-SR04 Ultrasonic Sensor Calculating The Distance Example 1

T =  750µs/2, c = 0.0343cm/µs

D = 750/2 x 0.0343

D = 12.863cm

So the distance between the object and the sensor is 12.863cm, to make sure that you understand, we will give you another example but you will solve it this time.

Example (2): Let’s say there is an object facing the sensor at a 15cm distance. calculate the pulse width that we received from the echo pin. 

HC-SR04 Ultrasonic Sensor Calculating the Distance Example 2
HC-SR04 Ultrasonic Sensor Calculating the Distance Example 2

To make sure that your answer is correct, pulse width = 874.6µs.  

HC-SR04 Ultrasonic Sensor With Arduino

Now let’s connect the HC-SR04 ultrasonic sensor with Arduino, but before that, you need to know all the required parts.

Parts Requirement

  • Arduino Uno
  • HC-SR04 ultrasonic sensor
  • Breadboard
  • Hook-up wires

Interfacing HC-SR04 Ultrasonic Sensor With Arduino

To interface HC-SR04 ultrasonic sensor with Arduino you need to connect the (VVC, GND) pins in the sensor with the (5V, GND) for the Arduino, and connect the (Trig, Echo) pins in the sensor with the digital output pins (8, 9) for the Arduino

Take a look at this picture down below to see the connection in detail.

Interfacing HC-SR04 Ultrasonic Sensor With Arduino
Interfacing HC-SR04 Ultrasonic Sensor With Arduino

HC-SR04 Ultrasonic Sensor Fritzing

This is the HC-SR04 ultrasonic sensor Fritzing file you can download it.

HC-SR04 Ultrasonic Sensor Arduino Code

Finally, this is the HC-SR04 ultrasonic sensor Arduino code so you can use it in your project, and you don’t need to download any library.

HC-SR04 Ultrasonic Sensor Arduino Code Without Library

This is the HC-SR04 ultrasonic sensor Arduino code without Library, with this code you can calculate the distance between the sensor and any object in front of it and the result will appear in your serial monitor in cm but if the object is not between (2-400)cm the result will be (Out of range).

#define Trig_Pin 8
#define Echo_Pin 9
 
float dur, dist; // (dist = distance) and (dur = duration)
 
void setup() {
  Serial.begin (9600);
  pinMode(Trig_Pin, OUTPUT);
  pinMode(Echo_Pin, INPUT);
}
 
void loop() {
   
  // To Write a pulse to the sensor trig pin
 
  digitalWrite(Trig_Pin, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig_Pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig_Pin, LOW);
 
  // TO  measure the response from the sensor echo pin
 
  dur = pulseIn(Echo_Pin, HIGH);
 
  // To calculate the distance by using the Speed Equation (Distance = Speed x Time)
  // The speed is the speed of sound which is 0.0343cm/µs
  // The Time (duration) is divided by two


  dist = (dur / 2) * 0.0343;
 
  // To send the result which is the distance to the Serial Monitor
  // If the result is between 2cm to 400cm the distance will appear in the Serial Monitor in cm
  // If the result doesn't between 2cm to 400cm the result will appear in the Serial Monitor as (Out of range)


  Serial.print("Distance = ");
  if (dist >= 400 || dist <= 2) {
     Serial.println("Out of range");
  }
  else {
    Serial.print(dist);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

This result will appear in your serial monitor if the object is 10cm away from the sensor.

HC-SR04 Ultrasonic Sensor Arduino Code Output Serial Monitor
HC-SR04 Ultrasonic Sensor Arduino Code Output Serial Monitor

Frequently Asked Questions

What Is The Disadvantage Of Ultrasonic?

There are some disadvantages to ultrasonic sensors such as: Limited range, being affected by other ultrasonic sources and temperature changes, humidity, and other environmental factors, but the ultrasonic sensor is still one of the best choices for robotics projects. 

What Is The Detection Range Of HC-SR04 Ultrasonic Sensor?

The HC-SR04 ultrasonic sensor has a detection range between 2cm to 400cm with an accuracy of about 3mm, however, the accuracy can be affected by some factors such as: size, shape, and material as well as environmental factors like temperature and humidity.

Do Ultrasonic Sensor Work In The Dark?

Of course, the ultrasonic sensor works In the dark, and don’t require any visible light, because the ultrasonic sensor uses high-frequency sound waves to detect objects and determine their distance, so it doesn’t rely on light. 

Is HC-SR04 Analog Or Digital?

The HC-SR04 ultrasonic sensor is a digital sensor, which means that the output digital signal can be read easily by a microcontroller like Arduino.

How Much Voltage Is Required For HC-SR04 Ultrasonic Sensor?

The HC-SR04 Ultrasonic Sensor requires a 5-volt power supply, so you can use the 5V pin in the Arduino and which means that you don’t need any additional power supply, but be aware if you use an additional power supply like a 9V battery that will damage the sensor.

At the end of this tutorial, it’s time to extend a grateful thank you for reading this article about Interfacing HC-SR04 ultrasonic sensor with Arduino. we hope you’ve discovered everything you needed to know.

Welcome to ROBOSANS, our mission is to simplify the learning process to start your learning journey with concise Arduino tutorials and straight to the point.

If you’re eager to dive in deeper and learn more about sensors, check out these articles below.

Interfacing Force Sensing Resistor (FSR) with Arduino

Interfacing TTP223B Capacitive Touch Sensor with Arduino

DHT11 Humidity and Temperature Sensor with Arduino

Digital Temperature Sensor with Arduino

LDR  photoresistor with Arduino

Sources:

arduino.cc

wikipedia.org

Scroll to Top