ESP32-CAM PIR Motion Detector with Photo Capture (saves to microSD card)

In this project, we’re going to make a motion sensor detector with photo capture using an ESP32-CAM. When your PIR sensor detects motion, it wakes up, takes a photo and saves it in the microSD card.

ESP32-CAM Motion Detection with Photo Capture (saves to microSD card)

This project is very similar with a previous one, but after so many requests, we added a PIR motion sensor to the circuit. So, when motion is detected a picture is taken and saved on the microSD card.

Other ESP32-CAM projects and tutorials:

We have a similar project using a Raspberry Pi and a camera module:

Watch the Video Tutorial

You can watch the video tutorial or continue reading for the complete project instructions.

Parts Required

For this project, you’ll need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Project Overview

ESP32-CAM Motion Detector with Photo Capture Project Overview

Here is a quick overview on how the project works.

  • The ESP32-CAM is in deep sleep mode with external wake up enabled.
  • When motion is detected, the PIR motion sensor sends a signal to wake up the ESP32.
  • The ESP32-CAM takes a photo and saves it on the microSD card.
  • It goes back to deep sleep mode until a new signal from the PIR motion sensor is received.

Recommended reading: ESP32 Deep Sleep with Arduino IDE and Wake Up Sources

Formatting MicroSD Card

The first thing we recommend doing is formatting your microSD card. You can use the Windows formatter tool or any other microSD formatter software.

1. Insert the microSD card in your computer. Go to My Computer and right click in the SD card. Select Format as shown in figure below.

Formatting your microSD card Windows

2. A new window pops up. Select FAT32, press Start to initialize the formatting process and follow the onscreen instructions.

Formatting your microSD card Windows

Note: according to the product specifications, the ESP32-CAM should only support 4 GB SD cards. However, we’ve tested with 16 GB SD card and it works well.

Installing the ESP32 add-on

We’ll program the ESP32 board using Arduino IDE. So, you need the Arduino IDE installed as well as the ESP32 add-on:

ESP32-CAM Take Photo with PIR Sketch

Copy the following code to your Arduino IDE.

/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-cam-pir-motion-detector-photo-capture/
 
  IMPORTANT!!!
   - Select Board "AI Thinker ESP32-CAM"
   - GPIO 0 must be connected to GND to upload a sketch
   - After connecting GPIO 0 to GND, press the ESP32-CAM on-board RESET button to put your board in flashing mode
 
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*********/
 
#include "esp_camera.h"
#include "Arduino.h"
#include "FS.h"                // SD Card ESP32
#include "SD_MMC.h"            // SD Card ESP32
#include "soc/soc.h"           // Disable brownour problems
#include "soc/rtc_cntl_reg.h"  // Disable brownour problems
#include "driver/rtc_io.h"
#include <EEPROM.h>            // read and write from flash memory
// define the number of bytes you want to access
#define EEPROM_SIZE 1
 
RTC_DATA_ATTR int bootCount = 0;

// Pin definition for 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
 
int pictureNumber = 0;
  
void setup() {
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  Serial.begin(115200);
 
  Serial.setDebugOutput(true);
 
  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;
  
  pinMode(4, INPUT);
  digitalWrite(4, LOW);
  rtc_gpio_hold_dis(GPIO_NUM_4);
 
  if(psramFound()){
    config.frame_size = FRAMESIZE_UXGA; // FRAMESIZE_ + QVGA|CIF|VGA|SVGA|XGA|SXGA|UXGA
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    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 with error 0x%x", err);
    return;
  }
 
  Serial.println("Starting SD Card");
 
  delay(500);
  if(!SD_MMC.begin()){
    Serial.println("SD Card Mount Failed");
    //return;
  }
 
  uint8_t cardType = SD_MMC.cardType();
  if(cardType == CARD_NONE){
    Serial.println("No SD Card attached");
    return;
  }
   
  camera_fb_t * fb = NULL;
 
  // Take Picture with Camera
  fb = esp_camera_fb_get();  
  if(!fb) {
    Serial.println("Camera capture failed");
    return;
  }
  // initialize EEPROM with predefined size
  EEPROM.begin(EEPROM_SIZE);
  pictureNumber = EEPROM.read(0) + 1;
 
  // Path where new picture will be saved in SD Card
  String path = "/picture" + String(pictureNumber) +".jpg";
 
  fs::FS &fs = SD_MMC;
  Serial.printf("Picture file name: %s\n", path.c_str());
 
  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); // payload (image), payload length
    Serial.printf("Saved file to path: %s\n", path.c_str());
    EEPROM.write(0, pictureNumber);
    EEPROM.commit();
  }
  file.close();
  esp_camera_fb_return(fb);
  
  delay(1000);
  
  // Turns off the ESP32-CAM white on-board LED (flash) connected to GPIO 4
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  rtc_gpio_hold_en(GPIO_NUM_4);

  esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 0);
 
  Serial.println("Going to sleep now");
  delay(1000);
  esp_deep_sleep_start();
  Serial.println("This will never be printed");
} 
 
