A comprehensive guide to building a self-contained face recognition system using the ESP32-S3, MicroPython, an OLED display, and SD card execution. Btw dont forget to star this <3
- Introduction
- Hardware Requirements
- Software Requirements
- File Structure
- MicroPython Installation
- Steps to Add OV2640 to ESP32
- How to Run
This project implements a face recognition system using ESP32-S3. It dynamically loads Python scripts and executes them directly from an SD card, enabling easy updates without re-flashing.
Features:
- Face recognition with an ESP32-S3 camera module.
- Modular components (OLED, SD card, GPIO buttons).
- Supports four modes:
- On/Off: Long press to toggle power.
- Register: Capture and register a new face.
- Cancel: Exit registration mode.
- Clear: Delete all registered faces.
| Component | Quantity | Description |
|---|---|---|
| ESP32-S3 Board | 1 | With built-in or external camera. |
| Micro SD Card + Adapter | 1 | To store Python files. |
| SD Card Module | 1 | For SD card interfacing. |
| OLED Display | 1 | I2C-based, SSD1306 128x64. |
| Buttons | 4 | For On/Off, Register, Cancel, Clear. |
| LEDs | 2 | Green and Red LEDs. |
| Buzzer | 1 | For audio feedback. |
| Resistors | As needed | For button pull-up/down circuits. |
| Jumper Wires | Several | For connections. |
| Power Supply | 1 | ESP32-compatible (e.g., 5V via USB). |
| OV2640 Cam Module | 1 | Camera module used for this project. |
Install these tools on your computer:
pip install esptool adafruit-ampy mpremote rshell micropython-uasyncioPlace these files in the /sd/components/ directory:
ssd1306.py: OLED Display Driver.esp32_camera.py: Camera handling for face recognition (use custom camera build).hardware.py: Handles GPIO, buttons, LEDs, and buzzer.
Organize the SD card as follows:
/sd/
├── main.py # Main program
├── components/ # Component drivers and utilities
│ ├── ssd1306.py # OLED driver
│ ├── esp32_camera.py # Camera logic
│ ├── hardware.py # Buttons, LEDs, and buzzer
└── data/ # Registered face data
├── encodings/
Download the appropriate MicroPython firmware for ESP32-S3 from here.
- Install
esptool:pip install esptool
- Erase existing firmware:
esptool.py --chip esp32 erase_flash
- Flash MicroPython:
esptool.py --chip esp32 write_flash -z 0x1000 micropython.bin
The OV2640 camera module has several pins that need to be connected to the ESP32. Here's a common pin mapping:
| OV2640 Pin | ESP32 Pin (Example) | Description |
|---|---|---|
VCC |
3.3V | Power supply (3.3V). |
GND |
GND | Ground. |
D0 - D7 |
GPIO32-GPIO39 | Data lines. |
XCLK |
GPIO0 | Clock signal. |
PCLK |
GPIO21 | Pixel clock. |
VSYNC |
GPIO25 | Vertical sync. |
HREF |
GPIO23 | Horizontal sync. |
SCL |
GPIO22 | I2C clock. |
SDA |
GPIO21 | I2C data. |
RESET |
GPIO15 (Optional) | Reset pin. |
PWDN |
GPIO26 (Optional) | Power down (optional). |
- Use jumper wires or a dedicated ESP32 camera development board (e.g., ESP32-CAM) for connection.
- Double-check pin assignments for your specific ESP32-S3 development board, as they may vary.
Ensure you are using a MicroPython firmware version that supports the camera interface. Here’s how to get started:
- Use a MicroPython build with camera support, such as:
- MicroPython Camera Firmware by Loboris.
- Official ESP32-CAM firmware (check documentation for camera support).
Follow the same process as flashing standard MicroPython firmware:
esptool.py --chip esp32 write_flash -z 0x1000 camera_firmware.bin-
Black Image or No Output:
- Ensure
framesizeandqualitysettings match your camera module's capability. - Verify pin connections and mappings.
- Check the power supply (some cameras require a stable 3.3V).
- Ensure
-
Camera Initialization Fails:
- Double-check the firmware compatibility with OV2640.
- Try reducing the
xclk_freq_hzvalue (e.g., 10 MHz).
-
Error:
cameramodule not found:- Ensure you have flashed a camera-compatible MicroPython firmware.
For more advanced control or performance, consider using the ESP-IDF framework with native ESP32-CAM libraries, as it provides full support for OV2640.
-
Prepare the SD Card:
- Copy
main.pyand thecomponents/folder to the SD card.
- Copy
-
Insert SD Card:
- Insert the SD card into the SD card module.
-
Power On ESP32:
- Long press the On/Off button.
-
Modes:
- Normal Mode: Detects faces.
- Register Mode: Add a new face.
- Clear Mode: Deletes all faces.
-
Debug:
- Use a serial terminal to monitor output.
flowchart TD;
Start["Start / Power On"];
Start --> HardwareCheck["Check Components"];
HardwareCheck -->|Hardware OK| MainMode;
HardwareCheck -->|Hardware Error| ErrorDisplay["Show 'Hardware Error' on OLED"];
MainMode["Main Mode: Detect Face"] -->|Face Detected| AccessGranted["Green LED Blinks + 'Access Granted' on OLED"];
MainMode -->|Face Not Detected| AccessDenied["Red LED Blinks + 'Access Denied' on OLED"];
MainMode --> Register["Register Button Pressed"];
Register --> CaptureFace["Capture Face"];
CaptureFace --> ShowRegister["Show 'Register?' on OLED"];
ShowRegister -->|Cancel Pressed| MainMode;
ShowRegister -->|On/Off Pressed 3x| SaveFace["Save Face to SD"] --> MainMode;
MainMode --> ClearMode["Clear Button Pressed"];
ClearMode --> DeleteFaces["Delete All Faces on SD"] --> MainMode;
MainMode --> ResetMode["Reset Button Pressed"];
ResetMode --> Reinitialize["Reinitialize Hardware"] --> MainMode;
MainMode --> PowerOff["Long Press On/Off"];
PowerOff --> ShutDown["Show 'Shutting Down' on OLED"];