In this project we will learn how to interface ultrasonic sensor HC-SR04 with Arduino Uno.

Ultrasonic sensors are the sensors which work on sound waves whose frequency is greater than 20 Khz. Ultrasonic sound is inaudible to humans.

What is HC-SR04 Ultrasonic Ranging Module

The HC-SR04 is an ultrasonic sensor module that is commonly used in electronics projects for measuring distances between the sensor and an object. The sensor has two main components: an ultrasonic transmitter and an ultrasonic receiver. The transmitter sends out a high-frequency sound wave, which bounces off an object and returns to the receiver. The time it takes for the sound wave to travel back to the receiver is used to calculate the distance between the sensor and the object.

The HC-SR04 sensor module typically consists of a small circuit board with two ultrasonic transducers (one for transmitting and one for receiving), a control IC, and a few other components. 

The HC-SR04 sensor is commonly used in robotics, automation, and IoT applications for obstacle avoidance, object detection, and distance measurement. It is easy to use and widely available at low cost, making it a popular choice for hobbyists and DIY enthusiasts.

Features of HC-SR04 Sensor Module

Here are some features of the HC-SR04 ultrasonic sensor module:

  1. Range: The HC-SR04 can detect objects within a range of 3 cm to 300 cm.

  2. Accuracy: It has high accuracy, with a resolution of 0.3 cm.

  3. Ease of use: The sensor module is easy to use, and it requires only four connections to interface with a microcontroller or other electronic device.

  4. Low power consumption: The HC-SR04 operates on a voltage range of 5 V DC and has low power consumption, making it suitable for battery-powered projects.

  5. Non-contact measurement: The sensor measures distance without physical contact, making it ideal for applications where contact may damage the object being measured.

  6. Small size: The HC-SR04 is small in size, making it easy to integrate into a variety of projects.

Here are its main specifications:

Operating Voltage5V DC
Operating Current15 mA
Operating Frequency40 KHz
Min Range3 cm
Max Range300 cm
Accuracy3 mm
Measuring Angle<15°
Dimension45 x 20 x 15 mm

Pin Description of HC-SR04 Module

Pin Description of HC-SR04 Module

VCC It should be connected to 5V DC.

TrigIt’s an input to sensor, High pulse of 10µs is applied to this pin.

Echo It’s an output of sensor, High pulse at this pin indicates time to travel sound wave from transmitter to object and object to receiver. In above Image left side is transmitting side and right side is receiving side.

GNDThis pin should be connected to ground.

Working of HC-SR04

Timing Diagram of HC-SR04 Ultrasonic sensor

The HC-SR04 ultrasonic sensor works based on the principle of echolocation. The sensor sends out a high-frequency sound wave (typically 40 kHz) and measures the time it takes for the wave to bounce off an object and return to the sensor. This time can then be used to calculate the distance between the sensor and the object.

Working of Ultrasonic HC- SR04 Sensor

Here is a step-by-step explanation of how the HC-SR04 works:

  1. Triggering the sensor: To start a measurement, a 10μs pulse is sent to the trigger pin of the sensor. This triggers the sensor to emit a burst of ultrasonic sound waves.

  2. Sending the sound waves: The ultrasonic sound waves travel through the air and bounce off any objects in their path. The sensor waits for the sound waves to bounce back.

  3. Detecting the echoes: When the sound waves hit an object, they bounce back and are detected by the sensor’s receiver. The sensor generates an echo pulse on the echo pin, which indicates that the sound waves have been received.

  4. Measuring the time delay: The time between the trigger pulse and the echo pulse is measured using a timer. This time corresponds to the time it takes for the sound waves to travel to the object and back.

  5. Calculating the distance: Using the time delay measured in step 4, the distance to the object can be calculated using the following formula: Distance = (Time taken by the sound wave * Speed of sound) / 2, where, Time taken by the sound wave = Pulse width of the echo pulse, Speed of sound = 343 meters per second (at room temperature). It can be also be calculated as: Distance (in cm) = (Echo pulse time in micro second)/ 58.309

  6. Repeat: The sensor can be triggered again to take another measurement after a delay of at-least 50ms.

