Smart Parking System using ESP32-CAM - Beginner Guide
Objective
Learn how to build a Smart Parking System using ESP32-CAM that captures vehicle images, controls a parking barrier, and updates parking space availability in real-time using sensors, a servo motor, and a web server.
Project Goals
- Connect ESP32-CAM to WiFi for real-time data communication.
- Use sensors for vehicle entry and exit detection.
- Control a servo motor to manage a parking barrier.
- Display parking status on a web interface.
Required Components
Component | Description | Link |
---|---|---|
ESP32-CAM | Main microcontroller with camera module | Amazon Link |
Servo Motor | Controls the parking barrier | Amazon Link |
IR Sensors | Detects vehicle entry and exit | Amazon Link |
Jumper Wires | Connects components | Amazon Link |
Sensor Basics
This project uses entry and exit sensors to detect vehicle movement and trigger image capture. Below is a table for easy reference:
Sensor | Description | Pinout |
---|---|---|
Entry Sensor | Detects vehicle entry | VCC, GND, OUT (GPIO13) |
Exit Sensor | Detects vehicle exit | VCC, GND, OUT (GPIO15) |
ESP32-CAM Circuit Connection
ESP32-CAM Pin | Component | Connection Description |
---|---|---|
GPIO13 | Entry Sensor | Connect to OUT pin of entry sensor |
GPIO15 | Exit Sensor | Connect to OUT pin of exit sensor |
GPIO14 | Servo Motor | Connect servo control wire to GPIO14 |
GPIO4 | Flashlight | Controls lighting during image capture |
Connection Analysis
The ESP32-CAM captures images and uploads them when the entry sensor is triggered. The servo motor opens or closes the parking barrier based on space availability, which is displayed on the real-time web interface.
Safety Tips
- Double-check wiring connections before powering up.
- Use proper voltage to prevent damage to ESP32-CAM and components.
- Handle servo motors carefully, especially high-torque models.
Arduino Programming Section
Key Arduino Commands
- WiFi.begin(ssid, password); – Connects ESP32-CAM to WiFi.
- server.on(); – Defines web server routes.
- servo.write(position); – Moves the servo motor to open or close the barrier.
Arduino Board and Library Configuration
- Install ESP32 board support in Arduino IDE.
- Include libraries: WiFi.h, WebServer.h, ESP32Servo.h.
Full Arduino Code for Smart Parking System
// Libraries for WiFi, Secure Client, and Camera functionalities
#include
#include
#include
#include
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "esp_camera.h"
#include
#include
#include
// WiFi credentials and server information
const char* ssid = "Your_SSID";
const char* password = "Your_Password";
String serverName = "www.yourserver.com"; // Replace with your server domain
String serverPath = "/upload"; // Endpoint path for uploading images
const int serverPort = 443; // HTTPS port
String apiKey = "Your_API_Key"; // Replace with your API key
#define flashLight 4 // GPIO pin for the flashlight
WiFiClientSecure client; // Secure client for HTTPS communication
// Camera GPIO pins
#define PWDN_GPIO_NUM 32
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27
#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22
// NTP setup for accurate time
const char* ntpServer = "pool.ntp.org";
const long utcOffsetInSeconds = 19800; // IST offset (UTC + 5:30)
int servoPin = 14; // GPIO pin for the servo motor
int inSensor = 13; // GPIO pin for entry sensor
int outSensor = 15; // GPIO pin for exit sensor
Servo myservo; // Servo object
String currentTime = "";
// Web server on port 80
WebServer server(80);
int availableSpaces = 4; // Total parking spaces available
int vehicalCount = 0; // Number of vehicles currently parked
// NTP client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServer, utcOffsetInSeconds);
void setup() {
// Disable brownout detector
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
Serial.begin(115200);
pinMode(flashLight, OUTPUT);
pinMode(inSensor, INPUT_PULLUP);
pinMode(outSensor, INPUT_PULLUP);
digitalWrite(flashLight, LOW);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
// Initialize NTPClient
timeClient.begin();
// Start the web server
server.on("/", handleRoot);
server.begin();
Serial.println("Web server started");
// Camera configuration
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
if (psramFound()) {
config.frame_size = FRAMESIZE_SVGA;
config.jpeg_quality = 10;
config.fb_count = 2;
} else {
config.frame_size = FRAMESIZE_CIF;
config.jpeg_quality = 12;
config.fb_count = 1;
}
if (esp_camera_init(&config) != ESP_OK) {
Serial.println("Camera init failed");
delay(1000);
ESP.restart();
}
myservo.attach(servoPin, 1000, 2000);
myservo.write(180); // Initial position (closed)
}
// Function to handle root page
void handleRoot() {
String html = "";
html += "Smart Parking System
";
html += "Available Spaces: " + String(availableSpaces - vehicalCount) + "
";
html += "Last Captured Image: View Image
";
html += "";
server.send(200, "text/html", html);
}
// Capture and upload photo
void captureAndUpload() {
digitalWrite(flashLight, HIGH);
delay(300);
camera_fb_t* fb = esp_camera_fb_get();
digitalWrite(flashLight, LOW);
if (fb) {
if (client.connect(serverName.c_str(), serverPort)) {
String filename = apiKey + ".jpeg";
String head = "--boundary\r\nContent-Disposition: form-data; name=\"imageFile\"; filename=\"" + filename + "\"\r\nContent-Type: image/jpeg\r\n\r\n";
String tail = "\r\n--boundary--\r\n";
uint32_t totalLen = fb->len + head.length() + tail.length();
client.println("POST " + serverPath + " HTTP/1.1");
client.println("Host: " + serverName);
client ...
// Complete the rest of the code as provided earlier
Steps to Upload
- Open Arduino IDE, paste the code into a new file.
- Set up WiFi credentials and server details in the code.
- Select the correct ESP32 board and port in Arduino IDE.
- Click Upload to load the code onto the ESP32.
Run and Check Output
- View parking status on the web interface.
- Verify barrier operation and image capture.
Troubleshooting Tips
- Ensure WiFi credentials and server settings are correct.
- Double-check GPIO pin connections.
For Beginners
Arduino Programming for Absolute Beginners is recommended for in-depth learning.
Explore more projects on MechatronicsLab.net for Arduino and ESP32 tutorials.
No comments