Introduction
Creating an ultrasonic parking sensor with an I2C LCD display using an Arduino is a practical and educational project that combines electronics, programming, and a useful real-world application. In this guide, we will walk you through building your own parking sensor, explaining each step clearly to ensure you have a comprehensive understanding of the project.
Components Needed
- Arduino Board (e.g., Arduino Uno): The central component that controls the ultrasonic sensor and I2C LCD display.
- Ultrasonic Distance Sensor (HC-SR04): This sensor will measure the distance between your car and an obstacle
- I2C LCD Display (16×2 or similar): To provide visual feedback of the distance measurement.
- Jumper Wires: To connect the components together.
- Breadboard : For creating a stable prototype.
- Arduino IDE: To write and upload the Arduino code.
Circuit Connection
- Ultrasonic Distance Sensor (HC-SR04): Connect the sensor to the Arduino as follows:
- VCC to 5V on the Arduino
- GND to GND (Ground) on the Arduino
- TRIG to Digital Pin 9 on the Arduino
- ECHO to Digital Pin 10 on the Arduino
- I2C LCD Display: Connect the I2C LCD display to the Arduino as follows:
- SDA to the SDA pin on the Arduino (analog pin A4)
- SCL to the SCL pin on the Arduino (analog pin A5)
Code
Here’s an Arduino code that reads data from the ultrasonic sensor and displays the distance on the I2C LCD display:
Library : LiquidCrystal_I2C
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Create a LiquidCrystal_I2C object
const int trigPin = 9; // TRIG pin of the ultrasonic sensor
const int echoPin = 10; // ECHO pin of the ultrasonic sensor
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.init(); // Initialize the LCD
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Parking Sensor");
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
long duration;
int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
lcd.setCursor(0, 1);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm ");
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Delay for 1 second before taking the next measurement
}
Code Simplified
- We create a LiquidCrystal_I2C object to control the I2C LCD display.
- In the
setup()
function, we initialize the ultrasonic sensor’s TRIG and ECHO pins, the I2C LCD display, set up the cursor position, and begin serial communication for debugging. - In the
loop()
function, we measure the distance using the ultrasonic sensor by sending a pulse and calculating the duration of the echo. - We convert the duration into centimeters and display it on the I2C LCD display and the Serial Monitor.
Test the Code
- Upload the code to your Arduino.
- Connect the ultrasonic sensor and I2C LCD display to the Arduino as described.
- Power up your Arduino.
- Place an obstacle in front of the ultrasonic sensor, and you’ll see the distance displayed on the I2C LCD.
- As you move the obstacle closer or farther, the distance reading will update in real-time.
Conclusion
You’ve successfully built an ultrasonic parking sensor with an I2C LCD display using Arduino. This project combines practicality and educational value, making it a great way to learn about ultrasonic sensors, Arduino programming, and real-world applications. You can further enhance this project by adding additional features like alarms or notifications when the distance reaches a critical point. Enjoy your DIY parking sensor!