Translate

ESP32-CAM Face Recognition System

 1. Introduction

In this project, we will build a Face Detection and Recognition System using ESP32-CAM

The system will:

  • Detect human faces
  • Allow enrolling faces
  • Recognize stored faces

2. Components

  • ESP32-CAM Module (AI Thinker)
  • FTDI Programmer (USB to Serial)
  • Jumper wires
  • 5V 2A Power Supply

3. Circuit and Connections

ESP32 CAM Pinout Diagram

Pinout Diagram FTDI Programmer (USB to Serial)
Cable Pinout Diagram FTDI to USB

ESP32-CAM ↔ FTDI

  • 5V → 5V
  • GND → GND
  • TX → U0R
  • RX → U0T
  • GPIO0 → GND (for uploading)
🔧 Main Pins in FTDI
  • VCC → Power (5V or 3.3V)
  • GND → Ground
  • TX → Transmit
  • RX → Receive

🔗 Why It is Needed for ESP32-CAM

👉 ESP32-CAM does NOT have USB port

So:

  • You cannot connect it directly to PC ❌
  • You need FTDI to upload code ✔

🔌 Connection with ESP32-CAM

FTDI    ESP32-CAM
VCC             5V
GND           GND
TX            U0R
RX            U0T

⚠️ Also:
👉 GPIO0 → GND (for uploading)

⚠️ Critical Steps (Don’t Miss)
Before Upload
  • Connect GPIO0 → GND
  • Select board: AI Thinker ESP32-CAM
  • Click Upload

After Upload

  • Remove GPIO0 from GND
  • Press RESET button

⚠️ Important Tips
  • Use 5V output (ESP32-CAM needs power)
  • Do NOT power from 3.3V (unstable)
  • Press RESET after upload
  • Remove GPIO0 after upload

🧰 Common Names

FTDI may also be called:

  • USB to TTL Converter
  • Serial Adapter
  • FT232RL Module

🎯 Real-Life Example

Think like this:

👉 FTDI = Translator

  • Computer speaks USB
  • ESP32 speaks Serial
  • FTDI translates between them

Do You Always Need It?

✔ Yes → For ESP32-CAM
❌ No → For Arduino Uno (already has USB)


🚀 Alternative (Advanced)

You can also use:

  • Arduino Uno as programmer
  • CP2102 module

Conclusion

👉 FTDI Programmer is essential to:

  • Upload code
  • Communicate with ESP32-CAM

Without it → You cannot program ESP32-CAM ❌


⚠️ After uploading:

  • Remove GPIO0 from GND
  • Press RESET button

4. Circuit Working

  • ESP32-CAM captures live video
  • Processes image using built-in AI
  • Detects faces in real-time
  • Stores face data when enrolled
  • Matches faces during recognition

5. Code

#include "esp_camera.h"

#include <WiFi.h>
// ===================
// Select Camera Model
// ===================
#define CAMERA_MODEL_AI_THINKER
#include "camera_pins.h"
// ===========================
// Enter your WiFi credentials
// ===========================
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
// ===========================
// Start Camera Web Server
// ===========================
void startCameraServer();
// ===========================
// Setup
// ===========================
void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(false);
  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;
  // Frame settings
  if (psramFound()) {
    config.frame_size = FRAMESIZE_QVGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_QQVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  // Init Camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed");
    return;
  }
  // WiFi Connection
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("Camera Ready! Open this URL: http://");
  Serial.println(WiFi.localIP());
  // Start streaming server
  startCameraServer();
}
void loop() {
  delay(10000);
}

👉 Use default example: File → Examples → ESP32 → Camera → CameraWebServer   or copy paste above Code


Important Settings (VERY IMPORTANT)

Inside code, enable:

#define CAMERA_MODEL_AI_THINKER

WiFi Setup

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

6. Code Working

  • ESP32 connects to WiFi
  • Starts camera web server
  • Displays IP address in Serial Monitor
  • Open IP in browser
  • You will see:
    • Live video
    • Face detection option
    • Face recognition option

7. Tips

  • Use external 5V supply (important ⚠️)
  • Good lighting improves detection
  • Keep face straight and clear
  • Avoid backlight
  • Use Chrome browser for best results

8. Uses

  • Face recognition learning system
  • AI vision projects
  • Smart surveillance
  • Attendance system base
  • Security system foundation

9. Conclusion

This project successfully demonstrates Face Detection and Recognition using ESP32-CAM. It forms the foundation for advanced systems like smart door locks and attendance systems.


