ESP32-CAM MicroSD Card - Saving images with the ESP32-Cam

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're working with the micro sd card on the esp32 cam module we'll see how we can use the microsd card to store image data from the esp32 cam camera we'll also build a motion sensitive and time lapse camera with our board so smile for the camera and welcome to the workshop [Music] [Applause] well hello and welcome to the workshop and today we are back working with the esp32 cam board now we've worked with this board a number of times before because it's such an amazing little board and an amazing little value for about ten dollars you can get yourself a board that has an esp32 which is a 32-bit microcontroller it's got integrated wi-fi and bluetooth capabilities plus the board also has an on-board 2 megapixel video camera and so by putting all of that together we can build some pretty amazing projects well one thing the board also has it tends to get neglected is it has a slot for a micro sd card and we can use this micro sd card in our applications to store images from the camera and that's exactly what we're going to be doing today so let's go and take a quick review of the esp32 cam board its onboard camera and its micro sd card capabilities the esp32 cam module is based around an esp-32-s 32-bit microcontroller the board also has an ov2640 2 megapixel camera module it has its own internal antenna plus provisions for an external antenna the esp32 cam uses 3.3 volt logic however the board can be powered by either 5 volts or 3.3 volts as it has an onboard voltage regulator on the front of the board we can see the micro sd card the camera and a white led that is used as a flash several of the connections to the esp32 gpio have been brought out for use in your experiments there's also a connection for 5 volts or 3.3 volts in as either can power the board the vcc out is 3.3 volts and can be used for external peripherals one thing that the esp32 cam does not have is a built-in usb port you will need to use an external adapter or an ftdi adapter to program this board note that if you're using an ftdi adapter you'll also need to jumper gpio pin 0 to ground during the programming process despite its size the esp32 cam board has a number of very impressive specifications but perhaps the most impressive specification is the price the board was initially offered at nine us dollars and is now available for even less often with the inclusion of an adapter board that contains an ftdi adapter so let's go put this inexpensive module and its microsd card to work for us now let's take a look at an esp32 cam module and i've got one over here and as you can see it's a very tiny little module and obviously the most predominant feature of it is the camera that's over here on the top and it's actually on top of the micro sd card which you can see over here now one thing is they don't have this mounted it's just loose so if i were you i would put either a bit of hot glue or epoxy or maybe some double-sided tape over here to hold the camera down i've done that on a few of my modules on the front of the module another thing that you might want to note is up over here and that is an led this is a very bright white led and it can be used as a flash for the camera now if we flip it over you can see of course all the gpio pins but up in the corner over here you'll see a little reset button and you'll probably have to click that during the programming process and you'll also need to click it in order to reset the board and get everything going and another thing on this corner over here we've got a tiny little connector and that is for an external antenna but if you want to use the external antenna instead of the built-in antenna which you can see right over here in the printed circuit board you're going to have to do a bit of soldering you've got to remove the position of some tiny surface mount resistors over here and if you take a look at my original video on the esp32 cam you will see exactly how you can do that now the other thing is that in order to work with this you're going to need to use an ftdi card because as you'll notice this board has absolutely no connection for usb on it and so you can wire an ftdi card up directly to the board i'll show you how to do that but another thing you could do is i built this a while ago now this is actually an esp32 cam experimenters module and it has a place for the ftdi which is in here right now and you can see on the back it's also got a small sawdust breadboard it's got a built-in power supply a 3.3 and a 5 volt power supply and it's also hooked to this esp32 cam by the external antenna you can see the external antenna over here and in addition to letting you experiment with the camera board it brings all the connections out over here it also mounts the camera board so that it is actually facing in a more logical direction when you're experimenting with it and again you can go and see the video in the article about how to build that there's another thing you can do that's a bit simpler and there's these devices here i just picked one up on amazon and let me take a look at this right now it's an esp32 cam um accessory board it has got the ftdi adapter built on to it it's got a micro sd port on it over here it's also got two switches or reset switch and a switch to i o pin zero and you just plug your esp32 cam board into this and one nice thing about this board is on the ftdi method you need to go and jumper pin zero and one whenever you're recording in fact that's why i've added a switch to my module over here just to make that process easier but on this board here you don't have to do that you don't have to hit reset when you're uploading you just basically upload so it makes it a lot easier to use the esp32 and i got this on amazon along with an esp32 cam board and i noticed quite a few selections of these so you might want to look at getting something like that but at any rate that's the esp32 cam board and today we're going to learn how to take images from the video camera and save them onto the micro sd card now before we can start taking pictures with the esp32 cam and saving them to a micro sd card there are a few things we need to take care of and the very first thing of course is to get a micro sd card and to get it formatted correctly after that there's a couple of other things we're going to want to know we're going to want to know how to work with a micro sd card within our programs and it's pretty easy to do that we're also going to want to know how to work with the video camera in our program because naturally if we don't know how to get camera data we're not going to be able to save it so let's start off by learning how to format the microsd card that we're going to be using in the esp32 cam now in order to perform these experiments you're going to need to format your micro sd card in fat 32 format and if you have a smaller micro sd card and by smaller i mean a four gigabyte or maybe an eight gigabyte micro sd then it probably already is formatted in fat32 otherwise you'll likely find that it's formatted in x fat now in order to format in fat 32 there's a number of different options the majority of options are available for microsoft windows now if the card is 16 gigabytes or less microsoft windows file system can use its standard format utility to format in fat32 although it will leave some volume information on the disk that'll work for our experiments another way of doing it is with the smart disk utility and this is a utility that we've used before with the raspberry pi which also has a similar fat32 file format requirement now smart disk is a utility that verbatim is distributing and it's a free utility there are a number of other utilities out there that are not free but this is a free one and it will allow you to format a microsd card of pretty well any size with fat32 now in order to use this you need to have your micro sd card inserted first so i've already done that and i've opened up the smart disk utility it says that i've got a 14 gigabyte card it's actually advertised as a 16 gig but that's pretty common and i'm going to just hit format drive it tells me i'm going to do this and do it want to do it because i'm going to wipe everything out i'll say yes and there you go the drive has been successfully formatted so we've now formatted our microsd card with the fat32 file system and we can begin to use it in our experiments with the esp32 cam in order to use a microsd card with the esp32 cam it must be formatted with the fat32 file system the esp32 cam will address up to four gigabytes of space on the microsd card you can use larger cards but they will just be treated as four gigabyte cards internally the microsd holder is connected to the esp-32s spi bus there are two libraries that come into play when programming for the microsd card the fs library is the file system and the sd underscore mmc library is the sd library for the esp32 you'll begin our program by initializing the microsd we'll then open a file up for writing we'll write the file to the microsd card and then we'll close the file the camera included in the esp32 cam module is an ov2640 this module has a 1600 by 1200 sensor array it uses a quarter inch fixed focus sensor it has a maximum resolution of 1600 by 1200 at 15 frames per second internally the camera is connected to the esp32 gpio most of the esp32 cam camera operation is controlled by the esp underscore camera library in your code you'll need to define the camera pin outs in its connection to the gpio as different camera equipped esp32 boards utilize different connections we'll then configure the camera parameters and there are a vast number of parameters you can choose from to alter the image you'll initialize the camera and then you'll set up a frame buffer data from the camera will appear in the frame buffer and you can read that data to use it in your code so now let's start coding for the esp32 cam camera and its micro sd card so now that we've got the micro sd card formatted it's time to start working with the esp32 cam now our first project is going to be a basic camera we're going to attach a push button switch to the esp32 cam board and we're going to use it to take pictures so every time we press the button the esp32 cam will take a picture and save it to the micro sd card so let's go and take a look at how we're going to hook that up now here's how we're going to hook up our camera project we will of course require an esp32 cam module and we'll need a push button switch a momentary contact normally open switch any style will work we'll also be using a 10k pull-up resistor we'll begin by connecting the vcc pin on the esp-32 cam board to one side of the resistor vcc is an output pin and on the esp32 cam it's been strapped to give an output of 3.3 volts regardless of whether you're powering your camera with 5 volts or 3.3 volts the other side of our resistor will go to one side of the push button switch we'll connect the other side of the push button switch to one of the esp32 ground pins and finally gpio pin 13 will be connected to the junction of the switch and the resistor and this completes our wiring now here's the sketch that we're going to use for our esp32 camera and this will allow us to save our picture data to the microsd card now we're going to start off by including all of the required libraries so we start with the libraries we need for the esp32 camera first we also need some libraries for our micro sd card these are those and we're also going to use a library for the ee prom and the reason we're using the ee prom is we want to have a number that we can save in the ee problem that we're going to use as part of the file name for the images that we save and we're going to increment that number constantly so we're going to save that into eeprom so every time we wake up we will know what the next number that we need to use is now we're going to only use one byte of the ee prompt so we define the ee prompt size and here is the variable that we're going to use for the picture counter and we'll initialize it with a value of zero now the pin definitions over here are taken straight from the webcam server example and it's for the ai thinker camera so i've only brought out the definitions for the camera that's on the esp32 board and this saves a lot of space in the code now this function that we have over here is the first of a couple of functions it's called config esp camera and this pretty well configures all of the camera parameters and again a lot of this has been just directly copied out of the camera web server example this stuff has over here where we build a configuration object and we just tell it where all the various camera functions are and once again this has been taken directly out of that example that was provided by expressive as was this over here where we set the frame size depending on whether or whether or not that we support psram and over here we're going to actually initialize our camera now this section over here allows you to do quality adjustments on the picture and if you've used the webcam server example with the esp32 cam and i'm sure you have it's the first thing most of us do you'll notice it has a bunch of controls on the front where you could set things like the brightness and the contrast and the number of special effects well this is how you can do it programmatically so every one of those controls is pretty well repeated over here and what you could do is run the webcam server example play with the controls with your camera to get the picture the way you want and go back and set the values here right now everything is set to the default now the next function that we're going to look at is to initialize the microsd card and essentially it just mounts the micro sd card and detects that there actually is a card there and that it can verify its type so a properly formatted micro sd card and the next function is the one we're going to use to take the picture we call it take new photo and it needs one input and that's the actual path or the file name of the picture we're going to set up a frame buffer and then we're going to pass that frame buffer over to the file system to build a file for our micro sd card and we're going to basically open the file pass it to the micro sd card and then close it so we're going to save the file and we're going to return that frame buffer back for use by other processes now we're going to go into the setup and setup is where everything runs because we're running this sketch so that it's just going to wake up it's going to take a picture and then it's going to put the esp32 cam into a deep sleep and by going into a deep sleep we could probably save a lot of power if this was a remote battery operated type of affair so we're going to start off first of all in our setup by just eliminating those nasty brown detector messages with this line here and then we're going to start our serial monitor so that we can view the activity and we're going to be using the serial monitor when we run this we'll initialize the camera by calling the es config esp camera routine and then we'll initialize the micro sd by calling the function we just showed you init micro sd card and then we're going to initialize the ee problem with the predefined size which is only one byte and we're going to get our picture count variable updated and so we're going to read the ee prompt and then we're going to add 1 to whatever the value is so picture count is going to get incremented over here based on the value that we'd saved in the eeprom and then we're going to use that value to build the path that we are going to save our image at and then we're going to take the photo we're going to call take new photo and pass that path over to it and once we've taken our photo we need to update our ee prom and so we're going to write to the ee prom with the new picture count now this is how we do the putting the micro pro microcontroller to sleep this is where we bind a pin that when it's active we will wake up the microcontroller so we're binding gpio pin 13 and the zero over here means we're expecting a low input so when gpio pin 13 goes low the microcontroller will wake up and it will run whatever is in setup and over here once we've done that we're going to print the serial monitor that we're going to enter sleep mode delay for a second and then we're going to enter sleep mode over here and this is how you put the microcontroller to sleep so it's going to go to sleep at this point and if it wakes up it'll run everything that we just saw on the setup which includes taking a picture so when you start this it's going to take a picture without you pressing the button and then it's going to wait for you to press a button and wake it up on this pin to take another picture now there's nothing in the loop because everything runs in the setup so let's load this up to our esp32 cam board and see if we can use our camera so i've got the camera sketch loaded up to the esp32 cam which i've got on my experimenters module and i'll show you at the back of the module over here it has a built-in push button and that's what i've used in order to implement my own push button here and so here's my 10k resistor up in there if you can see that and so when i press the button back over here you'll see a little flash and i took a picture and if you look at my serial monitor it says it's image number three which is correct because i've already taken two pictures with this i can take another one maybe aim it up a little bit more so it has something more interesting to see take a picture of some of the equipment that's taking the video right now and maybe a picture of myself and another picture and as you see every time that i do we'll see up on the serial monitor that i've got the picture listed and then it says it's entering into the sleep mode because it's actually consuming very very little current in between taking all of these images and so it does seem to work we can take a look at what we captured on the micro sd card to see if any of these pictures actually came out so here's the contents of our micro sd card and as you can see i took a dozen pictures and i'm sure many of them are absolutely horrible but hopefully at least one or two of them came out so let's take a look and oh we've got a picture of the wires that are on spools that are right above the drawers where i keep connector parts and things so that's our first picture and there's an overexposed version of it and there's a picture of my arm and a little bit of test equipment behind it and my stomach oh there i am with some of the lights on the ceiling uh there you go there's one of the cameras that's taking uh the video that we can see and also in the background some of my tools so that one came out great i'm going to maybe frame that picture and that's an overexposed version of the the above and it looks like we got a lot of blank ones and then we're back to the first one again but we did manage to take some pictures with our esp32 cam camera that we built by just adding a push button and a micro sd card now after you play with the esp32 cam camera for a little while you might want to reset your picture counter back to zero and i wrote a little sketch that would allow you to do that it's very simple we just load the ee prom library we set it up the same way that we did before to define a size of one byte and then in this setup we'll open up a serial monitor we're going to initialize the ee prom library then we're going to print the current value that we have inside there so this will be the current value that we're holding for our picture counter and then we're going to reset it by setting it back to zero and we're going to print out that it's reset so it's a pretty simple sketch i've already uploaded it actually so let me go and open my serial monitor and [Music] let me just reset my board and there we go the current value was 18 and now it has been reset i'm going to reset the board one more time the current value was zero so reset did indeed bring it back to zero so this is just a quick way that you can clean that out when you're experimenting with your esp32 cam camera now our next project is basically an extension of the previous project we're going to take away the push button switch and we're going to replace it with an infrared proximity sensor now we've worked with these proximity sensors before so as you know when they detect infrared heat energy such as that coming from a human being or an animal they are triggered so they can be used to determine when someone has entered a room we're going to have the proximity sensor trigger the camera and then take a picture so you could use an application like this to monitor a controlled area or find out who's been eating all of your jelly beans so let's go and see how we hook that up we'll be building our motion sensitive camera around an esp32 cam board we'll also require a passive infrared or pir sensor and the one i'm using is the am312 this is an ideal sensor as it is powered by 3.3 volts however you can also use an sr50 sensor if you wish now the sr505 needs a 5 volt power supply but it will output a 3.3 volt logic level we'll begin by wiring the supply voltage to our pir sensor if you're using the am312 this means connecting this to the vcc pin on the esp32 cam board if you're using the sr505 however you'll have to find a 5 volt power source for it we'll connect the signal out pin of our pir sensor to gpio pin 13. and finally we'll connect the sensor's ground to one of the esp32 cam boards grounds and this completes our wiring now we don't need to write a new sketch for our motion sensitive camera as we can use the exact same sketch that we used for our basic camera this is the sketch that will wake up the esp32 take a picture and then put the esp32 to sleep we do need to make one modification to the sketch for it to work however and it's down here on line 221 [Music] let's move down there and there we go now this is the line where we bind gpio13 to the wake up call and in our original sketch we looked for gpio13 going low because when we press the push button we are connecting it to ground and sending the signal low well in this case we want the opposite thing to happen the pir sensor will emit a high output every time that it detects the presence of someone and so we want it to bind when it goes high and not low so what you need to do is just change the zero to a one because that's the purpose of this it tells the esp32 what sort of a signal to look for a low or a high so you want to change this zero to a one and once you do that you could load that back up to the esp-32 and we can check out our motion sensitive camera and so here's my motion sensitive camera and it seems to be working pretty good every motion i make around the workbench it is taking a picture and you can see that every time it does it prints up on the serial monitor and then goes into sleep mode so let me show you what i've got over here this is the pir sensor on the back just on my little circuit board and so every movement i've got around here is causing this to go and make a picture which of course is exactly what we wanted to do in the first place so as you can see just a simple hardware change in one very basic software change has turned our basic camera into a motion sensitive camera now time lapse photography is a pretty interesting application and we've worked with it before when i did my 10th anniversary video for the raspberry pi i included the time lapse photography project in it but we can also do time lapse photography with the esp32 cam and the micro sd card now in time lapse photography what you do is you take an image and you take another image and you space them out of the predetermined time and once you've gathered several hundred images together you can stitch them together and make a movie and in the raspberry pi project we used a utility called ffmpeg to take all of our images and create a movie file and we can do that as well with the images that we get from the esp32 cam now naturally the esp32 cam is a bit limited because it has a two megapixel camera whereas the raspberry pi you could use a variety of different cameras and different qualities however still with the esp-32 cam this could find practical application such as this monitoring a secured area or if you wanted to monitor a process such as a 3d print being built up now we don't need to have any hookup for this project because we're simply just using the esp32 cam board by itself so let's jump straight into the code that we'd need to use in order to turn our esp32 cam into a time-lapse photography unit now here's the sketch that we're going to be using with our time-lapse camera and i think that you'll recognize a lot of the code in this sketch from the previous ones that we worked with today now we're going to start off by including the libraries for the camera and the libraries for the microsd card then we do the camera pin definitions for the ai thinker camera and then we set up a counter for our picture number so we can use that in the file name and here is where we establish the delay time the time between taking the pictures so this is in milliseconds so right now every 10 seconds our camera is going to take a picture but of course you could increase that or decrease that as your needs require then we're going to go through a number of functions that you've seen before the config esp camera that configures all the camera parameters and again if you want to adjust the picture quality you can go through the section over here and change some of these values as you wish the init micro sd card initializes the micro sd card and the take new photo once again takes a picture and we'll pass it on to the micro sd card and so in our setup we're going to disable the brown detector start our serial monitor initialize the camera initialize the micro sd card and then to the serial monitor we'll just print out what our delay time is so that we can remember how long it's going to be before we take a picture now we'll go into the loop and we'll establish what the file name is going to be for our new picture and we'll print that to the serial monitor as well and then we'll call the function to take a new photo and then we're going to increment that picture count there's no sense in saving this in ee prom because we aren't going to be putting this to sleep and then we finally just delay everything for the delay time so it's actually quite a simple sketch so let's load it up and take some time lapse pictures okay let's give our time lapse camera a try i'm going to plug it in here and it seems to be taking pictures now to do it every 10 seconds so we'll wait for another picture there we go and if we look up on our serial monitor we'll see that it's printing out that it's saved image zero and now image one and basically we're taking pictures of me i think that's what we're taking pictures of right now and i set the timer of course to uh 10 seconds because it's a reasonable time for a demonstration but in real life you would probably want to set the timer for a much longer duration a lot of time lapse photography you only take a picture every 10 minutes or something like that it really depends what it is that you're monitoring monitoring me talking isn't going to be that particularly exciting but as i said it's just a demo we're doing it for 10 but it does seem to be working it's printing out on the serial monitor that has been saving some pictures to our micro sd card and it's taking pictures every 10 seconds which is pretty well what is designed to do so as you can see you can very easily turn your esp32 cam board into a pretty effective time lapse photography device with just a bit of code and a micro sd card and here are the images that we grabbed from the micro sd card uh now i've only allowed it to go through nine of them but they should be about 10 seconds apart and we'll open them up and well that one looks a little on the purple side as does that one that's a little less purple and so basically photographs of me photographs of the light on the ceiling of the workshop pretty exciting stuff but as you can see it does work our time lapse camera with the esp32 snapped my picture once every 10 seconds so that brings us to the end of our look at using the esp32 cam to save data onto its micro sd card but one thing i want to address right now is the subject of saving video you'll notice that in today's video all i've done is save images and i haven't saved video and you might be wondering why because obviously we can stream video from this camera so we know it's capable of video well the answer is it's actually pretty difficult to save video and the reason is that although the esp32 is a very powerful processor it does have its limitations and the limitations have to do with both timing and with buffering data so that you can send it to the micro sd card while you're doing the video live however it isn't impossible to do that and there are a number of programs on github including basically an esp32 cam vcr program which has been forked about 14 different times where people have attempted to do exactly this now i tried a couple of forks of the program and to be honest i couldn't get it to work well enough to use it in a demonstration for today's video but you might have better luck than i do so i've included links to some of those projects in the article that accompanies this video on the dronebotworkshop.com website now that article also has all of the code samples that i used from today's video so you might want to go and grab those and there's a link to the article right below the video now also while you're on the website if you haven't signed up to my newsletter yet please consider doing so it's not a sales letter it's just my way of letting you know what's going on here in the workshop and also if you'd like to discuss this further the best place to do that is on the dronebot workshop forum where you'll find a bunch of like-minded individuals who love to talk about electronics and membership of the forum of course is free as well now finally if you haven't subscribed to the youtube channel please do that i love getting new subscribers and i'd be honored if you'd be my next one all you need to do is click on the red subscribe button and also click that little bell notification and assuming that you've enabled notifications on youtube you will get notified every time that i make a new video so until we meet the next time please take good care of yourself please stay safe and i'll see you again very soon here in the dronebot workshop goodbye for now [Music] do [Applause] [Music] you
Info
Channel: DroneBot Workshop
Views: 91,935
Rating: undefined out of 5
Keywords: esp32 cam, esp32 projects, esp32, micro sd card, motion sensor camera, time lapse camera
Id: 5KszL2Opuo0
Channel Id: undefined
Length: 36min 0sec (2160 seconds)
Published: Sun Apr 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.