void loop() {
 
}

View raw code

This code is very similar to one of our previous ESP32-CAM projects, but it enables external wake up on GPIO 13.

esp_sleep_enable_ext0_wakeup(GPIO_NUM_13,0);

To learn more about the code, go to the following project:

ESP32-CAM Upload Code

To upload code to the ESP32-CAM board, connect it to your computer using an FTDI programmer. Follow the next schematic diagram:

Many FTDI programmers have a jumper that allows you to select 3.3V or 5V. Make sure the jumper is in the right place to select 5V.

Important: GPIO 0 needs to be connected to GND so that you’re able to upload code.

ESP32-CAMFTDI Programmer
GNDGND
5VVCC (5V)
U0RTX
U0TRX
GPIO 0GND

To upload the code, follow the next steps:

1) Go to Tools > Board and select AI-Thinker ESP32-CAM.

2) Go to Tools > Port and select the COM port the ESP32 is connected to.

3) Then, click the upload button to upload the code.

4) When you start to see these dots on the debugging window as shown below, press the ESP32-CAM on-board RST button.

After a few seconds, the code should be successfully uploaded to your board.

Schematic Diagram

ESP32-CAM with PIR Motion Sensor Circuit Breadboard

Assemble all the parts as shown in the following schematic diagram.

ESP32-CAM with PIR Motion Sensor Circuit Diagram
Thanks to David Graff for sharing the schematic diagram for this project

If you prefer, you can follow the Fritzing diagram instead.

ESP32-CAM with PIR Motion Sensor Schematic Diagram Fritzing

To prevent problems during upload, we recommend assembling the circuit only after uploading the code.

Demonstration

After uploading de code and assembling the circuit, insert a formatted microSD card and apply power to your circuit – you can use a portable charger, for example.

Powering ESP32-CAM with powerbank

Then, press the reset (RST) button, and it should start working. When it detects motion, it turns on the flash, takes a photo and saves it on the microSD card.

ESP32-CAM takes photo when detects motion with PIR sensor

Experiment with this circuit several times to make sure that it is working. Then, insert the microSD card to your computer to see the captured photos.

ESP32-CAM takes photo when detects motion with PIR sensor photo demonstration

Here’s an example:

ESP32-CAM photo example captured

Now you can finish this project the way you want, you can either use a dummy camera and insert your ESP32-CAM with the PIR motion sensor, or you can build your own enclosure.

ESP32-CAM PIR Motion Detector with Photo Capture with fake dummy camera

You can also apply the concepts learned in this tutorial in your own projects.

Troublehsooting

If you’re getting any of the following errors, read our ESP32-CAM Troubleshooting Guide: Most Common Problems Fixed

  • Failed to connect to ESP32: Timed out waiting for packet header
  • Camera init failed with error 0x20001 or similar
  • Brownout detector or Guru meditation error
  • Sketch too big error – Wrong partition scheme selected
  • Board at COMX is not available – COM Port Not Selected
  • Psram error: GPIO isr service is not installed
  • Weak Wi-Fi Signal
  • No IP Address in Arduino IDE Serial Monitor
  • Can’t open web server
  • The image lags/shows lots of latency

