DISTANCE CALCULATOR USING ULTRASONIC SENSOR
by vanshbhansali03 in Circuits > Arduino
208 Views, 0 Favorites, 0 Comments
DISTANCE CALCULATOR USING ULTRASONIC SENSOR
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)
- Mount the Sensor:
- Place the HC-SR04 ultrasonic sensor onto the breadboard, ensuring it is securely seated.
- 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.
- 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).
- 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)
- 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.
- 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:
- Wire.h: This library allows the Arduino to communicate with I2C devices.
- 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:
- Serial Communication:
- Initializes the serial communication at 9600 baud rate for debugging purposes.
- LCD Initialization:
- Initializes the LCD and turns on the backlight.
- Prints the static text "Distance:" on the first row of the LCD.
- Sensor Pin Modes:
- Configures the trigPin as an output and the echoPin as an input.
Loop Function:
- 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.
- Measure Echo Time:
- Reads the duration of the echo pulse from the echoPin.
- 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.
- 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.
- Serial Monitor Output:
- Optionally prints the distance to the Serial Monitor for debugging.
- Delay:
- Adds a 500-millisecond delay before the next measurement cycle to allow for readable updates.