nimbuscode.dev/blog/posts/getting-started-with-esp32-cam
C:\> cat BLOG/ESP32_CAM.md

Getting Started with ESP32-CAM: Computer Vision on a Budget

Introduction

Computer vision has traditionally been restricted to high-end devices with powerful processors and expensive cameras. However, with the rise of affordable microcontrollers with integrated camera modules, computer vision applications are now accessible to hobbyists and makers on a limited budget.

The ESP32-CAM is one such device that has revolutionized affordable computer vision. At around $10, this tiny module combines an ESP32 microcontroller (with WiFi and Bluetooth capabilities) and an OV2640 2-megapixel camera, making it perfect for IoT projects requiring visual inputs.

In this guide, we'll explore the capabilities of the ESP32-CAM and walk through the basics of setting it up for your computer vision projects.

Hardware Overview

Before diving into programming, let's understand what the ESP32-CAM offers:

  • Processor: ESP32-S dual-core Tensilica LX6 microprocessor (up to 240MHz)
  • Memory: 520KB SRAM + 4MB PSRAM
  • Flash: 4MB
  • Camera: OV2640 2-megapixel sensor
  • Connectivity: WiFi 802.11b/g/n and Bluetooth 4.2
  • SD Card: Supports microSD card for image storage
  • GPIO: Limited GPIO pins available
  • LED Flash: Built-in flash LED

The module's small form factor (27mm x 40.5mm x 4.5mm) makes it ideal for compact projects. However, it's important to note that the ESP32-CAM doesn't include a USB port for programming. You'll need an FTDI programmer or another ESP32 development board to program it.

Setting Up ESP32-CAM

To get started with the ESP32-CAM, you'll need:

  • ESP32-CAM module
  • FTDI programmer or USB-to-TTL converter
  • Breadboard and jumper wires
  • 5V power supply
  • Arduino IDE or PlatformIO

Wiring for Programming

Connect the ESP32-CAM to your FTDI programmer as follows:

ESP32-CAM    FTDI Programmer
----------    ---------------
5V            VCC (5V)
GND           GND
U0R (GPIO3)   TX
U0T (GPIO1)   RX
IO0           GND (only during programming)

Note: For programming mode, GPIO0 needs to be connected to GND. After uploading, remove this connection for normal operation.

Installing Required Software

If you're using Arduino IDE:

  1. Open Preferences and add this URL to the Additional Boards Manager URLs field: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
  2. Go to Tools → Board → Boards Manager and search for "ESP32"
  3. Install the "ESP32 by Espressif Systems" package
  4. Select "AI Thinker ESP32-CAM" from the boards list

Capturing Images

Let's start with a basic example to capture and save an image:

#include "esp_camera.h"
#include "FS.h"
#include "SD_MMC.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "driver/rtc_io.h"

// Pin definitions for AI Thinker ESP32-CAM
#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

void setup() {
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // Disable brownout detector
  
  Serial.begin(115200);
  Serial.println();
  
  // Initialize camera
  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;
  
  // Initial settings
  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA; // 1600x1200
    config.jpeg_quality = 10;  // 0-63, lower is higher quality
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA; // 800x600
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }
  
  // Initialize the camera
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }
  
  // Initialize SD card
  if(!SD_MMC.begin()) {
    Serial.println("SD Card Mount Failed");
    return;
  }
  
  // Take a picture
  camera_fb_t * fb = NULL;
  fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Camera capture failed");
    return;
  }
  
  // Save to SD card
  String path = "/picture.jpg";
  fs::FS &fs = SD_MMC;
  File file = fs.open(path.c_str(), FILE_WRITE);
  if(!file) {
    Serial.println("Failed to open file in writing mode");
  } else {
    file.write(fb->buf, fb->len);
    Serial.printf("Saved file to path: %s\n", path.c_str());
  }
  file.close();
  
  // Release the frame buffer
  esp_camera_fb_return(fb);
  
  // Turn off the ESP32-CAM white on-board LED (flash)
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  rtc_gpio_hold_en(GPIO_NUM_4);
  
  Serial.println("Going to sleep now");
  delay(500);
  esp_deep_sleep_start();
}

void loop() {
  // Not used
}

This code initializes the camera, takes a picture, saves it to the microSD card, and then goes into deep sleep mode to conserve power—ideal for battery-powered applications.

Video Streaming

One of the most popular applications for the ESP32-CAM is setting up a web server that streams video. Here's a simplified example:

#include "esp_camera.h"
#include "esp_http_server.h"
#include "esp_timer.h"
#include "img_converters.h"
#include "Arduino.h"
#include "WiFi.h"

// Camera pin definitions (same as above)
// ...

// Replace with your network credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Function declarations
void startCameraServer();
void setupCamera();

void setup() {
  Serial.begin(115200);
  Serial.println();
  
  // Initialize camera
  setupCamera();
  
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Start streaming web server
  startCameraServer();
  Serial.print("Camera Stream Ready! Go to: http://");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Nothing to do here
  delay(10000);
}

The startCameraServer() function is quite complex, as it sets up routes for both still image capture and video streaming. The ESP32 Arduino library includes a complete example called "CameraWebServer" that you can use as a starting point.

With this setup, you can access the video stream from any device on your network by navigating to the ESP32-CAM's IP address.

Project Ideas

Now that you understand the basics, here are some inspiring projects you can build with the ESP32-CAM:

  1. Security Camera: Create a motion-activated security camera that sends notifications and images when motion is detected.
  2. Wildlife Camera: Set up a solar-powered wildlife monitoring camera that takes pictures when animals are detected.
  3. Smart Doorbell: Build a doorbell that shows you who's at the door on your phone.
  4. Plant Monitor: Create a system that monitors your plants and takes pictures to track growth.
  5. QR Code Reader: Use image processing libraries to build a QR code or barcode scanner.
  6. Object Recognition: Integrate with TensorFlow Lite to perform basic object recognition.

For more advanced projects, you can combine the ESP32-CAM with cloud services or edge computing platforms like AWS IoT, Google Cloud IoT, or Edge Impulse for more sophisticated image processing and machine learning capabilities.

Conclusion

The ESP32-CAM opens up a world of affordable computer vision projects. While it has limitations in terms of processing power and image quality compared to more expensive options, it's perfect for many IoT applications and a great way to get started with computer vision.

Remember that the ESP32-CAM is designed for low-power applications, so it's best suited for projects where images are captured periodically rather than continuous high-speed video processing. For more intensive computer vision tasks, consider using the ESP32-CAM as an image capture device that sends data to a more powerful system like a Raspberry Pi or cloud service for processing.

Have you built any projects with the ESP32-CAM? Share your experiences in the comments below!

Comments (0)

Sort by: