Introduction: Arduino Based Distance Measurement Using Ultrasonic Sensor

About: Hello Welcome to Electrozubous on instructables, enjoy so much more on electronics circuit designing, Arduino projects, coding,3D printing,3D designing and much more so don't forget to stay creative.enjoy!!
In this tutorial we learn about how to design the Distance measuring devices using arduino and some other component. we design the very simple devices with the help of Arduino uno and ultrasonic sensor. you can use this devices to measure distance for a objects, human. also it can calculate distance to open or close a door of a house.

Step 1: Step 1: Required Materials

The components required for this build are:

Arduino uno R3
i2c LCD
Ultrasonic sensor module HC-SR04
Breadboard
Male to male jumper wires


Step 2: How HC-SR04 Ultrasonic Sensoar Works

An ultrasonic sensor is a device that generally used to measure the distance to an object and object detection using ultrasonic sound waves. The sensor head transmits an ultrasonic wave and receives the wave reflected from the object . Ultrasonic Sensor measure the distance to the object by calculating the time between the transmission and reception.
Distance = (Time x speed of sound in the air (340m/s))/2

Step 3: CIRCUIT DIAGRAM

The circuit diagram for Ultrasonic distance measurement using Arduino is given below.

Arduino and Ultrasonic Sensor based Distance Measurement Circuit Diagram

Trig and Echo pins of the ultrasonic sensor are connected to digital pin 3 & 2 of Arduino. VCC pin of the ultrasonic sensor is connected to the 5v pin of Arduino while the GND pin is connected to the GND of Arduino. SDA & SCL pin of the I2C module is connected to the A4 & A5 pin of Arduino while VCC and GND pins are connected to the 5V & GND pin of Arduino.

Step 4: Wiring Up the Circuit Diagram in Breadboard

Follow the circuit diagram below and wire up the connection.
Don't be to fast cross check all your connection.

Step 5: CODE EXPLANATION

Complete code with a working video for this Arduino and ultrasonic sensor based distance measurement project is given at the end of the document. Here we are briefly explaining the code.



Start the code by including the library files for LCD and Wire. These library files will be used for I2C communication between Arduino and LCD module.

#include

#include

LiquidCrystal_I2C lcd(0x27, 16, 2);



Then define the pins where you connected the Trig and Echo pins of the ultrasonic sensor.

const int trigPin = 3;

const int echoPin = 2;



After that, define two variables for duration and distance.

long duration;

int distance;



Inside the void setup function, define the Trig pin as an output pin and Echo pin as an input.

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);



Now inside the void loop function, calculate the time between triggered and received signal. This time will be used to calculate the distance.

duration = pulseIn(echoPin, HIGH);

distance = duration * 0.0340 / 2;

Step 6: TESTING THE PROJECT

After connecting all the components connect the Arduino to the laptop and upload the program. Now to check whether your project working correctly or not, put an object at different distances and compare the calculated distance with actual distance.

Step 7: COMPLETE CODE FOR ARDUINO DISTANCE MEASUREMENT

#include
#include
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int trigPin = 3;
const int echoPin = 2;
long duration;
int distance;

void setup() {

lcd.begin(); // Initializes the interface to the LCD display
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);

}

void loop() {
lcd.clear();
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.0340 / 2;
Serial.println("Distance");
Serial.println(distance);
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print("cm");
delay(1000);
}

Ignore this and clear this statement in your Arduino IDE // the modified code will be posted soon.

Step 8: MODIFIED CODE

// THIS CODE IS OFFICIALLY REPRODUCED BY ELECTROZUBOUS
// DATE 01/09/2020
// TIME 13:53


#include
#include
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define trigger 3
#define echo 2


float time=0,distance=0;




void setup() {
// put your setup code here, to run once:

pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
Serial.begin(9600);

lcd.init(); // Initializes the interface to the LCD display
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Ultra sonic");
lcd.setCursor(0,1);
lcd.print("Distance Meter");

delay(2000);

lcd.clear();
lcd.setCursor(0,0);
lcd.print("Electrozubous");

delay(2000);


}

void loop() {
// put your main code here, to run repeatedly:

lcd.clear();
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);

distance=time*340/20000;

Serial.println("Distance");
Serial.println(distance);

Serial.print("Distance");
Serial.print(distance/100);

lcd.clear();

lcd.print("Distance");
lcd.print(distance);

lcd.print("cm");
lcd.setCursor(0,1);

lcd.print("Distance");
lcd.print(distance/100);

lcd.print("m");

delay(1000);



}

Step 9: SHORT VIDEO

You can watch the short video of projects on YouTube channel just follow this link https://youtu.be/hWe78muF1lU