DISTANCE CALCULATOR USING ULTRASONIC SENSOR

by vanshbhansali03 in Circuits > Arduino

208 Views, 0 Favorites, 0 Comments

DISTANCE CALCULATOR USING ULTRASONIC SENSOR

WhatsApp Image 2024-06-30 at 21.10.09.jpeg

This project involves creating a simple distance measuring device using an Arduino, an HC-SR04 ultrasonic sensor, and an I2C LCD display. The device measures the distance to an object and displays this distance in centimeters on the LCD screen.

Supplies

Arduino Uno

Male to Male jumper wires

Male to Female jumper wires

HC-SR04 Ultrasonic Sensor

2C LCD Display (16x2

Bread board

Circuit

1. Connecting the Ultrasonic Sensor (HC-SR04)

  1. Mount the Sensor:
  • Place the HC-SR04 ultrasonic sensor onto the breadboard, ensuring it is securely seated.
  1. Power Connections:
  • Connect the VCC pin of the sensor to the 5V power rail on the breadboard.
  • Connect the GND pin of the sensor to the GND rail on the breadboard.
  1. Signal Connections:
  • Connect the Trig pin of the sensor to any available digital pin on the Arduino (e.g., pin 9).
  • Connect the Echo pin of the sensor to another digital pin on the Arduino (e.g., pin 10).
  1. Ground Connections:
  • Ensure that the GND rail of the breadboard is connected to the GND pin on the Arduino.

2. Connecting the I2C LCD Display (16x2)

  1. Power Connections:
  • Connect the VCC pin of the LCD to the 5V power pin on the Arduino.
  • Connect the GND pin of the LCD to the GND pin on the Arduino.
  1. I2C Connections:
  • Connect the SDA pin of the LCD to the A4 pin on the Arduino.
  • Connect the SCL pin of the LCD to the A5 pin on the Arduino


Code

This code is designed to measure the distance to an object using an HC-SR04 ultrasonic sensor and display the measured distance on a 16x2 I2C LCD screen. The code uses the Arduino platform to interface with both the sensor and the display, providing real-time distance measurements.

Libraries:

  1. Wire.h: This library allows the Arduino to communicate with I2C devices.
  2. LiquidCrystal_I2C.h: This library facilitates control of the I2C LCD.

Pin Configuration:

  • Ultrasonic Sensor:
  • trigPin is connected to digital pin 9.
  • echoPin is connected to digital pin 10.
  • I2C LCD:
  • I2C address is set to 0x27.
  • LCD dimensions are 16 columns by 2 rows.

Setup Function:

  1. Serial Communication:
  • Initializes the serial communication at 9600 baud rate for debugging purposes.
  1. LCD Initialization:
  • Initializes the LCD and turns on the backlight.
  • Prints the static text "Distance:" on the first row of the LCD.
  1. Sensor Pin Modes:
  • Configures the trigPin as an output and the echoPin as an input.

Loop Function:

  1. Generate Ultrasonic Pulse:
  • Sets the trigPin LOW for 2 microseconds to ensure a clean pulse.
  • Sets the trigPin HIGH for 10 microseconds to generate the ultrasonic pulse.
  • Sets the trigPin LOW to end the pulse.
  1. Measure Echo Time:
  • Reads the duration of the echo pulse from the echoPin.
  1. Calculate Distance:
  • Calculates the distance using the formula: distance = duration * 0.034 / 2, where 0.034 cm/μs is the speed of sound in air.
  1. Display Distance:
  • Clears the second row of the LCD to ensure the previous distance is removed.
  • Displays the newly calculated distance on the second row of the LCD.
  1. Serial Monitor Output:
  • Optionally prints the distance to the Serial Monitor for debugging.
  1. Delay:
  • Adds a 500-millisecond delay before the next measurement cycle to allow for readable updates.