Applications of Ultrasonic Sensor HC-SR-04

The HC-SR04 ultrasonic sensor has a wide range of applications, including:

  1. Distance measurement: The sensor is commonly used for measuring distances in robotics, automation, and other applications that require precise distance measurements.

  2. Obstacle avoidance: The sensor can be used to detect obstacles and avoid collisions in robots, vehicles, and drones.

  3. Object detection: The sensor can be used to detect the presence of objects in industrial automation, security systems, and other applications.

  4. Level sensing: The sensor can be used to measure liquid levels in tanks, wells, and other containers.

  5. Parking assistance: The sensor can be used to help drivers park their cars by detecting the distance between the car and obstacles.

  6. Proximity sensing: The sensor can be used to detect the presence of people or objects in home automation systems, security systems, and other applications.

Overall, the HC-SR04 ultrasonic sensor is a versatile and reliable sensor that can be used in a wide range of applications.

Interfacing HC-SR04 Ultrasonic Sensor with Arduino Uno Board

Circuit diagram of Interfacing HC-SR04 Ultrasonic Sensor with Arduino Uno Board

Component List

NameQuantityComponent
Arduino UNO1Arduino Uno R3
HC-SR041Ultrasonic Distance Sensor
LCD1LCD 16 x 2
Pot110 kΩ Potentiometer
Resistance11 kΩ Resistor

In the above Diagram there is an Arduino Uno Board connected to an Ultrasonic sensor and a 16×2 LCD. Distance in centimetres is calculated using Ultrasonic Sensor and It is Displayed on Alphanumeric LCD.

First pin of Sensor is VCC which is connected to 5V Dc.

Second pin, Trigger pin, is connected to pin 6 of Arduino. This pin is input for sensor and Output for Arduino.

Third pin, Echo pin is connected to pin 7 of Arduino. This pin is output for sensor and input for Arduino.

Fourth pin of Sensor is GND which is connected to Ground pin of Arduino.

To display the distance in cm, we have connected an 16×2 alphanumeric LCD.

The connection of Arduino with LCD is Written below:

LCD Side PinArduino Side/
Bread Board Connection
VSS (Pin 1)GND
VDD (pin 2)VCC
VEE (Pin 3)Connected to Variable pin of 10k POT to Control Contrast of LCD
RS (Pin 4)12
R/W (pin 5)GND
E (Pin6)11
D0 (pin 7)GND
D1 (pin 8)GND
D2 (pin 9)GND
D3 (pin 10)GND
D4 (pin 11)5
D5 (pin 12)4
D6 (pin 13)3
D7 (pin 14)2
LED(+) (Pin 15)Connected to VCC via 1 kΩ resistor
LED(-) (Pin 16)GND

Arduino Code

#include "LiquidCrystal.h"    // We are using JHD 16x2 alphanumeric LCD using HD44780 controller for its controller

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// define trigger and Echo pin for Ultrasonic sensor
const int trigPin = 6;
const int echoPin = 7;

long distance = 0;

void display(void) {
  // write column, row and counting starts from zero so 0th Colum and 1st row
  lcd.setCursor(0, 1);
  lcd.print("            ");
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0)
  lcd.setCursor(0, 1);
  lcd.print(distance);
}

void setup() {

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a message to the LCD.
  lcd.print("Distance =");

  // set the trigPin as an output and echoPin as an input
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  digitalWrite(trigPin, LOW); // set trigger pin to LOW
  // provide some time for the sensor to settle
  delay(500);
}