How to Store camera_pins.h File (ESP32-CAM)

Method 1 (BEST – Recommended 👍)

👉 You DON’T need to create it manually

Follow this:

  1. Open Arduino IDE
  2. Go to:
    File → Examples → ESP32 → Camera → CameraWebServer
  3. Click it

👉 Now Arduino will open multiple tabs:

  • .ino file
  • app_httpd.cpp
  • 👉 camera_pins.h (already there ✅)

Nothing to store manually
✔ Everything is pre-arranged


Method 2 (Manual Creation – If Needed)

👉 Only do this if:

  • File missing ❌
  • You are writing custom code

🔧 Step-by-step

1. Create new tab

In Arduino IDE:

  • Click small dropdown (top right)
  • Click “New Tab”
  • Name it exactly:

👉 camera_pins.h

⚠️ Must be .h (not .ino)


2. Paste this code

#if defined(CAMERA_MODEL_AI_THINKER)

#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

#endif

3. Save Project

  • Press Ctrl + S
  • Arduino stores .ino + .h together

3.1. What These Pins Mean

  • Y2–Y9 → Camera data pins
  • XCLK → Camera clock
  • VSYNC → Frame sync
  • HREF → Line sync
  • PCLK → Pixel clock
  • SIOD/SIOC → Camera control (I2C)

3.2. ⚠️ Important Point

👉 If you use wrong pin configuration:

  • Camera will NOT work ❌
  • You will get errors like:
    • Camera init failed
    • No image

3.3. ✅ Do You Need to Create It Manually?

👉 Usually NO

If you use:

  • CameraWebServer example

Then:
camera_pins.h is already included automatically


3.4. 🧾 When You Need It

You need this file only if:

  • You copy code manually
  • Or create custom project
  • Or error says:
    👉 camera_pins.h not found

3.5. 🎯 Final Understanding

👉 camera_pins.h =
Bridge between ESP32 and camera hardware

Without it → camera cannot communicate properly.


📂 How Arduino Stores Files (Important)

When you save project:

YourProjectFolder/
├── YourProject.ino
├── camera_pins.h

✔ Both files are in same folder
✔ Arduino automatically links them


⚠️ Common Mistakes

❌ Naming file as:

  • camera_pins.h.txt
  • camera_pins

❌ Keeping file outside folder

❌ Using wrong camera model


How to Check It’s Working

If correct:

  • Code compiles without error ✅
  • Camera starts successfully ✅

If wrong:

  • Error:
    👉 camera_pins.h: No such file or directory

🎯 Final Tip (Very Important)

✔ Always use CameraWebServer example
✔ Avoid manual file creation (less errors)


📷 How to Know if Face is Stored & Recognized (ESP32-CAM)

1. During Face Enrollment (Storing Face)

👉 After clicking “Enroll Face” in browser:

🔍 What you will see:

  • Face box appears on screen
  • Multiple captures happen automatically
  • Finally:

👉 ✔ “Enrolled Face” / “Enroll Success” message appears


🧠 Visual Confirmation

  • Face ID number gets assigned (like: ID 0, ID 1…)
  • Green box appears around face

2. After Enrollment (Recognition Stage)

👉 When you show your face again:

✔ If Face is Recognized:

  • Green box appears
  • You will see:

👉 “Match Face” or ID number (e.g., Face ID: 0)

✔ This means:
👉 Face is stored AND recognized successfully


❌ If Face is NOT Recognized:

  • Red box appears
  • Shows:

👉 “Unknown”


3. Serial Monitor Output (Extra Check)

Open Serial Monitor (115200):

You may see:

  • Device connected
  • Camera started

👉 But face match details mostly show in web interface, not serial.


⚠️ Important Points

  • Face is stored in ESP32 memory (RAM/flash)
  • If power OFF → faces may be lost ❌ (depends on version)
  • You need to enroll again sometimes

🔧 Troubleshooting

❌ Problem: Face not recognized after enroll

✔ Solution:

  • Improve lighting 💡
  • Keep face straight
  • Re-enroll properly

❌ Problem: Always showing “Unknown”

✔ Solution:

  • Enable Face Recognition toggle
  • Not just Face Detection

❌ Problem: Enroll button not working

✔ Solution:

  • Use Chrome browser
  • Check WiFi stability

🎯 Simple Understanding

👉 Stored face = You clicked Enroll and got success
👉 Recognized face = System shows ID / Match


Final Confirmation Rule

✔ If you see:
👉 Face ID number → SUCCESS

❌ If you see:
👉 Unknown → Not recognized

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.