Maverick Den STEM Robotics Pvt. Ltd.
Email: info@maverickden.com
Contact No. : 7715916683 (India)
60123250147 (Malaysia)

Start Your Career with STEM Based Robotics (In 6/12 Weeks Internship)

MEASURING DISTANCE USING ULTRASONIC SENSOR

In this tutorial we will teach you how to measure distance  with Arduino UNO / Nano

COMPONENTS USED :

SR NO

COMPONENT NAME QUANTITY
1 Arduino Board(Uno or Nano)

1

2

BreadBoard 1
3 Ultra Sonic Sensor

4

4

Jumper Cable (male to male) 1 set
5 LCD

1

 

Schematic Diagram :

Source Code:

#include <LiquidCrystal.h>                  //Load Liquid Crystal Library

LiquidCrystal LCD(11,10,9,2,3,4,5);  //Create Liquid Crystal Object called LCD

#define trigPin 13                                 //Sensor Echo pin connected to Arduino pin 13

#define echoPin 12                              //Sensor Trip pin connected to Arduino pin 12

//Simple program just for testing the HC-SR04

//Ultrasonic Sensor with LCD dispaly

 

void setup()

{

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

 

LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD

LCD.setCursor(0,0);  //Set LCD cursor to upper left corner, column 0, row 0

LCD.print(“Target Distance:”);  //Print Message on First Row

}

 

void loop() {

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

 

duration = pulseIn(echoPin, HIGH);

//  duration1 = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

// distance1=((duration/2) / 29.1);

LCD.setCursor(0,1);  //Set cursor to first column of second row

LCD.print(“”); //Print blanks to clear the row

LCD.setCursor(0,1);   //Set Cursor again to first column of second row

LCD.print(distance); //Print measured distance

LCD.print(” cm”);  //Print your units.

delay(250); //pause to let things settle

}

Video: