FINAL PROJECT


<br> ## Final Project Idea I am moving forward with the project to try and make a camera trap for beavers. The basic gist is to have a device that triggers a camera based on motion and then takes a picture. There are a lot of challenges around this project, including: <ul> <li>How to power devices for a few days at a time in the wild.</li> <li>How to take good pictures at night</li> <li>How to protect the electronics from the elements but still have the necessary elements exposed to capture motion and pictures</li> <li>Whether the PIR motion sensor or the ultrasonic range finder works better for detecting motion</li> <li>How to distinguish beavers from other animals</li> <li>How to maintain low power mode while accommodating for the time needed to power up the sensors, camera, etc. and whether that will allow for quick enough pictures to capture beavers</li> </ul> ## Materials and Timeline I have to think a bit more about how I want the sensing system to work and maybe chat with Nathan to get his ideas and a better sense of our constraints. I could imagine a few options though for a beaver camera trap. Because the beavers glide along the water, I could have sensors in the water to detect motion and snap photos from there, but this either requires waterproof sensors or a waterproof enclosure that is also transparent. I think this might also need some sort of buoy to keep the hardware at the right position as the river changes height. (Update: this would be fun to try for the future but is out of scope for this project, so I'm sticking to out of water sensing.) Another option is to try and sense from outside of the water, but this still requires weatherproof enclosures. If everything is outside of the water, I could probably have one enclosure but it still has to be transparent for the camera unless I have a waterproof camera. Or maybe I can make some sort of umbrella to cover the entire device. For 3D modeling, I think I'll need two enclosures, one to store the Arduino and wires connecting to that and another around the camera and motion sensors, unless I get waterproof camera and motion sensors. ###Materials: Because I obviously don't have enough else to do with my life, I've decided to try and build two different versions of this camera trap. One will use the ESP32CAM and the other will use an Arduino Nano and Arducam that I have from my TinyML class. The materials I'm using are: <ul> <li>Arduino Nano 33 BLE</li> <li>Arducam (I have one already, but there's a waterproof one too that costs $55)</li> <li>ESP32 CAM</li> <li>FTDI for programming the ESP32</li> <li>PIR motion sensors (there is a <a href="https://store-usa.arduino.cc/products/waterproof-ultrasonic-sensor-with-separate-probe">weatherproof option</a> for $16), but for now just using regular ones to see if they are affected by rain</li> <li>Ultrasonic range finder (Nathan kindly got me a waterproof one and I may try another regular one and see if it lasts okay)</li> <li>Photoresistors to sense external light and know if IR leds should turn on for nighttime pics</li> <li>Solar panels for power (I have a few small ones in my office that might be enough to power the device, at least for a few days of testing)</li> <li>Batteries (if they are large enough I maybe don't need the solar panels)</li> </ul> ###Timeline: This was my ideal timeline for the project. Unfortunately, several things got in the way of it including 1) so many issues getting the ESP32 CAM working, 2) endless edits on my PhD dissertation, and 3) not having my car to get out to the forest because I rear-ended a truck on my first trip out there :(. But I did manage to get a prototype working and will go back to the Harvard Forest to do more testing after the final project fair and before graduation. <ul> <li>Week of March 18: Get camera working and saving to SD card</li> <li>Week of March 25: Visit Harvard Forest, examine beaver sites, pick a site and use that to determine how the enclosure and hardware setup will work</li> <li>Week of April 8 and 15: Figure out how to take photos at night, make waterproof enclosures, stand to hold everything, etc.</li> <li>Week of April 22: Test everything outside in my yard with rat detection (gross). Fix bugs and anything else necessary</li> <li>Week of April 29: Setup and test in Harvard Forest</li> <li>Week of May 6: Go through results, determine next steps</li> </ul> ## Learning about Beavers and their Locations In late March, I drove out the Harvard Forest to examine potential beaver sites and get a sense of where I might set up a sensor. You can find where beavers are active by seeing where trees have been knocked down and wood has been set up into dams. I identified two potential sites for a beaver cam. One was along a thin path that other animals also use to cross over, which would allow for incorporating some TinyML to identify beavers versus not beavers: <img src="/harvard_ps70/15_finalproject/pics/site1.jpg" width="300"> The other site was by a pond where beavers were starting to build a dam: <img src="/harvard_ps70/15_finalproject/pics/site2.jpg" width="300"> ## Minimum Viable Product The <a href="/harvard_ps70/07_output/index.html">week 7 assignment</a> has my MVP code and images. ## Enclosure As mentioned in the Materials section, I'm using two Arduino setups, one Arduino Nano with an Arducam and an ESP32-CAM. For each Arduino, I want an enclosure to hold the board, battery, wires, and anything else that can't get wet. ### ESP32 CAM Enclosure For the ESP32-CAM, one option is to 3D print an enclosure someone has already designed, such as <a href="https://www.thingiverse.com/thing:4727126">this one</a>, <a href="https://www.printables.com/model/142405-esp32-cam-indoor-and-outdoor-cases">this one</a>, or <a href="https://www.printables.com/model/75024-esp32-cam-case">this one</a>. I started by printing out the 3rd option, which works really well with the ESP on its own, but I need to figure out the battery I'm going to use to determine how large the case should be. After initial testing, a few issues arose with the first case. 1) It only holds the ESP32, not other electronics including wires that have to be included. 2) It did not allow for any adjusting of the camera angled, which makes things a lot easier when trying to stake a metal pole in the ground and get the pictures that are desired. 3) It did not have easy ways to attach to a metal pole for actually putting out in the wild. So to address these issues, I put together parts from different cases I found online that could hopefully work in the wild. The <a href="https://www.thingiverse.com/thing:5540145">first case </a> allows for holding the ESP32 cam, PIR motion sensor, and batteries. The <a href="https://www.thingiverse.com/thing:3816458">other case</a> has a swivel mount to adjust the camera angle. I downloaded the files for both of these cases, attached the different parts I needed using Fusion 360 and Prusa, then printed it all out. Unfortunately the first print did not work well with merging two of the parts - one fell off right away, so I redid the merge in Fusion then tried again. The final 3D model for the case I used with the swivel mount is in <a download="/harvard_ps70/15_finalproject/pics/ESP32 Case v1.sty">this file</a> and shown below. <img src="/harvard_ps70/15_finalproject/pics/case.png" width="300"> I put together some metal parts I found in the lab to make a stand for the case. Unfortunately we only had long, narrow screws so I had to zip tie it together, but it works! <img src="/harvard_ps70/15_finalproject/pics/case.jpg" width="300"> ### Arduino Nano Enclosure The Arducam I have already has a shield and is separate from the Arduino, so a case for the Nano could be a box similar to the one for the ESP32, just with a slightly longer and narrower compartment for the Arduino, which is 10 millimeters narrower and 4 millimeters longer than the ESP. Additionally because the Arducam is separate from the Arduino, there is no need for a hole for the camera. ## How well does it work? Short answer: not well :D. I primarily focused on trying to get the ESP32CAM working because it is significantly cheaper than using the Arducam. But of course it turns out there is a reason for that. Additionally, because beavers are primarily nocturnal (or maybe crepuscular, which is a fantastic word), I focused on getting good pictures in low light. But I didn't want to use the flash which might scare off animals. And this added another challenge. With flash at night, the system captures a decent picture with motion, as seen below: <img src="/harvard_ps70/15_finalproject/pics/picture29.jpg" width="300"> When trying to capture animal motion (using my cat as a test subject), the time it takes for the ESP to fully wake up, load the SD card, and take the picture was too slow and it missed the cat: <img src="/harvard_ps70/15_finalproject/pics/picture44.jpg" width="300"> Without the flash, the picture was about what you would expect: <img src="/harvard_ps70/15_finalproject/pics/picture256.jpg" width="300"> Unfortunately I did not have time to test the IR LED photos at night. Also hilariously because humans can't see IR it's hard to tell if it's even working :D. ## Code and Circuits Getting the basic code and circuit for a motion detected picture saving to an SD card was pretty straightforward and is documented in my MVP assignment for the ESP32. I updated the code for the final project to remove the delay, turn off the flash, and add functionality for the IR LED. The final code I used is: ``` // Adapted from Random Nerd Tutorials // takes a picture when motion is detected and saves to SD card // instead of flash uses the IR LED in pin #15, high is on and low is off #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; const long delayTimeSD = 500; const long delayTimeESP = 1000; unsigned long prevMillis = 0; // store milliseconds 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; unsigned long currentMillis = millis(); // turn on IR LED pinMode(15, OUTPUT); digitalWrite(15, HIGH); rtc_gpio_hold_dis(GPIO_NUM_15); 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"); if (currentMillis - prevMillis >= delayTimeSD) { prevMillis = currentMillis; 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); if (currentMillis - prevMillis >= delayTimeESP) { prevMillis = currentMillis; // Turns off the IR LED connected to GPIO 15 pinMode(15, OUTPUT); digitalWrite(15, LOW); rtc_gpio_hold_en(GPIO_NUM_15); // pin 13 is the PIR motin sensor esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 0); Serial.println("Going to sleep now"); if (currentMillis - prevMillis >= delayTimeESP) { prevMillis = currentMillis; esp_deep_sleep_start(); Serial.println("This will never be printed"); } } } void loop() { } ``` One thing I'm not sure of is why they put all the code in the setup() loop. I think maybe it's because it gets run whenever the device wakes up again? But I'd like to investigate that more. For the circuit, I followed the one on Random Nerd Tutorials, as indicated in my MVP, then added in the LED to ground, 5V, and pin 15 on the ESP. I'm not sure why we need the transistor and also don't know how the transistor and resistors would get soldered onto everything without using a protoboard (maybe that's just what you're supposed to use?) I also got a pretty sweet Nano circuit setup: <img src="/harvard_ps70/15_finalproject/pics/nano.jpg" width="300"> ## Next Steps I have a lot of things I want to do to actually make this project feasible for the real world. First, I'm not sure that ESP 32 CAM is the best option. Because it only has one 5V pin, it requires a lot of wiring hackery to get the motion sensor connected as well. And that doesn't include the addition of the Ultrasonic sensor, which will help make the system more robust. So I have to spend time figuring out if it's better to make my own PCB (which comes with its own struggles and the environmental impact of making and shipping them). Or perhaps there are easier ways to hook upeverything together and have it be scalable. Additionally, the ESP32 CAM has to be programmed via the FTDI, which is tedious and will make it hard to do any sort of software updates. Also, I want to do more testing with the IR LED, especially around animals that aren't used to lights (aka not my cat). I think the case actually turned out pretty well, but the structure holding the case is obviously not scalable. So I will have to figure out a design for a metal structure to hold the case and make sure that is scalable. Finally, I need to figure out an external power source, ideally through solar panels so the devices can remain in the wild for a long period of time. Good suggestion from the project fair - saving photos to an SD card makes sense, but making researchers/field scientists retrieve from the SD card will be difficult. So consider adding in bluetooth or something else that allows photos to be transferred wirelessly when people come by. ## What I learned I learned a lot from this project, including: <ul> <li> Don't take a class "for fun" when finishing your dissertation</li> <li> Have an understanding of the power needs of each sensor and part of your project before putting everything together</li> <li> There is a reason why night vision motion cameras are pretty expensive. Using low cost components for this requires a lot of time and work</li> <li> Working with the ESP32 CAM is not for the faint of heart. But taking good notes every time helped me get a lot faster working with it</li> <li> Soldering on tiny things requires a lot of hand/finger dexterity that I don't have because of symptoms from my autoimmune disease. So I have to weight that against the time/cost of designing/ordering PCBs </li> </ul>