Learn how to program and build 17 projects with the ESP32-CAM using Arduino IDE DOWNLOAD »

Learn how to program and build 17 projects with the ESP32-CAM using Arduino IDE DOWNLOAD »

Wrapping Up

We hope you’ve liked this project. For more ESP32-CAM projects you can subscribe to our newsletter. If you don’t have an ESP32-CAM yet, you can get one for approximately $6.

If there is any project you’d like to see with the ESP32-CAM or if you’d like to share your project with us, write a comment in the comment’s section below.

We have more projects and tutorials about the ESP32-CAM that you may like:

Thank you for reading.



Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

108 thoughts on “ESP32-CAM PIR Motion Detector with Photo Capture (saves to microSD card)”

    • Hi,
      That’s a really cool project !
      I’ve tried it but there was a problem.
      The camera doesn’t stop taking picutres.
      I turn the system on and it just takes pictures on its own every 3 seconds.
      Do you know what the problem could be ?

      Best Regards.

      Reply
      • Hi.
        It’s probably because the PIR motion sensor is being triggered every 3 seconds. Double check the connections of the PIR motion sensor.
        To make sure the ESP32-CAM is working properly, you can try attaching a pushbutton instead of the PIR sensor and see if it works as expected.
        Also, check with a multimeter if the PIR motion sensor is working as expected.
        Regards,
        Sara

        Reply
  1. Hi love all your articles, thought you mite like to do a project using the small e ink modules 1.5 inches type maybe thanks so much .
    Jeff.

    Reply
  2. Just curious why you are using the elaborate setup to pull the pin low with the PIR instead of just connecting it directly and pulling it high?

    Reply
  3. Is it possible to use only the ESP32-CAM as a motion sensor to send an email when it detects motion? In my case, it won’t be asleep/need to wake up, it will be on power supply and on waiting for a client to connect to stream video.

    thank you. nice website.

    Reply
    • Hi.
      I’m not sure if the ESP32-CAM is powerful enough to detect motion on its own (i’ve never tried it).
      Sending an image via email will be one of our next projects.
      Regards,
      Sara

      Reply
    • Yes, it is possible. I realized, with due limits, a motion detector without the aid of the PIR, but processing the images (frame). The purpose was didactic and not professional, but it works 🙂
      forum.arduino.cc/index.php?topic=623310.msg4299034#msg4299034

      Reply
    • The 2N3904 is a very generic small silicon NPN transistor being used as a switch. Almost any cheap silicon NPN transistor ought to work, since you’re not using it for audio or RF purposes. Amazon lists a bag of 200 of them for less than a nickel ($0.05) each

      Reply
  4. Hello,
    Is it possible to change the code because I will not be using PIR sensor. Im thinking if it is possible that the esp32 can be made to automatically take pictures once it is turn on. and take pictures every 2 seconds. What do I need to change in the Codes. thanks

    Reply
  5. Hi, great article.

    Question: is it possible in this esp32 board that you plug in directly an e-ink raw display like WaveShare and drive it with the esp-32?

    Reply
    • Hi Nik.
      Many of the pins exposed in this ESP32 board are being used either by the camera or by the microSD card slot.
      So, it can be very difficult and tricky to setup a display like that with this ESP32-CAM.
      (I’m not saying it is not possible, but it should be very tricky).
      Regards,
      Sara

      Reply
  6. Hi there, i am a newbie. The article it is just the Top.
    I have this problem:
    ————————————————————————————
    /esp-who/examples/single_chip/esp32-camera-screen/main/app_main.cpp:41:20: fatal error: dl_lib.h: No such file or directory
    ————————————————————————————-
    i just comented the:
    //#include “dl_lib.h”
    and it work ok.
    Any idea?
    And it is possible to disable the flash light? Thnx.

    Reply
  7. I can upload the sketch OK but cannot get the camera to work. Just grounding GPIO13 should cause the flash and camera to record a photo but nothing happens. Do you think I have got a faulty ESP32-CAM board?

    Reply
  8. Just thinking about it, to wake up from deep sleep your code indicates that the pin is to be turned low by the PIR when movement is detected. But for this to work it will need an external pullup resistor so that it is normally high. When movement is detected by the PIR it will then pull this pin low and wake it up from deep sleep.
    esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 0);
    The pullup resistor would need to connect to 3.3V and not 5V.

    Reply
    • The U0R and U0T are the U(ART)0R(X) and U(ART)0T(X) ports/pins on the ESP32. The ESP32 also has a UART1 (GPIO9/10, neither of which are broken out) and a UART2 (GPIO 16/17 but only GIPO16/RX2 is broken out.)

      Reply
      • Hi Micro, if I know that the esp32 has several serial ports, but my question is about the esp32 cam, I want to know if it has more serial ports because seeing the data sheet says it only has one UOT and one UOR is why my doubt.

        Also that you recommend me to connect with a sim800l and send commands at from esp32 cam 🙂

        Thanks in advance 🙂

        Reply
  9. in the beginning i had problem compiling the code but after reading in the comment section about the l.lib.h that can be commented,i did that and uploaded the code and is working perfect now.thank you guys,i appreciated it.regards.

    Reply
    • Just comment out that library and you’re ready to go… It was already explained in some other comments and even one above yours 😉

      Reply
  10. So if I understand correctly , there is nothing in the code that inhibits flash for this project? I really would like to set it up on a warehouse but I need it without flash
    Thanks again for all you guys do!

    Reply
  11. Hi guys,
    Trying to run this ESP-Cam motion sensor app. On compile, I get “dl_lib.h No such file or directory”. Your first web server app works fine. Obviously, I’m missing a library or two.
    Where do you reference these libraries?

    Reply
  12. Hi Sara and Rui,

    Can I use the PIR Motion Sensor (HC-SR501) in this project?

    If I can use it, do I have to modify the circuit, or the code?

    Best,
    Joel

    Reply
  13. The ESP32 doesn’t stay in sleep mode. Therefore I’m getting numerous photos saved to the SD card because the code is automatically resetting again.
    Would this be because the PIR is constantly on LOW and so PIN 13 is always 0. I have kept very still but it goes round again and takes and saves a photo.

    Reply
  14. Solved. It was a question of a reliable constant power supply. So, instead of using the laptop or a mains 5v adapter plug to power the device I used a YuRobot 545043 breadboard power supply fixed to a 1/4 size (420 points) breadboard. It now works smoothly at a constant 5v and without any glitches. I’ve yet to see how long the 9v battery will last though!
    Excellent sketch. I’ve also got it to send me email with photo attachment.

    Reply
    • Hi Malcom.
      Thanks for sharing that.
      Can you tell me what approach did you use to send the photo to your email?
      Regards,
      Sara

      Reply
      • Hi Sara & Kilo
        You can get a good idea how to do this with :
        youtube.com/watch?v=ywCL9zDIrT0&feature=youtu.be
        and
        bitsnblobs.com/motion-triggered-image-capture-with-email—esp32-cam

        Obviously you will need to do a bit of tweaking to suit your own requirements.

        Let me know if you get stuck and I’ll see if I can help.

        Reply
    • Yes.
      You can use GPIO 16 por example to attach the button (I’ve tested it and it works well with that pin).
      Then, you can simply check if the button was pressed and take a photo if that is the case.
      buttonState = digitalRead(buttonPin);
      if (buttonState==HIGH){
      (code to take a photo)
      }
      Regards,
      Sara

      Reply
  15. Wow, Thank You both for your time, expertise and effort in helping us all out with this great hobby of ours. Please keep up the great work!!!
    From BC, Canada

    Reply
  16. Hey Guys,
    I’m running this code on two AI Thinker ESP-32-Cam modules. Both systems run fine for awhile, taking pictures, storing pics, etc but then error-out when they come out of sleep. i.e.
    Saved file to path: /picture157.jpg
    Going to sleep now
    ets Jun 8 2016 00:22:57

    rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len:1216
    ho 0 tail 12 room 4
    load:0x40078000,len:9720
    ho 0 tail 12 room 4
    load:0x40080400,len:6352
    entry 0x400806b8
    Starting SD Card
    Picture file name: /picture158.jpg
    Saved file to path: /picture158.jpg
    Going to sleep now
    ets Jun 8 2016 00:22:57

    rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
    configsip: 0, SPIWP:0xee
    clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
    mode:DIO, clock div:1
    load:0x3fff0018,len:4
    load:0x3fff001c,len:1216
    ho 0 tail 12 room 4
    load:0x40078000,len:9720
    ho 0 tail 12 room 4
    load:0x40080400,len:6352
    entry 0x400806b8
    [E][camera.c:1049] camera_probe(): Detected camera not supported.
    [E][camera.c:1249] esp_camera_init(): Camera probe failed with error 0x20004

    This happens on both systems. Anything similar happening for anyone else??

    Reply
  17. I have mase this project. For a while I kept getting an error about waiting for VSync when I pressed the reset button. Eventually I discovered that there are at least two common versions of the camera and I had one where the pin allocations for VSYNC and VLCK were reversed from those in your code. When I set VSYNC to 22 and VLCK to 25 this problem disapeared. However I now have a different problem that has me stumped.
    I get this message on the serial monitor.
    Starting SD Card
    Guru Meditation Error: Core 1 panic’ed (Interrupt wdt timeout on CPU1)
    Core 1 register dump:
    PC : 0x40083e9c PS : 0x00060034 A0 : 0x80082df5 A1 : 0x3ffbe770
    A2 : 0x00000000 A3 : 0x3f400f74 A4 : 0x800df868 A5 : 0x3ffb1e80
    A6 : 0x00000003 A7 : 0x3ffc0ae0 A8 : 0x00000002 A9 : 0x3ffb843c
    A10 : 0x00000000 A11 : 0x00000080 A12 : 0x000b87c8 A13 : 0x3ffb1e60
    A14 : 0x00000000 A15 : 0x00000000 SAR : 0x00000016 EXCCAUSE: 0x00000006
    EXCVADDR: 0x00000000 LBEG : 0x4000c46c LEND : 0x4000c477 LCOUNT : 0x00000000
    Core 1 was running in ISR context:
    EPC1 : 0x400e00f1 EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x40083e9c

    Backtrace: 0x40083e9c:0x3ffbe770 0x40082df2:0x3ffbe7a0 0x40083411:0x3ffbe7c0 0x400e00ee:0x3ffb1ec0 0x400d1b9e:0x3ffb1ef0 0x400d3233:0x3ffb1fb0 0x4008bcd5:0x3ffb1fd0

    Core 0 register dump:
    PC : 0x400feede PS : 0x00060134 A0 : 0x800e6812 A1 : 0x3ffbc020
    A2 : 0x00000000 A3 : 0x00000001 A4 : 0x00000000 A5 : 0x00000001
    A6 : 0x00060120 A7 : 0x00000000 A8 : 0x800ea756 A9 : 0x3ffbbff0
    A10 : 0x00000000 A11 : 0x40089d48 A12 : 0x00060120 A13 : 0x3ffbb6c0
    A14 : 0x00000000 A15 : 0x3ffbbd20 SAR : 0x00000000 EXCCAUSE: 0x00000006
    EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000

    Backtrace: 0x400feede:0x3ffbc020 0x400e680f:0x3ffbc040 0x4008dbd5:0x3ffbc060 0x4008bcd5:0x3ffbc080

    Rebooting…
    ets Jun 8 2016 00:22:57
    This goes on and on over and over.
    Looks like the watchdog timer is rebooting.
    Any help would be great.

    Reply
  18. Hello, I like your tutorials. I request to make the tutorial to Email the Pictures clicked by the CAM module after detecting the motion by PIR sensor.

    Reply
  19. The schematic doesn’t appear to match the fritzing diagram. The 5 volt + goes to the 5V pin on the ESP32 on the diagram, but the schematic show it going to VCC, which I think is correct.

    Reply
  20. Hello, I’m doing a rush school project right now and might want to ask if it’s possible to add something on your project, probably a relay that will turn on light and make an active buzzer ring whenever there is a motion detected from the PIR using only the esp32cam?

    Reply
  21. There have been a few questions regarding the flash LED on the ESP32-CAM board and this should help clarify the LED activity. The LED is controlled by GPIO4 which is also used as DATA1 on the SD card. Reading and writing to the SD card will cause the LED to glow brightly. In this program the LED is NOT lit when the picture is taken, so it will not help with the exposure. It IS brightly lit when the frame buffer is written to the SD card, immediately after the picture is taken. This may lead one to incorrectly think that it is a flash to illuminate the subject. When using the SD card, you have no control over the LED. Programs that do not use the SD card may control the LED by setting GPIO4 high or low. If LED illumination is needed for proper exposure, this program could be modified to initialize the SD card after the picture is taken. This would allow you to use GPIO4 to illuminate the LED before taking the picture.

    Reply
  22. Very awesome ! it works with me…than you very much! 🙂

    but I have one question…Is there any way I can remove the flash light? from the code?

    Reply
  23. Hi Sara,
    it´s a very good Projekt.
    Sorry for my bad english. I am from Germany.
    I need to take a Picture and save ist to sd card in the VoidLoop.
    Operation with power adapter then i dont need the sleep mode.
    In my Projekt works the ESP32 Cam Modul with WIFI and Blutooth thats why not Sleep Mode.
    Can you tell me what´s code i need only for make a Picture and save to sd card.
    I try it but i becom some Error over Variable not declared in this scope.
    My Problem is, i works not so long with Arduino.

    Heiko

    Reply
    • Hi.
      You can use the following function to take and save a photo. Copy that function to the end of your code.

      void takeSavePhoto(String path){
      // Take Picture with Camera
      camera_fb_t * fb = esp_camera_fb_get();
      if(!fb) {
      Serial.println(“Camera capture failed”);
      return;
      }

      // Save picture to microSD card
      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); // payload (image), payload length
      Serial.printf(“Saved file to path: %s\n”, path.c_str());
      }
      file.close();

      //return the frame buffer back to the driver for reuse
      esp_camera_fb_return(fb);
      }

      Then, in the loop() you can call it as follows:

      String path = “/picture” + String(pictureNumber) +”.jpg”;
      Serial.printf(“Picture file name: %s\n”, path.c_str());

      // Take and Save Photo
      takeSavePhoto(path);
      pictureNumber++;
      delay(5000);

      Make sure you have the microSD card initialized.

      I hope this helps.

      Regards,
      Sara

      Reply
  24. If I don’t use the sd card, can I use:
    GPIO 12, 13, 14, 15 for ADC input?
    and GPIO 2, 16 for I2C communication?

    Or are any of these pins also used by the camera?

    Reply
  25. Hi Rui,
    Can the ESP32-CAM be programmed to be a streaming web server plus being able to detect motion, which then will make it send a picture to email?

    Reply
    • Hi.
      It should be possible. However I’m not sure if the ESP32-CAM is able to stream and detect motion with interrupts at the same time.
      Maybe you can have the two features on your project, but not at the same time. It can only detect montion if it is not streaming.
      However, I haven’t tried it.
      Regards,
      Sara

      Reply
  26. Hello,

    In building the motion triggered ESP32 project, I see that in order to start the
    program a reset is needed. Is there any way to set up an external reset switch?
    Being that the reset on the ESP32 pcb is very hard to access?

    Thank you,
    Marty

    Reply
  27. Currently you store the image on a SDcard or SPIFFs.
    The problem is that these wear, you don’t have unlimited write cycles.

    But you can send the image data without storing it.
    smtpData.addAttachData(FILE_PHOTO, “image/jpg”, (uint8_t*) fb->buf, (size_t) fb->len );

    I do this directly in the sendPhoto(); function.
    Tested, works.

    Reply

Leave a Reply to peien Cancel reply

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.