In the last video, we successfully created a simple IP camera using the ESP32-CAM board
and we tested out the face detection and recognition features. In this one, we will build a digital
camera that will take an image and save it to a microSD card, every time you press a
button.
At a very high level, here’s what the sketch will be doing. As soon as we boot, the board
will take an image, save it to the microSD card and it will then go to sleep to save
power. This is handy if you decide to power it using a battery. The next time we press
the reset button it will repeat the above and go to sleep again, this continues indefinitely.
Also, we store the image number in the EEPROM and read it before taking a new image. The
ESP32-CAM board already has a reset switch, a microSD card slot and camera module so we
simply need a suitable microSD card to run this sketch.
The microSD card should be formatted with the FAT32 file system. In windows, you can
do this by right clicking the microSD card, selecting format, then the correct settings
and hitting format. Once this is done, insert the microSD card into the ESP32-CAM board.
Next, we need to upload the sketch but let’s take a look at it before we do this. The first
thing we do is select the right camera model as this is needed to assign the correct camera
pins. For the ESP32-CAM board, this is the AI_THINKER model. Next, we specify the EEPROM
addresses and count. We will talk about this a little later. We then print a message to the
serial terminal and call some statements to configure the FLASH pin, again, we will talk
about this a little later. We then configure the camera and initialize it. We then initialize
the microSD card, mount it and make sure that we can communicate with it. We initialize
the EEPROM next and try to get the new image number.
By default, all the EEPROM locations hold the value 0xFF or 255 and if we were to read
the value from a particular address and increment it by 1, then the first image would have 256
as the image number which is incorrect. One way to fix this is by using another EEPROM
location to tell us if the EEPROM has been initialized by us or if it is empty, in which
case we need to initialize it and set the count to 0. This is the reason why we have
any defined an additional address called ID_ADDRESS and also an ID_BYTE.
We start by reading the ID_ADDRESS location, if the value is not the same as the ID_BYTE
then we know that the image count is invalid. We set the ID_BYTE for the next check and
we also assign the image number to 0. However, if the ID_BYTE is present, then the image
count is valid and so we simply read the value and increment it by one as that will be the
next image number.
We then take a new image, generate the file name or path, create the file and save the
image. We then also save the new image number to the EEPROM location so that we can read
it the next time we boot. For the image number, we call the EEPROM put and get functions as
this enables us to save an integer to the EEPROM which in turn allows us to save up
to 65,536 images. If we were to use the EEPROM write function then we could only store a
byte at a time which would limit the number of images to 256.
The LED flash pin is also shared with the microSD card, and thus, we need to ensure
that the flash is switched OFF before we go to sleep. We also need to hold the pin state
before putting the board to sleep and that’s what these statements do. This is also the
reason why we have the disable hold statements early on in the sketch as we need to release
this pin so that the microcontroller can communicate with the microSD card without any issues.
Finally, we then print everything out to the serial terminal and enter sleep mode.
Now that we know how the sketch works, we need to upload it to the board. Simply wire
the ESP32-CAM board using this image as a reference. Once again, it is recommended to
use an external 5V power source to power it and a USB breakout board works just fine.
Download the sketch from the link in the description and open it up in the Arduino IDE. Make sure
you have the boot jumper in place, then Power ON the board, open up the serial monitor,
and press the reset button. If everything is OK then you will obtain a message like
this indicating that the board is ready to accept new code. Then, hit the upload button
and wait for it to complete. Once done, remove the boot jumper and press the reset button.
The board will take an image, save it to the microSD card and go to sleep. The serial terminal
will give you the board status along with any errors or warnings. Press the reset button
and the board will boot, capture an image and go to sleep again.
Once you are done taking images, you can remove the microSD card, and connect it into your
computer. All the images should be visible for you to use. This is a simple way by which
you can create a digital camera using the ESP32-CAM board. The image quality is not
that great but that should change once they release better quality cameras for this board.
That’s it for this video, if you liked it, then don’t forget to hit that like button.
We will be building many more projects using the ESP32 along with the regular DIY projects
as well, so do subscribe to stay notified. Thank you for watching and I will see you
in the next one.