We’re now going to build motion triggered image capturing system that will not only
store the image on a microSD card but it will also send it out in an email.
At the core, we will be using the ESP32-CAM board along with a suitable microSD card.
The microSD card needs to be formatted in the FAT32 file system and we’ve already
done this a few times now. We also need a USB to serial converter to download the sketch
and a microUSB breakout board to power the board. For the motion detection, we will be
using a PIR sensor that is based on the AM312 sensor. I would recommend getting the slim
version like this but I don’t currently have that so I will use this slightly longer
version which works the same way. Apart from this, we will also need a general purpose
NPN transistor like the BC547/BC548/BC549 or 2N3904 along with a 10K ohm and 1K ohm
resistor.
We will divide this build into two parts: we first build a motion detection and image
capture system and in part 2, we add the email feature.
The sketch is largely based on the time lapse project we built earlier. As soon as the board
boots, we take an image and save it to the microSD card. Instead of setting a pre-determined
time interval like last time we set the board to wake up whenever a general-purpose pin
or GPIO goes low. Most of the pins on the board are shared with the camera and microSD
card and using them directly would often result in some error or the other. That is the reason
why we will use an interfacing circuit between the PIR sensor and the ESP32-CAM board. Before
we look at the final circuit, let’s talk about the PIR sensor.
The PIR or passive infrared sensor detects changes in the infrared light that’s radiated
from objects. The AM312 sensor has all the detection circuitry built-in and has a total
of 3 pins – 2 for power and one for the signal. These modules have a wide operating
voltage, but I’ve had inconsistent performance when I powered the module with 3.3V and that’s
why we will power it using 5V. The modules have a built-in 3.3V regulator as the actual
AM312 PIR sensor operates at 3.3V. Whenever motion is detected, the PIR sensor output
goes HIGH and this signal is approximately about 3.3V. Connecting this output directly
to the ESP32-CAM board resulted in errors pretty much all the time and that’s the
reason why we will be using an interfacing circuit, as mentioned previously.
Here’s what the connections look like and we have a transistor in an open-collector
configuration. This configuration prevents the loading of the GPIO pin 13 and this has
worked well for me. The 10K ohm resistor value also helps with the isolation. We add a small
1K ohm current limiting resistor to the base to protect the transistor and sensor. Please
make a note of the transistor pin configuration when wiring the circuit. In summary, when
motion is detected, the output from the AM312 module will go HIGH, which will switch ON
the NPN transistor. This will then pull GPIO pin 13 to ground taking it LOW. Our sketch
will detect this and wake up the ESP32-CAM to take an image. Once this is completed,
it will go back to sleep and wake up only when the pin goes LOW again. This works good
in theory but there’s one more issue that we need to deal with here. The output from
the PIR sensor stays HIGH for a few seconds, usually 2-3 seconds for these modules. Thus,
we need to add a delay after taking an image or else, the ESP32-CAM board will quickly
wake up again and take several images which is not exactly what we want.
Now that we’ve covered all this, download the sketch and open it up in the Arduino IDE.
Once again, this is very similar to the previous sketch. We start by selecting the correct
camera and initializing it. I’ve added these statements to set the camera brightness, contrast
and saturation but you wouldn’t necessarily need it if the lighting is good. We then mount
and initialize the microSD card, take care of the EEPROM and the image number. We then
take an image, save it to the microSD card, write the image number to the EEPROM and then
report this to the serial terminal. Before going to sleep, we turn OFF the flash LED,
configure the board to wake up when pin 13 goes LOW and we also wait for 3 seconds to
allow the PIR output to settle down. Finally, we go to sleep.
Before we can download the sketch to the board, you need to make sure that you’ve installed
the ESP32 board support package. There are two posts on the website to help you get started
with this. Make sure you wire up the board using this image as a reference and open up
the serial monitor. Press the reset the button and you should see a message like this, indicating
that the board is ready to accept code. Once the upload has completed, power off the board
and connect the motion sensor as per the diagram shown here. We will test it out on a breadboard
to make sure everything works as expected. Power ON the board and it should start by
taking an image. I have added a multimeter to the PIR sensor output pin to help determine
its state. The board should continue taking images as long as motion is detected. If you
connect the USB to serial converter and open up the serial monitor, you will be able to
view the board status along with any errors.
I will be mounting the system into an enclosure but I will do that after we cover part 2 which
adds the email feature. For part 2, we will make use of this github library and you need
to install this before you can compile the sketch. Simply open up the Manage libraries
window and type in ESP32 Mail client. Install the library that shows up.
In order to send an email from the board, we need to do a couple of things. First, we
need to connect to a local WiFi network. We also need to login to an email account in
order to send the email and we will use SMTP for this. We would also need to attach the
image to the email and send it to one or more recipients. Download the sketch and open it
up in the Arduino IDE.
Start by entering the network SSID and password. Since we need to login to an email account,
I would recommend creating a new email account for this board as we also need to store the
password. The sketch has been tested with a GMAIL account. Once you’ve created an
account, activate less secure apps by visiting the link shown here and activating the setting.
Add the password along with the recipient email addresses which can be the same as the
sender address. You can add multiple recipients and if you do, then you would need to add
them to the email before it is sent. We will learn to do this later. Before we upload the
sketch, let’s quickly run through it.
We start by connecting to the WiFi network and printing the IP address to the serial
port. We configure and setup the camera like last time. Also, the Mailclient library only
works with the SD library and not the SD_MMC library that we used earlier and that’s
the reason why we need to add this additional statement before mounting and initializing
the SD card. We then initialize the EEPROM, take an image and save it to the microSD card
like before.
Finally, it’s time to send the email. We log into the account, specify the sender details,
set the importance and priority. We then work on the email body which consists of the subject
line and the message itself, which can consist of HTML and CSS. Then, specify the recipient,
and by default, we only have one recipient but you can add more as needed.
If you add multiple recipients then the emails will be sent as a carbon copy. Finally, we attach
the image by specifying the path and storage location. We set the callback function which
simply prints outs the email status as the mail client library takes care of the functions
behind the scenes. The call back function will automatically be called by the internal
library functions.
We then send the email, and start clearing the email object. We turn OFF the flash, set
the microcontroller to wake up when pin 13 goes low and then call a 10 second delay.
I decided to add a 10 second delay as I did not want the board to send multiple emails
in quick session as this would flood the inbox. Keep this is mind and adjust it as needed.
Now that this is complete. Get the board ready to accept code by connecting it as shown here
and pressing the reset button. Hit the upload button and wait for it to complete. Once done,
remove the connections, connect the PIR sensor using the final circuit diagram and power
it ON. I would recommend connecting the USB to serial converter when you first power it
ON. Open up the serial monitor to view the status and make sure everything works as expected.
The email status will be displayed along with any errors and you can follow through to correct
them. If everything works as expected, then you should receive an email along with the
image like so.
Now that we have everything working, it is time to wire it all inside an enclosure. I’ll
be using this 3D model from Thingiverse which is very similar to the time lapse model we
used earlier. I printed it top down, with supports, so the finish isn’t as good but
it is fine for now.
The ESP32-CAM board fits well into the enclosure but the PIR sensor I am using is
a little too big. I decided to de-solder the AM312 sensor and solder wires from it to the
PCB. I then built the interfacing circuit onto a protoboard and wired the sensor PCB
output to it. I then added wires to a microUSB breakout board and connected it all together
as shown here. I used a bit of heat-shrink tubing to make sure the power pins do not
come in contact and added some Kapton tape to the PCBs. Keep in mind that I’m using
the following wiring diagram which is the same one we used for the breadboard version.
Start by gluing in the PIR sensor. I added some more glue to ensure that the pins are
held in place and do not come in contact with each other. Then, glue the PIR PCB in place,
followed by the microUSB breakout board. I started to place the ESP32-CAM board and realised
that the model does not allow you to access the microSD card once in place. Keep this
as a note and perhaps use a different model if required. This didn’t matter to me as
the images would be emailed across anyway. I placed the ESP32-CAM board along with the
sensor interface board and bent the transistor in place to make sure the cover fits correctly.
It was then time to add the cover and mount it all to the stand. Plug in a USB cable and
test it out by moving your hand across the sensor. The board should take about 2 images
when triggered and these should be sent to the email address listed in the sketch.
Instead of a 3D printed enclosure, you can also buy these dummy camera cases. You can
add some infrared or IR LEDs to illuminate the space if you want to capture images in
low light.
That’s it for this video. Don’t forget to like, share and subscribe to this channel.
Thank you for watching and I will see you in the next one.