Arduino Nano-Based Smart Parking System with Ultrasonic Sensors and I2C LCD Display
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to monitor parking slots using ultrasonic sensors and indicate their status using LEDs and an LCD display. The Arduino Nano serves as the central controller, interfacing with HC-SR04 ultrasonic sensors to detect the presence of vehicles in parking slots. The status of each slot is displayed on an I2C LCD screen and indicated by red and green LEDs: red for occupied and green for available. Additionally, a Tower Pro SG90 servo is included in the design, potentially for a physical indicator or barrier control.
Component List
Arduino Nano
- Microcontroller board based on the ATmega328P
- It has a variety of digital and analog I/O pins.
I2C LCD 16x2 Screen
- A 16x2 character LCD display that uses the I2C communication protocol.
HC-SR04 Ultrasonic Sensor
- An ultrasonic distance sensor that can measure distances by emitting ultrasonic waves and measuring the time taken for the echo to return.
LED: Two Pin (green)
- A basic green LED with an anode and cathode for indicating statuses such as power or signal.
LED: Two Pin (red)
- A basic red LED with an anode and cathode, similar to the green LED, for status indication.
Tower Pro SG90 Servo
- A small and lightweight servo motor capable of precise control.
Wiring Details
Arduino Nano
- D2-D9: Connected to the TRIG and ECHO pins of the four HC-SR04 Ultrasonic Sensors.
- D10: Connected to the Signal pin of the Tower Pro SG90 servo.
- A0-A3: Connected to the anodes of the green LEDs and cathodes of the red LEDs.
- A4 (SDA), A5 (SCL): I2C communication lines connected to the SDA and SCL pins of the I2C LCD 16x2 Screen.
- 5V: Powers the VCC pins of the HC-SR04 Ultrasonic Sensors, the I2C LCD 16x2 Screen, and the Tower Pro SG90 servo.
- GND: Common ground for all components.
I2C LCD 16x2 Screen
- SCL, SDA: I2C communication lines connected to the A4 (SDA) and A5 (SCL) pins on the Arduino Nano.
- VCC (5V), GND: Power supply lines connected to the 5V and GND pins on the Arduino Nano.
HC-SR04 Ultrasonic Sensor
- TRIG: Trigger pin connected to the corresponding digital pins D3, D5, D7, and D9 on the Arduino Nano.
- ECHO: Echo pin connected to the corresponding digital pins D2, D4, D6, and D8 on the Arduino Nano.
- VCC, GND: Power supply lines connected to the 5V and GND pins on the Arduino Nano.
LED: Two Pin (green)
- Anode: Connected to the corresponding analog pins A0, A1, A2, and A3 on the Arduino Nano.
- Cathode: Connected to the GND pin on the Arduino Nano.
LED: Two Pin (red)
- Anode: Connected to the 5V pin on the Arduino Nano.
- Cathode: Connected to the corresponding analog pins A0, A1, A2, and A3 on the Arduino Nano.
Tower Pro SG90 Servo
- Signal: Connected to the D10 pin on the Arduino Nano.
- +5V, GND: Power supply lines connected to the 5V and GND pins on the Arduino Nano.
Documented Code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD objects for two LCDs with their I2C addresses
LiquidCrystal_I2C lcd1(0x27, 16, 2);
LiquidCrystal_I2C lcd2(0x26, 16, 2);
// Define pins for ultrasonic sensors
const int trigPin1 = 2;
const int echoPin1 = 3;
const int trigPin2 = 4;
const int echoPin2 = 5;
const int trigPin3 = 6;
const int echoPin3 = 7;
const int trigPin4 = 8;
const int echoPin4 = 9;
// Define pins for LEDs
const int redLED1 = 10;
const int redLED2 = 11;
const int redLED3 = 12;
const int redLED4 = 13;
const int greenLED1 = A0;
const int greenLED2 = A1;
const int greenLED3 = A2;
const int greenLED4 = A3;
long duration;
int distance;
void setup() {
// Initialize the LCDs
lcd1.init();
lcd2.init();
lcd1.backlight();
lcd2.backlight();
lcd1.setCursor(0, 0);
lcd1.print("Slot 1:");
lcd1.setCursor(0, 1);
lcd1.print("Slot 2:");
lcd2.setCursor(0, 0);
lcd2.print("Slot 3:");
lcd2.setCursor(0, 1);
lcd2.print("Slot 4:");
// Initialize pins for ultrasonic sensors
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
// Initialize pins for LEDs
pinMode(redLED1, OUTPUT);
pinMode(redLED2, OUTPUT);
pinMode(redLED3, OUTPUT);
pinMode(redLED4, OUTPUT);
pinMode(greenLED1, OUTPUT);
pinMode(greenLED2, OUTPUT);
pinMode(greenLED3, OUTPUT);
pinMode(greenLED4, OUTPUT);
}
void loop() {
// Check parking slot 1
int distance1 = getDistance(trigPin1, echoPin1);
lcd1.setCursor(8, 0);
if (distance1 < 10) {
lcd1.print("Occupied");
digitalWrite(redLED1, HIGH);
digitalWrite(greenLED1, LOW);
} else {
lcd1.print("Empty ");
digitalWrite(redLED1, LOW);
digitalWrite(greenLED1, HIGH);
}
// Check parking slot 2
int distance2 = getDistance(trigPin2, echoPin2);
lcd1.setCursor(8, 1);
if (distance2 < 10) {
lcd1.print("Occupied");
digitalWrite(redLED2, HIGH);
digitalWrite(greenLED2, LOW);
} else {
lcd1.print("Empty ");
digitalWrite(redLED2, LOW);
digitalWrite(greenLED2, HIGH);
}
// Check parking slot 3
int distance3 = getDistance(trigPin3, echoPin3);
lcd2.setCursor(8, 0);
if (distance3 < 10) {
lcd2.print("Occupied");
digitalWrite(redLED3, HIGH);
digitalWrite(greenLED3, LOW);
} else {
lcd2.print("Empty ");
digitalWrite(redLED3, LOW);
digitalWrite(greenLED3, HIGH);
}
// Check parking slot 4
int distance4 = getDistance(trigPin4, echoPin4);
lcd2.setCursor(8, 1);
if (distance4 < 10) {
lcd2.print("Occupied");
digitalWrite(redLED4, HIGH);
digitalWrite(greenLED4, LOW);
} else {
lcd2.print("Empty ");
digitalWrite(redLED4, LOW);
digitalWrite(greenLED4, HIGH);
}
delay(1000); // Update every second
}
// Function to get the distance measured by the ultrasonic sensor
int getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
The code provided is for the Arduino Nano microcontroller. It initializes two I2C LCD displays and sets up the pins for the ultrasonic sensors and LEDs. In the main loop, it measures the distance using the ultrasonic sensors and updates the LCD and LED statuses accordingly. The getDistance function is used to calculate the distance based on the duration of the ultrasonic echo.