Motion Triggered Image Capture and Email

About: #BnBe is a platform to help teach electronics no matter what the age or skill level. We’re currently designing a wide range of products from beginner level kits to industry-standard microcontroller platforms.

We build upon the previous ESP32-CAM projects and build a motion-triggered image capturing system that also sends an email with the image as an attachment. This build uses the ESP32-CAM board along with a PIR sensor module that is based on the AM312 sensor. The board spends most of the time in sleep mode and wakes up to take an image once motion is detected. In part 1, we modify the previous time-lapse sketch to add the motion detection feature. We then update the sketch and add the email feature in part 2

The video above covers everything you need to know and also explains how the sketch is put together.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Gather the Electronics

The ESP32-CAM board already contains the camera module, and microSD card slot that we need for this sketch. In addition to this, you will need a microSD card, a PIR sensor module (based on the AM312 sensor), a general-purpose NPN transistor (BC547, BC548, BC549 or 2N3904), a microUSB breakout board, a 10K Ohm and 1K ohm resistor and also a USB to serial converter to upload the sketch.

Step 2: Upload the Sketch for Part 1

The ESP32-CAM board does not have an onboard USB connector so you need to use an external USB to serial converter to upload the sketch. You can use the wiring connections shown above but make sure that the USB to serial converter is connected in the 3.3V mode.

It is recommended to use an external 5V supply to power the board, particularly if you are using an FTDI breakout board. For the external 5V supply, a simple USB breakout board will do just fine. There has been some success in powering the board directly from the CP2102 breakout board so you can try that first. The board also has a 3.3V power pin if needed.

The jumper is needed to put the board in the download mode. Once you have everything connected, power up the board, open a serial terminal (Tools->Serial Monitor) with a baud rate of 115,200 and press the reset button. You should obtain an output as shown in the image and this will indicate that everything is working as expected.

You can download the sketch using the following link:

Step 3: Connect the Circuit and Test

Build the circuit using a breadboard and test to make sure everything works as expected. I added a multimeter to the final sensor output to help determine it's state. Once you're happy with the way everything works, move on to part 2.

Step 4: Install the Mail Client Library

Open up the library manager and type in "ESP32 Mail Client". Install the library that shows up as we need this for the sketch.

Step 5: Upload the Sketch for Part 2

Download the sketch from the following link:

Open it up using the Arduino IDE and then update it with your details. You will need to add the network name and password as the board needs to connect to the WiFI network. You will also need to provide an email address along with the password for the board to send the email. I'd recommend creating a new GMAIL account. Once the account is created, you need to enable less secure apps by visiting the following link:

You also need to specify the recipient and you can have multiple if required. Watch the video to learn more. Once all of this is done, upload the sketch to the board and power it ON. I'd recommend connecting the serial terminal and watching the output as this will notify you if there are any errors.

If everything works as expected, then the board should capture, save an image and also send it across as an email.

Step 6: Add the Electronics to an Enclosure

I used the enclosure from the following link:

I added supports and printed it face down, which didn't give a very good finish but this should work for now. Since the PIR sensor was a little too big for the enclosure, I desoldered the sensor and connected it to the PCB using wires. I then created the interfacing circuit using a protoboard and wired it in place. Use the connection diagram shown earlier to connect it all together and you can add some Kapton tape for insulation.

I started by glueing in the PIR sensor, followed by the PIR PCB. I then placed the ESP32 board inside and realised that the case doesn't allow you to access the microSD card but this didn't matter to me as the images would be emailed. I then added the interface board and glued in the microUSB breakout board. Finally, I bent the transistor to allow the cover to close in place. Power on the board and it should take an image when motion is detected.

If you've liked this post, then don't forget to follow us using the links below as we will be building many more projects like this one:

Be the First to Share


    • Instrument Contest

      Instrument Contest
    • Make it Glow Contest

      Make it Glow Contest
    • STEM Contest

      STEM Contest

    3 Discussions


    16 days ago on Step 6

    Will not work when PIR is triggered a second time.
    This error appears:
    [E][sccb.c:154] SCCB_Write(): SCCB_Write Failed addr:0x72, reg:0xff, data:0x00, ret:-1
    [E][sccb.c:154] SCCB_Write(): SCCB_Write Failed addr:0x72, reg:0x44, data:0x0a, ret:-1
    [E][sccb.c:154] SCCB_Write(): SCCB_Write Failed addr:0x72, reg:0x05, data:0x01, ret:-1
    [E][camera.c:1215] camera_init(): Failed to set frame size
    [E][camera.c:1270] esp_camera_init(): Camera init failed with error 0x20002

    The sketch works once only, then produces these error when camera is woken up again.

    2 replies

    Solved it myself 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!
    Excellent sketch.


    Reply 13 days ago

    Thank you. Didn't realize you left a comment here. I don't seem to be getting notification for new comments here.