Distance Measurement using Ultrasonic Sensor and Arduino

January 24, 2022 • Harsh Shinde

Arduino:

Arduino Uno board

Arduino is an open-source electronics platform that encompasses both hardware and software components. At its core, Arduino boards feature microcontrollers, such as the popular Atmel AVR family, with built-in input/output pins for interacting with external components. The Arduino Integrated Development Environment (IDE) provides a user-friendly interface to write, compile, and upload code to the board, simplifying programming with its C/C++ language support.

Ultrasonic Sensor

Ultrasonic Sensor

An ultrasonic sensor is a device that uses sound waves at frequencies higher than the human audible range to measure distances and detect objects without physical contact. It works based on the principle of sending ultrasonic pulses and measuring the time it takes for the sound waves to bounce back after hitting an obstacle.

Working of Ultrasonic Sensor?

Working of Ultrasonic Sensor

An ultrasonic sensor works by emitting high-frequency sound waves (ultrasonic pulses) and measuring the time it takes for the waves to bounce back after hitting an object. The sensor then calculates the distance to the object based on the time of flight of the sound waves. This distance measurement technique allows the sensor to detect objects or measure distances without any physical contact, making it useful in various applications such as distance sensing, object detection, and collision avoidance.

The speed of sound in air is nearly 344 m/s

Basic Formula:
Distance = Speed × Time

In the code, the "duration" variable stores the time taken by the sound wave traveling from the emitter to the receiver. This is double the time to reach the object, as the sensor returns the total time including sender to object and object to receiver. Therefore, the time taken to reach the object is half of the total time measured.

Applied Formula:
Distance = Speed of Sound in Air × (Time Taken ÷ 2)
Note: Speed of sound in air = 344 m/s

16x2 LCD

16x2 LCD Display

16×2 Display Features:

Some Info are listed below:

  1. A 16x2 LCD, which stands for 16 characters by 2 lines LCD, is a popular alphanumeric display module used in a wide range of electronic applications.
  2. It consists of 16 columns and 2 rows of character spaces, allowing it to display up to 16 characters per line and accommodate a total of 32 characters.

Bread board

A "breadboard short" refers to an unintended electrical connection (short circuit) between two or more points on a breadboard. Breadboards are prototyping tools used by electronics enthusiasts, hobbyists, and engineers to build and test circuits without soldering. They consist of a grid of interconnected holes that allow components and wires to be inserted and connected easily.

Breadboard

Circuit Diagram:

Circuit Diagram

Setup:

  1. Connect the Echo pin of the sensor to the pin of the Arduino.
  2. Connect the Trigger pin of the sensor to the pin of the Arduino.
  3. Navigate to Tools and select board and port.
  4. Verify and compile the code, then upload the code to the Arduino Uno board.
  5. Monitor the output in the Serial monitor or in LCD Display.

Arduino Code:

Github Repo

#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
long duration;
int distanceCM;
int distanceInch;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.  
  pinMode(7,OUTPUT);
  pinMode(6,INPUT);
  lcd.clear();
}

void loop() {
  delay(120);
  lcd.clear();
  digitalWrite(7,LOW);
  delayMicroseconds(2);
  digitalWrite(7,HIGH);
  delayMicroseconds(10);
  digitalWrite(7,LOW);
  duration =  pulseIn(6,HIGH);
  distanceCM = (duration*0.034)/2;
  distanceInch = (duration*0.0133)/2;
  lcd.setCursor(0,0);
  lcd.print("Distance: ");
  lcd.print(distanceCM);
  lcd.print(" Cm");
  delay(10);
  lcd.setCursor(0,1);
  lcd.print("Distance: ");
  lcd.print(distanceInch);
  lcd.print(" In\"");
  delay(10);
}

Conclusion:

Indeed! The Ultrasonic sensor is a versatile and widely used sensor in DIY electronics projects. It offers non-contact distance measurement, object detection, and position sensing capabilities, making it ideal for various applications. With the help of an Arduino board, it becomes easy to interface and utilize the sensor's data for a wide range of projects, including robotics, home automation, and more. Its affordability and ease of use make it a popular choice among hobbyists and students for creating innovative and interactive projects.

NOTE: All the above projects are hosted in GitHub. There are many other projects based on every topic every language and every technology.

Now let's Get Started with it!