Distance Measurement Using Ultrasonic Sensor

Distance Measurement Using Ultrasonic Sensor

Distance Measurement using Ultrasonic Sensor

Today we are going to create a digital Scale for Measurement of Objects distance. We are going to create a Distance Measurement using Ultrasonic Sensor system which will display the Distance in ‘cm’ on a 16X2 LCD Display everything will be controlled by Arduino.

So first Gather all the components :

What is I2C?

I2C combines the best features of SPI and UARTs. With I2C, you can connect multiple slaves to a single master (like SPI) and you can have multiple masters controlling single, or multiple slaves. This is really useful when you want to have more than one microcontroller logging data to a single memory card or displaying text to a single LCD.

Like UART communication, I2C only uses two wires to transmit data between devices:

Introduction-to-I2C-Single-Master-Single-Slave

SDA (Serial Data) – The line for the master and slave to send and receive data.

SCL (Serial Clock) – The line that carries the clock signal.

I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (the SDA line).

For more detailed information on I2C – Click Here

For more information on Ultrasonic Sensor – Click Here

Circuit Diagram

I2C Ultrasonic LCD CIrcuit Diagram
I2C Ultrasonic LCD CIrcuit Diagram

Connections of the distance measurement using Ultrasonic sensor is shown above.

you can download the fritzing file here.

Code

/*
  Ultrasonic Distance Measurement on I2C LCD.
  Created by - Alpha Electronz
  Date Created - 16-01-2020
  For more information visit - https://alphaelectronz.com/distance-measurement-using-ultrasonic-sensor
*/
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int trig_pin = 8;
const int echo_pin = 9;
long distance, duration;
void setup()
{
  Serial.begin(9600);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, INPUT);
  digitalWrite(7, HIGH);
  lcd.init();                      // initialize the lcd
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("   Welcome to   ");
  lcd.setCursor(0, 1);
  lcd.print("Alpha Electronz");
  delay(1000);
  lcd.clear();
}
void loop()
{
  digitalWrite(8, HIGH);
  delayMicroseconds(20);
  digitalWrite(8, LOW);
  delayMicroseconds(20);
  duration = pulseIn(echo_pin, HIGH); //To receive the reflected signal.
  distance = duration * 0.034 / 2;
  Serial.print(distance);
  Serial.println("cm");
  lcd.setCursor( 0, 0); //set the cursor to column 0 and line 1
  lcd.print(distance);
  lcd.setCursor( 0, 1);
  lcd.print("cm");
  delay(500);
  lcd.clear();
}


Output Video

0

Leave a Comment

Your e-mail address will not be published. Required fields are marked *