The HC-SR04 Ultrasonic Distance Sensor is a versatile and popular choice for distance measurement in various DIY electronics projects. When combined with an Arduino microcontroller, it can accurately measure distances, making it ideal for applications such as obstacle avoidance in robotics, liquid level measurement, and more.

This will walk you through the process of interfacing the HC-SR04 sensor with an Arduino, providing clear instructions, code examples, and troubleshooting tips to ensure successful implementation. In this comprehensive guide we use HC-SR04 Ultrasonic Sensor with Arduino for displaying the measured distance on an LCD screen.

What is HC-SR04 Ultrasonic Sensor

The HC-SR04 Ultrasonic Sensor sensor measures the distance to an object by emitting an ultrasonic sound wave and calculating the time it takes for the echo to return. With a range of 3 cm to 300 cm, the HC-SR04 is ideal for applications that require precise distance measurements.

Technical Specifications

Here are the technical specifications of the Ultrasonic Sensor HC-SR04:

  • Operating Voltage: 5V DC
  • Operating Current: 15mA
  • Frequency: 40kHz
  • Range: 3cm to 300cm
  • Resolution: 0.3cm (0.1 inch)
  • Measuring Angle: 15 degrees
  • Trigger Input Signal: 10µs TTL pulse
  • Echo Output Signal: TTL level signal proportional to the distance
  • Dimensions: 45mm x 20mm x 15mm
  • Weight: 9g
  • Communication Interface: 4-pin (VCC, Trigger, Echo, GND)
  • Temperature Range: -15°C to 70°C
  • Maximum Range Accuracy: ±3mm

How Does the Ultrasonic Sensor HC-SR04 Work?

The Ultrasonic Sensor HC-SR04 consists of two main components: a transmitter (which emits ultrasonic waves) and a receiver (which detects the reflected waves). The transmitter generates a high-frequency sound pulse at 40 kHz. When this pulse hits an object, it is reflected back to the sensor and detected by the receiver.

First, the microcontroller connected to the HC-SR04 sends a trigger signal to the sensor. This trigger signal is a 10-microsecond TTL pulse. Upon receiving this pulse, the sensor emits an ultrasonic burst for 8 cycles at 40 kHz, which travels through the air. When the sound waves encounter an object, they reflect back to the sensor. The sensor's receiver then captures these reflected waves.

The sensor measures the time interval between sending the trigger signal and receiving the echo. This time interval is directly proportional to the distance the sound wave has traveled. The HC-SR04 outputs this time interval as a high TTL pulse on the Echo pin. The length of this pulse represents the duration taken for the echo to return.

To calculate the distance, the following formula is used: Distance=(Time×Speed of Sound​)/2 The division by 2 accounts for the fact that the sound wave travels to the object and then back to the sensor, covering twice the actual distance to the object. The speed of sound in air is approximately 343 meters per second (m/s) at room temperature. Therefore, if the time is measured in microseconds (µs), the distance in centimeters (cm) can be calculated using: Distance (cm)=(Time (µs)×0.0343​)/2​ For instance, if the time measured is 500 µs, the distance to the object would be: Distance=(2500×0.0343​)/2​=8.575 cm This calculated distance allows the sensor to determine the proximity of objects accurately within its specified range.

Setting Up the Arduino Environment

Installing Arduino IDE To program the Arduino, you need to install the Arduino Integrated Development Environment (IDE). Download the latest version from the official Arduino website and follow the installation instructions for your operating system.

Pinout of HC-SR04

The HC-SR04 Ultrasonic Sensor has four pins, each serving a specific purpose. Here is a detailed description of each pin:

  • VCC: This pin is used to provide the operating voltage to the sensor. The voltage required is 5V DC.
  • Trig: This pin is used to initiate the ultrasonic pulse.
  • Echo: This pin outputs a signal that is proportional to the time taken for the echo to be received.
  • GND: This pin is connected to the ground of the circuit.

Wiring the HC-SR04 Sensor to Arduino

Here are the connections:

HC-SR04 Ultrasonic Sensor and Arduino Uno Connections:

  • VCC (Power Supply): Connected to the 5V pin on the Arduino Uno.
  • GND (Ground): Connected to the GND pin on the Arduino Uno.
  • Trig (Trigger Input): Connected to digital pin 6 on the Arduino Uno. This pin will receive the trigger signal from the Arduino to initiate the ultrasonic burst.
  • Echo (Echo Output): Connected to digital pin 7 on the Arduino Uno. This pin sends the echo signal back to the Arduino..

The connection of Arduino with LCD:

  • VSS (Pin 1) to GND: This pin is the ground pin for the LCD. Connecting it to the GND of the Arduino completes the circuit and provides a common reference voltage for the system.
  • VDD (Pin 2) to VCC: This pin is the power supply pin for the LCD. Connecting it to the 5V pin on the Arduino powers the LCD.
  • VEE (Pin 3) to the Variable Pin of 10k Potentiometer: This pin controls the contrast of the LCD. By connecting it to the variable middle pin of a 10k potentiometer, you can adjust the voltage and hence the contrast of the display.
  • RS (Pin 4) to Digital Pin 12: The Register Select (RS) pin is used to select the data register or command register. Connecting it to digital pin 12 of the Arduino allows the microcontroller to switch between instructions (commands) and data for the display.
  • R/W (Pin 5) to GND: The Read/Write (R/W) pin controls whether the LCD is in read mode or write mode. Connecting it to ground sets the LCD to write mode, as this project typically only writes data to the LCD.
  • E (Pin 6) to Digital Pin 11: The Enable (E) pin is used to enable the display for receiving data. Connecting it to digital pin 11 of the Arduino allows the microcontroller to send data to the LCD by toggling this pin.
  • D0 to D3 (Pins 7 to 10) to GND: These pins are the lower data bits for the LCD. In 4-bit mode, these pins are not used and are connected to ground.
  • D4 (Pin 11) to Digital Pin 5: This is the fourth data bit in 4-bit mode. Connecting it to digital pin 5 of the Arduino allows the microcontroller to send data to the LCD.
  • D5 (Pin 12) to Digital Pin 4: This is the fifth data bit in 4-bit mode. Connecting it to digital pin 4 of the Arduino allows the microcontroller to send data to the LCD.
  • D6 (Pin 13) to Digital Pin 3: This is the sixth data bit in 4-bit mode. Connecting it to digital pin 3 of the Arduino allows the microcontroller to send data to the LCD.
  • D7 (Pin 14) to Digital Pin 2: This is the seventh data bit in 4-bit mode. Connecting it to digital pin 2 of the Arduino allows the microcontroller to send data to the LCD.
  • LED(+) (Pin 15) to VCC via 1kΩ Resistor: This pin powers the backlight of the LCD. Connecting it to VCC through a 1kΩ resistor limits the current and prevents damage to the backlight.
  • LED(-) (Pin 16) to GND: This pin is the ground for the backlight. Connecting it to GND completes the circuit for the backlight.

Arduino Code

Below is a basic Arduino code to interface with the HC-SR04 sensor. This code triggers the sensor, reads the echo time, and calculates the distance.

#include "LiquidCrystal.h"   
// We are using JHD 16x2 alphanumeric LCD with HD44780 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);
 lcd.print(" cm");
}

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);
}

Built With

Share this project:

Updates