Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

  Smart Parking System Code :-

✅ Detect 5 parking slots using IR sensors
✅ Detect car at entrance using Ultrasonic
✅ Control servo gate
✅ Show all output in Serial Monitor

🚗 Smart Parking System Code (Serial Monitor Output)

#include <Servo.h>

// ------------------- IR Sensor Pins -------------------
int ir1 = 14;
int ir2 = 27;
int ir3 = 26;
int ir4 = 25;
int ir5 = 33;

// ------------------- Ultrasonic Pins -------------------
#define TRIG 4
#define ECHO 5

// ------------------- Servo -------------------
Servo gateServo;
int servoPin = 18;

long duration;
float distance;

void setup() {

Serial.begin(115200);

// IR Sensors as input
pinMode(ir1, INPUT);
pinMode(ir2, INPUT);
pinMode(ir3, INPUT);
pinMode(ir4, INPUT);
pinMode(ir5, INPUT);

// Ultrasonic pins
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);

// Attach servo
gateServo.attach(servoPin);
gateServo.write(0); // Gate initially closed

Serial.println("Smart Parking System Started");
}

void loop() {

// ---------------- Slot Status Check ----------------
int available = 0;

bool slot1 = digitalRead(ir1);
bool slot2 = digitalRead(ir2);
bool slot3 = digitalRead(ir3);
bool slot4 = digitalRead(ir4);
bool slot5 = digitalRead(ir5);

if (slot1 == HIGH) available++;
if (slot2 == HIGH) available++;
if (slot3 == HIGH) available++;
if (slot4 == HIGH) available++;
if (slot5 == HIGH) available++;

Serial.println("--------- Parking Status ---------");

Serial.print("Slot 1: ");
Serial.println(slot1 == HIGH ? "Available" : "Occupied");

Serial.print("Slot 2: ");
Serial.println(slot2 == HIGH ? "Available" : "Occupied");

Serial.print("Slot 3: ");
Serial.println(slot3 == HIGH ? "Available" : "Occupied");

Serial.print("Slot 4: ");
Serial.println(slot4 == HIGH ? "Available" : "Occupied");

Serial.print("Slot 5: ");
Serial.println(slot5 == HIGH ? "Available" : "Occupied");

Serial.print("Total Available Slots: ");
Serial.println(available);

// ---------------- Ultrasonic Distance Measure ----------------
digitalWrite(TRIG, LOW);
delayMicroseconds(2);

digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);

duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;

Serial.print("Entrance Distance: ");
Serial.print(distance);
Serial.println(" cm");

// ---------------- Gate Logic ----------------
if (distance < 15) {

Serial.println("Car detected at entrance!");

if (available > 0) {
Serial.println("Slots Available - Opening Gate");
gateServo.write(90); // Open gate
delay(3000);
gateServo.write(0); // Close gate
}
else {
Serial.println("Parking Full - Gate Closed");
}
}

Serial.println("----------------------------------");
delay(2000);
}

🧠 FULL CODE EXPLANATION (Understand Properly)


🔹 1. Including Servo Library

 
 
#include <Servo.h>
 

This library allows us to control the servo motor angle.


🔹 2. IR Sensor Pin Declaration

 
 
int ir1 = 14;
 

Each IR sensor is connected to a separate GPIO pin.

We use digitalRead() to check whether slot is empty or occupied.

If IR output HIGH → No obstacle → Slot Available
If IR output LOW → Car present → Slot Occupied


🔹 3. Ultrasonic Pins

 
 
#define TRIG 4
#define ECHO 5
 

TRIG sends sound wave.
ECHO receives reflected wave.


🔹 4. Servo Setup

 
 
gateServo.attach(servoPin);
 

This connects servo to GPIO 18.

 
 
gateServo.write(0);
 

Gate starts closed at 0°.


🔹 5. Counting Available Slots

 
 
bool slot1 = digitalRead(ir1);
 

We read all IR sensors.

Then:

 
 
if (slot1 == HIGH) available++;
 

We increase available count only if slot is empty.


🔹 6. Printing Slot Status

Example:

 
 
Serial.println(slot1 == HIGH ? “Available” : “Occupied”);
 

This is called a ternary operator.

If HIGH → print Available
Else → print Occupied


🔹 7. Ultrasonic Distance Formula

 
 
distance = duration * 0.034 / 2;
 

0.034 = speed of sound in cm/µs

Divide by 2 because sound travels forward and back.


🔹 8. Gate Logic

 
 
if (distance < 15)
 

If car is within 15 cm of entrance:

Check:

 
 
if (available > 0)
 

If slots available:
→ Open gate (90°)

Else:
→ Keep gate closed


📊 Serial Monitor Output Example

When some slots empty:

 
 
——— Parking Status ———
Slot 1: Available
Slot 2: Occupied
Slot 3: Available
Slot 4: Occupied
Slot 5: Available
Total Available Slots: 3
Entrance Distance: 10 cm
Car detected at entrance!
Slots Available – Opening Gate
———————————-
 

When parking full:

 
 
Total Available Slots: 0
Entrance Distance: 8 cm
Car detected at entrance!
Parking Full – Gate Closed
 

🎓 Viva Explanation (Short Version)

“This system uses IR sensors to detect parking slot occupancy and an ultrasonic sensor to detect vehicle presence at the entrance. The ESP32 counts available slots and controls a servo motor to open the gate only when space is available. All system status is displayed in the Serial Monitor.”



Scroll to Top