Skip to content
Advertisements

How to use HC-SR04 ultrasonic module to measure distance in cm and inch

 

 

Hello, here we are using the HC-SR04 ultrasonic module to measure distance, it’s a classic Arduino project to learn how to use the board, this module can measure up to 5m 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: 

1) Arduino Uno board

2) HC-SR04 ultrasonic sensor

3) LCD i2c screen (tutorial =  Arduino LCD I2C simple use and direct write from serial monitor)

4) Some jump wires and a breadboard

 

Wiring:

 

Codes:

Code 1: Displaying the distance on the serial monitor

//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 surtrtech channel for wiring


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: Measure and display the value on the LCD screen in (cm)

//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 surtrtech channel for wiring


#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: Measure and display the value on the LCD screen in (inch)

//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 surtrtech channel for wiring


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


}
Advertisements

Yassine View All

Electronics, Automation engineering student... I try to share my little experience with other electronics amateur.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: