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

Click to enlarge
Arduino Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//Arduino Code - HC-SR04 int trig = 12; //Define the pin# on the Arduino for the Trig pin on HC-SR04 int echo = 11; //Define the pin# on the Arduino for the Echo pin on HC-SR04 float duration, dist_cm, dist_in; //Define varaibles void setup() { pinMode(trig, OUTPUT); // Set trig as Output pinMode(echo, INPUT); // Set echo as Input Serial.begin(9600); // Start the serial communication } void loop() { digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH);// Set trig on HIGH for 10microseconds delayMicroseconds(10); digitalWrite(trig, LOW);//Set trig to LOW duration = pulseIn(echo, HIGH);//Read the pulse on echo and return the sound wave travel time in microseconds // Compute travel time. Note that the waves traveled back and forth (which is why we divide the time by 2). Multiply to get to cm or inches. dist_cm= duration*0.0343/2.0; //Compute distance in cm using the computed travel time dist_in= duration*0.0135/2.0; //Compute distance in inches computed travel time // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.print(dist_cm,1); Serial.print("cm / "); Serial.print(dist_in,1); Serial.println("in"); delay(300); } |
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)

Click to enlarge
Arduino Code (for use with LCD)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
//Arduino Code - HC-SR04 with 1602 LCD #include <LiquidCrystal.h>// include the library code LiquidCrystal lcd(9,10,4,5,6,7);// Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) int trig = 12; //Define the pin# on the Arduino for the Trig pin on HC-SR04 int echo = 11; //Define the pin# on the Arduino for the Echo pin on HC-SR04 float duration, dist_cm, dist_in; //Define varaibles void setup() { lcd.begin(16, 2); // set up the LCD's number of columns and rows pinMode(trig, OUTPUT); // Set trig as Output pinMode(echo, INPUT); // Set echo as Input } void loop() { digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH);// Set trig on HIGH for 10microseconds delayMicroseconds(10); digitalWrite(trig, LOW);//Set trig to LOW duration = pulseIn(echo, HIGH);//Read the pulse on echo and return the sound wave travel time in microseconds // Compute travel time. Note that the waves traveled back and forth (which is why we divide the time by 2). Multiply to get to cm or inches. dist_cm= duration*0.0343/2.0; //Compute distance in cm using the computed travel time dist_in= duration*0.0135/2.0; //Compute distance in inches computed travel time // Print the distance on the LCD lcd.setCursor(0,0); // set the cursor to column 0, line 0 lcd.print("Dist(cm): "); lcd.setCursor(10,0); // set the cursor to column 11, line 0 lcd.print(dist_cm,1); lcd.setCursor(0,1); // set the cursor to column 0, line 1 lcd.print("DIYEngineers.com"); delay(300); lcd.clear(); // Clears the display } |
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.

Components used in this example
*As an Amazon & Ebay Associate I earn from qualifying purchases.
Component | Link |
Arduino UNO | https://amzn.to/3uYVAMC https://ebay.us/veZdKX |
HC-SR04 | https://amzn.to/3gj8roX https://ebay.us/RFHEfU |
1602 LCD (with I2C module) | https://amzn.to/2P146LY https://ebay.us/sofDTP |
Electronic Component Kit (includes potentiometer) | https://amzn.to/3gj4r7O https://ebay.us/AdnfMw |
Breadboard (Elenco 9440) | https://amzn.to/3x23dnq https://ebay.us/FcwSdb |