DIY Projects

Distance Measurement Using Ultrasonic Sensor with Arduino UNO

Statement of the Problem

In this article, we are going to see how to interface the Ultrasonic Sensor with Arduino and Monitor the distance measured by the Ultrasonics sensor using an LCD.  Various threshold levels of the distance are also measured and it will be indicated by the LED connected to the Arduino.

This article helps the reader to understand the interfacing of the Ultrasonic Sensor with Arduino. The Ultrasonic Sensor is a type of digital sensor, that emits a sound wave at a frequency higher than humans can hear, ultrasonic sensor’s function. To receive and transmit ultrasonic sound, the sensor’s transducer functions as a mic. As with many other ultrasonic sensors, ours sends a pulse and receives an echo using a single transducer.

Schematic Of Interfacing Ultrasonic Sensor And Lcd With Arduino

Connection Diagram Of Arduino With Ultrasonic And Lcd

Code For Interfacing Ultrasonic and LED With Arduino

#include<LiquidCrystal.h>

LiquidCrystallcd(7, 6, 5, 4, 3, 2);

#defineECHO9

#defineTRIGGER10

#defineDELAYTIME1000

#definePULSE_TRIGGER1

floatTOTAL_DURATION;

floatTOTAL_DISTANCE;

float INCHES;

float LED;

