How to use HC-SR04 ultrasonic module with Arduino to measure distance in cm and inch
Hello there, here we are using the HC-SR04 ultrasonic module to measure distance, it’s a classic Arduino project to get you started, this module can measure up to 5m (16ft) it’s quiet accurate and easy to calibrate as we saw in the video, you can integrate it in your projects, especially robots to detect obstacles or measure distance too while moving… (we don’t talk too much here) so here’s the wiring I used it’s only one wiring:
- Arduino Uno board
- HC-SR04 ultrasonic sensor
- LCD i2c screen (tutorial = Arduino LCD I2C simple use and direct write from serial monitor)
- Some jump wires and a breadboard
Below we gonna check how to measure using Serial monitor or LCD in both metric and imperial units.

The module’s look make it interesting for robots projects, it has four pins VCC/GND for power (5VDC) and Trigg/Echo are for Sending/Receiving signal.
N.B:
Before you use it in any project, make sure to do some test first on the surface you want to detect if it reflects the ultrasound back or not, also the shape of the surface can affect the correct measurements.
Wiring:

The LCD i²c is optional in case you want to use it, otherwise remove it and slightly modify the code.
Library:
Download LCD i2c library here
Codes:
Download all codes .ino format here or check the following:
Code 1: Direct test and distance measuring in cm, with display on the Serial Monitor
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 |
//This code is to use with HC-SR04 ultrasound module, //it measures the distance and display it on the serial montior in cm //Refer to http://www.SurtrTech.com for more details const int trigPin = 9; //pins where trigger and echo are wired const int echoPin = 10; long duration; //some variables that we need, duration and distance int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication watch your baudrates } void loop() { digitalWrite(trigPin, LOW); //setting the trigger pin on low delayMicroseconds(2); //delay is usually in miliseconds but here we are on µs digitalWrite(trigPin, HIGH);//emitting the ultrasounds delayMicroseconds(10); //duration of emission digitalWrite(trigPin, LOW); //turning off the emitter duration = pulseIn(echoPin, HIGH); //measuring the duration pulseIn function measure the time between the echopin getting on high and the getting on low distance= duration*0.034/2; //Quick maffs to get the correct distance you can do some calibration here as seen in the video Serial.print("Distance: "); //printing on the serial monitor Serial.print(distance); Serial.println(" cm"); delay(1000); //Change the delay if you want to change the measuring frequency } |
Code 2: Display the measured values in cm on the LCD i²c screen
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
//This code is to use with HC-SR04 ultrasound module, //it measures the distance and display it on the LCD i2c monitor in cm //refer to http://www.surtrtech.com for more information #include <LiquidCrystal_I2C.h> //LCD i2c library #define I2C_ADDR 0x27 #define BACKLIGHT_PIN 3 #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); // from this and above are LCD i2c stuff const int trigPin = 9; //pins of the module and where they are wired const int echoPin = 10; long duration; //variables nedeed for measuring float distanceCm; void setup() { lcd.begin(16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { lcd.clear(); //I add this fucntion so solve that problem of "cmcm" digitalWrite(trigPin, LOW); //setting the trigger pin on low delayMicroseconds(2); //delay is usually in miliseconds but here we are on µs digitalWrite(trigPin, HIGH);//emitting the ultrasounds delayMicroseconds(10); //duration of emission digitalWrite(trigPin, LOW); //turning off the emitter duration = pulseIn(echoPin, HIGH); //measuring the duration pulseIn function measure the time between the echopin getting on high and the getting on low distanceCm= duration*0.034/2; lcd.setCursor(0,0); lcd.print("Distance: "); lcd.setCursor(0,1); lcd.print(distanceCm); lcd.print(" cm"); delay(1000); } |
Code 3: Display the measured values in inches on the LCD i²c screen
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
//This code is to use with HC-SR04 ultrasound module, //it measures the distance and display it on the LCD i2c monitor in inch //refer to http://www.surtrtech.com for more information #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 #define BACKLIGHT_PIN 3 #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); const int trigPin = 9; //pins of the module and where they are wired const int echoPin = 10; long duration; //variables nedeed for measuring float distanceInch; void setup() { lcd.begin(16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { lcd.clear(); //I add this fucntion so solve that problem of "cmcm" digitalWrite(trigPin, LOW); //setting the trigger pin on low delayMicroseconds(2); //delay is usually in miliseconds but here we are on µs digitalWrite(trigPin, HIGH);//emitting the ultrasounds delayMicroseconds(10); //duration of emission digitalWrite(trigPin, LOW); //turning off the emitter duration = pulseIn(echoPin, HIGH); //measuring the duration pulseIn function measure the time between the echopin getting on high and the getting on low distanceInch = duration*0.0133/2; lcd.setCursor(0,0); lcd.print("Distance: "); lcd.setCursor(0,1); lcd.print(distanceInch); lcd.print(" inch"); delay(1000); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
3 thoughts on “How to use HC-SR04 ultrasonic module with Arduino to measure distance in cm and inch” Leave a comment ›