HC-SR04 Ultrasonic Sensor – How to use with Arduino

Learn to how use the HC-SR04 ultrasonic sensor. In this post we’ll go over basics, the sensor specs, wiring it to the Arduino, programming in Arduino, and testing. As a bonus we’ll go over how to use with a 1602 LCD to display the sensor measurements.

The HC-SR04 ultrasonic sensor works by emitting an ultrasound. The ultrasound will travel through air until it reaches an object, which will cause them to bounce back, and return to the sensor. During this process, the HC-SR04 will measure the time that it took the ultrasound to return. This time can then be used to determine the distance to the object.

Remember that speed is calculated by dividing the distance traveled over the time it took to travel such distance. Therefore speed = distance/time. In the case of the HC-SR04, the ultrasound is traveling at the speed of sound (343 meters/second or 13503.9 inches/second).

Since speed = distance/time, then distance = speed * time. We know the speed of sound, and we are measuring the time it takes for the ultrasound to travel from the sensor, to the object, and back to the sensor. Note that the total distance traveled equals 2*d (as shown on the image to the right). As a result, the distance, d, between the sensor and the object will be calculated as: d = speed*time/2.

HC-SR04 Specs

Operating Voltage: 5 Volts
Operating Current: 15mA
Range: 2cm (0.79in) – 400cm (157.48in)
Measuring Angle: 15 degrees

For more details, please see the sensor datasheet.

Connecting HC-SR04 to Arduino UNO

VCC – Connected to Arduino UNO 5V output
Trig – Connected to Arduino digital IO Pin #12
Echo – Connected to Arduino digital IO Pin #11
GND – Connected to Arduino GND

HC-SR04 connections to Arduino UNO.

Click to enlarge

Arduino Code

Run Arduino Code

All you need to do now is run the code that was provided above and open the Serial Monitor.

Click to enlarge

HC-SR04 with 1602 LCD display

Below we will show how to use the HC-Sr04 ultrasonic sensor along with a 1602 LCD display. Please reference a prior post on the 1602 LCD if you need more details.

Connecting to Arduino UNO (for use with LCD)

HC-SR04 connections to Arduino UNO (with LCD).

Click to enlarge

Arduino Code (for use with LCD)

Run Arduino Code

After running the Arduino code, the LCD should display something similar to what’s shown in the image to the right, where we can see how the LCD display shows the distance measured by the HC-SR04 ultrasounds sensor.

HC-SR04 testing with LCD.

Components used in this example

*As an Amazon & Ebay Associate I earn from qualifying purchases.

Arduino UNOhttps://amzn.to/3uYVAMC
1602 LCD (with I2C module)https://amzn.to/2P146LY
Electronic Component Kit
(includes potentiometer)
Breadboard (Elenco 9440)https://amzn.to/3x23dnq

Leave a Reply

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