# Ultrasonic Distance Sensor Using Arduino

61

We are going to use the HC-SR04 ultrasonic sensor which enables your robots to avoid obstacles and will move in different directions.

### Supplies:

Arduino Uno

Ultrasonic Sensor - HC-SR04

16X2 LCD with I2C Converter

9V battery

Connecting wires

Software:

Arduino IDE

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: About the Project

Ultrasonic sensors are useful tools to measure distance without actual contact and utilized at various places like distance measurement, water level measurement, etc. This is an effective way to measure small distances accurately.

IoT Training online adds additional advantage to learn those concepts with its practical implementation to build End-to-End IoT Solutions.

In this project, we have used an Ultrasonic Sensor to determine the distance of an obstacle from the sensor and then the device will display it on a screen. Ultrasonic sensor HC-SR04 is utilized here to measure distance within the range of 2cm-400cm with an accuracy of 3mm.

The sensor module includes an ultrasonic transmitter, receiver and control circuit. In-circuit connections Ultrasonic sensor module’s “trigger” and “echo” pins are directly attached to pin 18(A4) and 19(A5) of Arduino.

A 16x2 LCD with I2C is connected with Arduino in 4-bit mode. Control pins RS, RW and En are attached directly to Arduino pin 2, GND and 3. And data pin D4-D7 is connected to 4, 5, 6 and 7 of Arduino.

First of all, it is necessary to trigger the ultrasonic sensor module to transfer signals by using Arduino and then wait to receive ECHO. Arduino reads the time between triggering and Received ECHO. A 16x2 LCD is generally used for displaying distance.

so we can evaluate the distance by using the following formula: Distance= (time/2) * speed of sound Where speed of sound around 340m per second.

## Step 2: Run a Program

#include

#define trigger 18

#define echo 19

LiquidCrystal lcd(2,3,4,5,6,7);

float time=0,distance=0;

void setup()

{

lcd.begin(16,2);

pinMode(trigger,OUTPUT);

pinMode(echo,INPUT);

lcd.print(" Ultra sonic");

lcd.setCursor(0,1);

lcd.print("Distance Meter");

delay(2000);

lcd.clear();

lcd.print(" Circuit Digest");

delay(2000);

}

void loop()

{

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;

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

}

## Recommendations

• ### Internet of Things Class

22,838 Enrolled