voidsetup(){

  // set up the LCD’s number of columns and rows:

  pinMode(11, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(13, OUTPUT);

  pinMode(TRIGGER, OUTPUT);

  pinMode(ECHO, INPUT);

  Serial.begin(9600);

  lcd.begin(16, 2);

  lcd.print(“IN CM    :”);

  lcd.setCursor(0,1);

  lcd.print(“IN INCHES:”);

}

voidloop(){

  digitalWrite(TRIGGER, HIGH);

  delay(PULSE_TRIGGER); 

  digitalWrite(TRIGGER, LOW);

  TOTAL_DURATION= pulseIn(ECHO,HIGH);

  TOTAL_DISTANCE = 0.01716*TOTAL_DURATION;

  INCHES = 0.3937*TOTAL_DISTANCE;

  // LED Indication

    if(TOTAL_DISTANCE <25){

    digitalWrite(11, HIGH);

    digitalWrite(12, LOW);

    digitalWrite(13, LOW);

     }

  if(TOTAL_DISTANCE >= 25&&TOTAL_DISTANCE <50){

    digitalWrite(11, LOW);

    digitalWrite(12, HIGH);

    digitalWrite(13, LOW);

     }

  if(TOTAL_DISTANCE >= 50&&TOTAL_DISTANCE <75){

    digitalWrite(11, LOW);

    digitalWrite(12, LOW);

    digitalWrite(13, HIGH);

     }

  //Display Section 

  lcd.setCursor(11, 0);

  lcd.print(float(INCHES));

  lcd.setCursor(11, 1);

  lcd.print(float(TOTAL_DISTANCE));

  delay(DELAYTIME); 

  lcd.setCursor(11,0);

  lcd.print(” “);

  lcd.setCursor(11,1);

  lcd.print(” “);

}

Circuit Explanation

The above circuit consists of Arduino UNO, LCD Screen, Ultrasonic sensor, and LED. The major part of this article is about interfacing Arduino with the Ultrasonic sensor, the following content explains how to interface the sensor with Arduino and Various parts of the Project.

ARDUINO UNO Board

led explanation of the Arduino.

LCD Screen Interfacing

Ultrasonic Sensor

Ultrasonic rangefinders work based on timing how long it takes for a signal to travel from a transmitter to a receiver.

Proximity sensors are made of ultrasonic sensors. Ultrasonic sensors are less susceptible to interference from smoke, gases, and other airborne particles in proximity sensing applications than infrared (IR) sensors.Ultrasonic frequencies are those that are higher than our human hearing threshold. These are higher than 20k Hertz frequencies.

Features

  • Supply voltage +5 V; 
  • Consumption in silent mode 2 mA; 
  • Consumption at work of 15 mA; 
  • Range of measurements – 2 to 400 cm; 
  • Effective measuring angle 15°; 
  • The dimensions are 45×20×15 mm.

An electronic device known as an ultrasonic sensor uses ultrasonic waves to measure an object’s distance from it and then transforms the sound wave reflection into electrical signals.

Ultrasonic Sensor Pinout Details

Compared to audible sound, ultrasound travels faster. The two primary parts of an ultrasonic sensor are a transmitter and a receiver.

Working Principle of Ultrasonic Sensor

The module emits an ultrasonic sound through its transmitter. If an object is in front of the ultrasonic sensor, this sound will be reflected. The receiver in the same module hears the sound that is reflected. A wave propagating at a 30° angle carries an ultrasonic transmission.

To achieve optimal accuracy, the angles measured should be at least 15°. Here, the distance to the desired object cannot be determined because of outside objects that lie within this measurement angle.

Ultrasonic sound travel time and speed measurements are used to calculate the distance.

Measured Distance = Time of Signal Travel x Speed of sound/ 2

The trigger pin must be triggered as high as possible for at least 10us to produce the ultrasonic sound. The module will then begin transmitting 8 ultrasonic burst ultrasounds at a frequency of 40 kHz. The Echo pin receiver will detect it, calculate the output time, and determine the distance.

Code Explanation

Interfacing the Arduino with an Ultrasonic Sensor and LCD Four sections

  1. The LCD header file with the corresponding pin connected to the Arduino is declared in the first section. Since LCD interfacing was employed in the project.
  1. The Second Section deals with the declaration of variable and define the pin forthe Ultrasonic Sensor. There are four pins on the ultrasonic sensor: VCC, Trigger, Echo, and Ground. Each pin of the Ultrasonic sensor is connected to the corresponding pin of the Arduino Uno board. 
  1. The ECHO Pin of the Ultrasonic sensor is connected to PIN 9 of Arduino. Similarly, the TRIGGER pin of the ultrasonic sensor to a PIN of 10.
  1. The DELAYTIME is used for setting the LCD and the PULSE_TRIGGER is used for sending pulse delay to the Trigger signal
  1. The DURATION, DISTANCE, INCHES,and LED variables are used for various calculations and display purposes in the program
  1. The third section deals with the hardware interface to the Arduino board such as LCD, LED, and ultrasonic Sensor. The void setup() with pinMode declaration for the LED output, Ultrasonic Sensor output, and LCD configuration.
  1. The next section deals with the task that Arduino going to perform. In this article, the Arduino sends a pulse to the Ultrasonic sensor continuously and receives the signal to the Arduino through an echo pin. The time taken was measured by pulseIn function and stored in the variable DURATION. 
  2. The value stored in the DURATION and converted into CM with the constant 0.01716. Similarly, the converter into INCHES with a constant of 0.3937.
  1. The below code is used to display the various ranges of distance using LED indicator. 
  1. The Fourth section is the final stage of the article, the Display section. The display section displays the result in the LCD. The various variables calculated by the Arduino will be sent to the LCD using the following code. The SetCursor function is used for the starting position and printfunction is used for displaying the variable on the LCD screen

Result

The following images show the various stages of distance detected by the Ultrasonic sensor and display the result on an LCD screen. At the same time, the range detection is indicated by the LED. 

Detection Range Between 50 To 75 cm

Detection Range Between 25 To 50 cm

Detection Range Below 25 cm

By doing the above exercise the reader can BE able to interface the Ultrasonic Sensor and LCD with Arduino UNO

Embarking on the Ultrasonic Sensor and LCD journey with Arduino was incredible. From connecting wires to seeing distance magically displayed, it felt like diving into a tech adventure. This hands-on learning sparked curiosity for more Arduino magic!

Sundareswaran Iyalunaidu

With over 24 years of dedicated experience, I am a seasoned professional specializing in the commissioning, maintenance, and installation of Electrical, Instrumentation and Control systems. My expertise extends across a spectrum of industries, including Power stations, Oil and Gas, Aluminium, Utilities, Steel and Continuous process industries. Tweet me @sundareshinfohe

Related Articles

Back to top button