void loop() {

  // send a trigger pulse to the sensor of 10 us
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // measure the total duration (transmit + echo) from high time of echo pulse in microseconds
  long duration = pulseIn(echoPin, HIGH);

  // calculate the distance in centimetres
  // using formula distance = (speed * time)   
  // speed of sound = 340 m/sec = 0.034 cm/microseconds
  distance = ((0.034) * (duration));
  // Here duration is calculated forward + back time hence to get object distance from sensor divide by 2
  distance = distance/2;

  display();
  // provide some time between readings
  delay(100);
}

Description of Code

In above code firstly header “LiquidCrystal.h” is included for using functions related to LCD. And using “LiquidCrystal” library we created object called “lcd”. Using below code line.

#include “LiquidCrystal.h”
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

 

Here 12th pin and 11th pin of Arduino acts as R/S pin and Enable pin of LCD and R/W pin is directly connected to GND.

In this code, we are using lcd in 4-bit mode i.e., we will be using D4, D5, D6, D7 data lines of lcd to display characters on it by using 5,4,3,2 pins of Arduino board.

Then we define trigger pin and echo pin of ultrasonic sensor.

In the setup function we use lcd.begin(), for providing number of columns and rows.

pinMode() function is used to set Trigger pin output and Echo pin Input w.r.t Arduino Board.

And Initially set Trigger pin low using function digitalWrite() using below code line:

void setup() {

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // Print a message to the LCD.
  lcd.print("Distance =");

  // set the trigPin as an output and echoPin as an input
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  digitalWrite(trigPin, LOW); // set trigger pin to LOW
  // provide some time for the sensor to settle
  delay(500);
}

 

In the loop function firstly send a trigger pulse by first writing trigger pin high, wait for 10 microseconds and then pull the pin Low.

This trigger induces a ultrasonic burst of 40Khz frequency on transmitter due to which HIGH pulse is received on Echo pin, which is measured by pulseIn(echoPin, HIGH) function, this function measures high time of Echo pulse and returns its durations in microseconds.

Now distance is calculated by multiplying speed of sound in cm per microsecond with time duration waves takes back and forth. And the actual distance between object and sensor is calculated by dividing calculated distance by 2, using below mentioned code lines:

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  distance = ((0.034) * (duration));
  distance = distance/2;

 

After that this distance is displayed on LCD by display() function. In display function first the cursor is set to column 0 row 1 and its cleared and then distance is displayed. Using below mentioned code lines:

lcd.setCursor(0, 1);
lcd.print("            ");
lcd.setCursor(0, 1);
lcd.print(distance);

You can directly copy and paste the above code in Arduino IDE and flash it on Arduino UNO board, Design the circuit on bread board and power it.

FAQ’S

What is the ultrasonic sensor HC-SR04 used to detect?

The HC-SR04 ultrasonic sensor is primarily used to detect the presence and distance of objects in its vicinity. It utilises ultrasonic sound waves to measure distances. 

How do I connect an ultrasonic sensor to my Arduino Uno?

To connect an ultrasonic sensor, such as the HC-SR04, to an Arduino Uno, you need to connect the VCC pin of the sensor to the 5V pin on the Arduino, the GND pin of the sensor to the GND pin on the Arduino, the Trig pin of the sensor to a digital pin on the Arduino (e.g., pin 2), and the Echo pin of the sensor to another digital pin on the Arduino (e.g., pin 3). Then, you can write the code to control the sensor, including defining the pins as input and output, sending trigger signals, measuring the echo time, and calculating the distance.

How to measure distance with Arduino using ultrasonic sensor?

It measures distance by sending and receiving the ultrasonic wave. The sensor emits ultrasonic sound waves and measures the time it takes for the waves to bounce back after hitting an object. Based on the measured time, the sensor can calculate the distance to the object within its detection range. We can calculate the distance travelled by the sound wave as

Formula: Distance = Speed * Time

What is the detection range of HC-SR04 ultrasonic sensor?

The HC-SR04 ultrasonic sensor has a typical detection range of 3 cm to 300 cm. This range can vary depending on factors such as the ambient conditions, the object’s size and shape, and the angle of incidence.