This blog post covers the main design choices and prototyping stages from developing a wildlife camera unit. I had seen the ESP32-CAM, which is based on the ESP32 wifi module. It's incredibly low cost and does some amazing things (including face recognition) with the example code other people have written.
I got hold of a couple of samples and thought they would be perfect for a low-cost, solar powered wildlife camera which might be useful in lots of applications. This post covers the work I did on firmware design and PCB design for the unit.
All of the code and PCB design is available in this open-source github repository:
Please note: This is under development and may not work as expected as I add new features.
The first thing was to look at using the ESP32 with the Arduino IDE. There are lots of websites with information to get you started. Some websites that helped me were:
I would say that the Random Nerd Tutorials have been the most help and are the best starting point if you are interested in this kind of project!
When updating this project I found this video which is basically exactly the same thing as I am presenting here:
One of the most annoying things with the EXP32-CAM is that it does not have a USB-Serial converter on-board. I assume this is for cost savings. You can use a USB to serial converter, along with adding a jumper to put the unit into programming mode when it starts up. This is a bit of a hassle so I invested 12 whole Euros in the cam-prog from bitluni on Tindie. This has the USB to serial converter on a nice header board. It also has an automatic reset so that you don't need to press the reset button after programming. This makes everything nice and easy, so I would recommend getting one from bitluni.
This all got me to the point of uploading code to the ESP32-CAM. Uploading the ESP32->camera->cameraWebServer example through the Arduino got a pretty usable webcam which also had face recognition that worked pretty well! Amazing for just a few ££!
I looked around and saw that there was a huge variety of 'trail cameras' available. Most seem set up for hunting (which I'm not really into), but they also seem to be good for wildlife monitoring as well. They are available ready-made for prices from around £30 up to £400.
I wanted a slightly hack-able version with lots of features, but at a lower price point. I also like DIY kits, so this would be a DIY system, with through hole components as much as possible.
I developed my version of the code to achieve the following specifications:
- Must react to a PIR trigger
- Must react to an external switch input
- Can be set to do time-lapse photos, if required
- Flash can be set on/off
- Unit will be in sleep mode most of the time to conserve power
- Unit is solar powered with a li-po 18650 cell
- Unit must be easy to configure in the field with no code upload required.
- Timestamp added to photo file
Please check out the github repository for the code, which hopefully has explanations of how it works.
I developed the code so that, when the unit wakes up, it looks for a "settings.txt" file on wake up. From that file the unit can be configured.
The settings are:
- FLASH_FLAG - Use a flash with the photo? (0 (OFF) or 1 (ON))
- NUMBER_PHOTOS - How many photos to take when waking up?
- TIME_TO_SLEEP - How long (in seconds) between wake-ups (when in LAPSE mode)
- MODE - The mode to use is either LAPSE (wake up every TIME_TO_SLEEP seconds) or TRIGGER (either wake up on PIR trigger or external trigger)
- PHOTO_DELAY - delay (in milli-Seconds) between taking the photos.
- FLASH_START_DELAY - Flash will switch on then wait this time before taking photo (in milli-Seconds)
- FLASH_STOP_DELAY - Flash will be on and wait this time after taking photo (in milli-Seconds)
- DEBUG_FLAG - See debug info on serial port (0 (OFF) or 1 (ON))
- DEBUG_PHOTO - See debug info on serial port (0 (OFF) or 1 (ON))
This allows for multiple configuration settings. If there are no settings uploaded then the defaults (programmed into the unit) are used.
Real Time Clock
I added a DS3231 real time clock module. This is read using the I2C interface. Some pins have to be shared with this line, so eventually I found that using GPIO14 and GPIO15 worked as the I2C interface when configures with the "wire.begin(I2C SCL, I2C DAT)" line. Please check the code for the details on this.
One thing I still need to implement is how to configure the time. At the moment I set the RTC clock time directly using another sketch then use the RTC module in my circuit.
There is an annoying red power LED on the RTC module I used. This needs to be removed to save a bit of power, as every mA matters when things are solar powered!
The RTC module can be seen in this photo of the prototype, along with the repurposed solar 18650 charger PCB I used below.
This has been updated to a smaller RTC module (but samer IC) and also the solar charger has been added to a circuit board:
I wanted the unit to wake up either with a sleep timer, so that it wakes up at regular intervals, or with an external trigger. The external trigger happens on GPIO13.
I wanted to use a low cost PIR sensor to trigger the unit when a moving heat source goes by. Most of the PIR sensors I found work at 5V but not at 3.3V. Eventually I found the AM312 module which is designed to run at 3V so will work with the 3.3V I have available.
You can adapt other PIR modules to work at 3.3V by bypassing the regulator, but I didn't want that hassle!
I also added an input to run the camera from an external switch input. This is buffered through a transistor. There is also a push switch to trigger this, so you could make your own little photo booth!
This switch input could be used to trigger the camera, say when a trap or external detector has been set off.
Real Time Clock
I added a DS3231 real time clock module. This works through I2C, so I added the SCLK and SDA lines to two pins on the ESP32-Cam. I chose the wrong ones initially and it stopped the unit programming along with SD card! I used
I had already designed a 18650 li-po battery charger using the BQ24210 from Texas Instruments. This IC can take a solar PV panel input voltage from 4.5 to 18V DC and recharge a single lithium-polymer cell (such as the commonly available 18650 cells) with up to 800mA. This can give up to 2.5W into the battery, so a maximum of 2.5Wp solar PV panel should be used. I had some 1W panels so have rigged that up to work with the prototype.
As we have WiFi on the module, I added a bit more code to send the photos to an email address. I followed this guide, again from Random Nerd Tutiorials.
I used the ESP32 Mail Client library by Mobizt. NOTE: This is now depreciated!
DEC 2021: I have now updated to the new ESP Mail Client, also by Mobizt
I set up a new email account from which to send my photos (as you need to make it low-secure (allowing less-secure apps, such as this one), don't use your normal email for this!).
I took AGES trying to figure out how to both use 1 bit mode for the SD card (and hence get more pins to control the LEDs and the I2C) but also use the mail client. It turns out this is quite easy. When you set up your SD card you can use the line " MailClient.sdBegin(14, 2, 15, 13) " rather than SD_MMC, which what I used before. This solved everything (but took several nights of figuring it out).
I designed a PCB for this and got some made up. I made a few mistakes, but eventually got it work, as shown here:
I have updated the PCB and schematic file in the github, but have not had any new circuit boards made yet. I'm going to develop a bit more with what I have until I know it's working OK.
Edit: Night vision is a bit difficult. It involves removing the IR filter from the ESP camera and then adding an additional IR cut filter, which I found a unit with simple cut in/out. I'll try and add this on my next version.
The last thing I wanted for the camera was to run some external LEDs to add to the flash capability. This can be done by taking an output from GPIO4 (which controls the on-board bright white LED) and using an external transistor to run even more LEDs. This worked perfectly for adding a 3W white LED to give an even brighter flash (the on-board LED is pretty darn bright, though).
I also needed to check if the camera would react to infra red (IR) light, so I could then do some basic night vision.
I ran a bank of 950nm IR LEDs from the power supply to trigger along with the flash. These were picked up by the camera, so I think it does react quite well to IR light. At the moment a bank of 4 IR 940nm LEDs does not give a very bright image, even when run at high current.
I am ordering some more ultra-bright IR LEDs to add to this and test. It might be that many more IR LEDs are required to produce the brightness required. This needs more testing - any input useful, if you can comment!
I tested the unit by putting it all in a cardboard box and triggering the camera (with the flash white LED covered in tape).
There are two main wavelengths for infra red LEDs, 850nm and 940nm. As 850nm is closer to visible wavelengths then they sometimes have a visible red/purple glow, whereas 940nm LEDs cannot be seen by humans.
I read that most cameras are sensitive to 850nm, but fewer are sensitive to 940nm.
Also, with most cameras there is an IR filter on the lens. This blocks the IR so that the unit works well in daylight, but means that it is not sensitive to the IR light for night vision. I found this very useful post on removing the IR filter from the ESP32-CAM: https://marksbench.com/electronics/removing-ir-filter-from-esp32-cam/.
So it seems that there will have to be two options - a daylight version and an IR version. I'll try one with the IR lens removed.
Here are some more links for using the ESP32-Cam with IR for night vision:
I have added an electronic controller IR cut filter. This mechanically adds an IR filter to the camera. It requires an H-bridge power control circuit to reverse the applied voltage to the IR cut filter unit. This needs two more digital pin to control the filter, with the pins set high-low or low-high to set or remove the filer. When the filter has been set then these pins can be left floating. This might be more pins than we have available on the ESP32-CAM unit. This needs more work!
Time Lapse Function
One really nice feature I wanted to add was to have the unit as a low-cost solar powered time-lapse camera that can either save the photos to SD card or email them to a specific email address. Maybe like a very slow motion film! One frame a day from your garden!
This has been implemented by loads of other people - please do a search for them!
Once you have the photo taking and email control then actually setting a sleep period is pretty easy. I added some setting in the SD settings file which controls the mode: it can either be TRIGGER or LAPSE. TRIGGER will wake up on the PIR or push button trigger. LASPE will wake up every TIME_TO_SLEEP seconds. This involves two different methods of going to sleep, one setting an external wakeup (as detailed above) the other enabling a timer wakeup.
Sleep Power Consumption
I built up (another!) test PCB for this project. It came back looking nice, but now I need to sort out features and implement changes.
First thing to do was to test the power consumption. I'm using deep sleep mode for the ESP32 CAM. This should shut the unit right down to minimal (200uA) current consumption. When I tried this I measured around 5mA still! This would be way too much with the small solar panel I am hoping to use. Why is this?
I tried removing the ESP32 CAM and the RTC from the PCB. Current consumption dropped to less than 100uA. This means the PCB and control parts are not consuming much at all. This includes my own LDO voltage regulator. So its not the PCB/PCB parts.
I added the RTC to the unit. Current went up minimally to around 250uA. Still incredibly low!
When I plugged in the ESP32 CAM board then current (in deep sleep) was high (around 5mA, so 'high' is relative!). I really need this below 1mA if at all possible.
It seems that the ESP32 CAM low cost board is not optimised for very low power consumption. The following links were useful to me for figuring this all out:
The main issues are:
On-board LDO Voltage Regulator
The on-board LDO (Low Drop Out) voltage regulator actually guzzles current even then the unit is asleep. I actually don't use this at all, as I have added my own voltage regulator from the li-ion 18650 cell. So this can be removed.
Removing the LDO voltage regulator from the ESP32 CAM board dropped my current consumption from 5mA to around 2.6mA.
Set pinMode to output before sleeping
Adding a couple of lines to change the pinMode of some of the GPIO to output helps save some power.
This saves about 0.2mA (every little helps!). So we are now down to around 2.4mA.
Removing the Camera
Obviously I can't do this all the time! But when the small camera is removed from it's socket/connector then the current drops to 0.47mA. This is prefect, but not practical.
It seems the camera is never put into a sleep mode, so still consumes current all the time. I found this article to be very helpful here - basically someone doing exactly as I mention here:
I have not managed to find a software solution to switching off the camera. In the lionk above there is a hardware solution, but it means cutting a track and adding a tiny wire. Too fiddly for me!
I did add these lines:
Before sleeping in the hope that this would shut the camera down, but it does not make much difference. This is because the actual power down line for camera is tied to ground (so never sleeps) the CAM_PIN_PWDN actually controls a FET with controls the 1.2V and 2.8V power supplies needed for the camera. But it does not shut down the camera itself! As highlighted in the link above - this is crazy - why not also control the 3.3V power?
Sending Photos via WiFi
So one nice feature for this board is the facility to send any photos taken via WiFi to either an email or online store. I did get email working (using the ESP-Mail-Client library from Mobizt) but you need another email account with low security to do this and that is a pain.
I saw this (very good) tutorial from Random Nerd Tutorials
This shows the steps required to set up a Firebase storage account (from google). You can upload any files to this store and then access them via the storage account. This is a great, simple and free solution to this problem. But you will have to set up your own Firebase store. Please follow the tutorial above to sort this out. You need to include the email, password, api key and the storage stub name onto the SD card. This is used for uploading.
The only problem with this is that it takes a long time! Data is shifted in 512B chunks so a 60-70kB picture takes about 60 seconds to upload. Not terrible, but not fast....
Watch this space!
This is a work in progress. I'll try and keep updated with the updates I do on this project.
Share this post