Getting started with ESP32 Deep Sleep

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're trying to extend the battery life of your Internet of Things project and you happen to be using an esp32 development board then deep sleep modes can be the best method for that in this video I'm going to show you the absolute minimum code you need to get your esp32 into deep sleep using a timer wake up it's literally two lines of code and the amount of power you can save by putting your esp32 into deep sleep is kind of nuts let's jump into this [Music] now just in case you don't believe me here's a little chart uh from the esp32 data sheet I kind of just collated some of the information together here but basically uh there's different modes that the esp32 can be in in active mode a modem sleep mode and then a deep sleep mode there's also other modes I didn't include those because they didn't seem as pertinent to me anyhow when you're in the active mode that's like your Bluetooth is running your Wi-Fi is running and you can be using anywhere from 95 to 240 milliamps that's all systems go like use it all up Now's the Time It's like a battleship in the middle of a war it's just going nuts lots of energy okay and then you've got the modem sleep mode and that's when your CPU is operational but the uh you're not using the RF antenna I call that radio silence I think of like red October if you haven't seen that movie you should pause now and watch that but it's about like a submarine that's anyway uh so then in that you using 20 to 68 milliamps which is still quite a gain when you've got your Wi-Fi and Bluetooth disabled and then you've got deep sleep which is a whole nother game the esp32 starts using this ultra low power processor and now you're talking about using 10 microamps to 150 microamps talk about a game changer for you know power consumption so in deep Sleep I call that like Enter Sandman mode you know you're you're out it's you're not going to be doing anything here there are a couple things that do keep running for example the real-time clock can still work and some timers can work in fact in this lesson I'm going to demonstrate how to use a timer to wake this up if you want to check out some more of the specifics here I'll link to this uh article we have on our website in the description so now I just want to take a quick moment and talk about code flow because it's just sort of weird but once you kind of like get the idea of it's not so crazy anyway okay so think of an Arduino program right you've got setup and you've got got Loop setup runs once and then the loop goes over and over and over right well check out this program generally you think of your code going into Loop right like the stuff you want to do happens in the loop well in this case the stuff you want to do is going to happen in setup and in fact the stuff you want to do like the stuff that you're writing your program to do maybe it's going to read some sensor data and then transmit that over Wi-Fi or Bluetooth or you know whatever it's going to do all that you're going to want to take care of that as the first bit of code in your setup okay and then what you're going to do is you're going to set a wake up source for getting out of deep sleep now the wake up Source can be a timer or it can be one of these touch pins that they have on esp32s or it can be in like an external event that happens in this video we're going to just talk about the timer basically what we're going to do is just set an interval and that interval determines when the esp32 will wake up out of its deep sleep alright so you do it you need to do you set a wake up source and then you prepare for sleep and this is kind of like housekeeping stuff so what you need to do let's say you make a connection to a Wi-Fi client or something like that you're going to want to close that connection maybe you're talking to something over like an additional serial connection you want to shut that serial connection down like you just do your housekeeping stuff close up shop right and then finally you enable sleep so once you enable sleep this puppy goes to bed like Loop never gets executed nothing happens here and then what happens is when that wake-up signal is identified and in this case it's going to be that timer right so that timer is going to be running in the background then the esp32 is going to essentially like reset it's going to come back on and it's going to start at the top of your code again so it's going to start back up at setup you're going to do what you need to do set the wake up Source again prepare for sleep and then enable sleep like that's just gonna happen over and over and over now it doesn't it's not exactly like pressing the reset button but like functionally speaking it kind of is now I do want to know I know I'm showing that though you start right here at setup but you would start at like the top of your sketch right so if if you had some variable set above setup that's that's where you'd start but hopefully you get the idea there all right so now let's go ahead and jump into the Arduino IDE and what I'm going to do first is I'm going to show you the absolute minimum code in order to put your esp32 to sleep and have it wake up it's going to be two lines of code and it's going to be setting a wake up source and then enabling deep sleep and then what we'll do is add some utility functions to make it a little easier to read and maintain and that's about it alright so let's do this all right so here I am in the Arduino IDE this is just a blank sketch and I'm just gonna write out some comments here real quick [Music] okay so these are kind of like the four steps that we talked about right so you do what you need to do then you set a wake up Source do some housekeeping prepare for sleep and then you enable sleep so let's start with this set wake up source foreign okay so the function ESP sleep enable wake up timer is what is going to set the wake up mode for this deep sleep now what's sort of interesting about this function is that the value it accepts is in microseconds that's the interval right that it's going to wake it up at so a microsecond is one millionth of a second so if you wanted to sleep for two seconds you know you'd enter the number 2 million here so like if this function got in a fight with Bruce Lee like reaction time wise I I think it'd be pretty close this is pretty you know precise or Bruce Lee though it probably went anyway all right so ESP sleep enable timer mode so this means right now that when I put this thing to sleep every second it's going to wake back up okay now let's go ahead and enable sleep all right ESP deep sleep start okay we're done is that crazy two lines of code we put this thing into deep sleep and we just saved a ton of power so let's just talk through this real quick again you plug your esp32 in you upload this code you know it starts at the top of program it hits setup it sets the wake up source using the ESP sleep enabled timer wake up and then it just goes into deep sleep everything starts powering down and now it's it's out like Loop never runs so it's out but in the background there's a timer running and when the timer reaches that amount which is in this case one second the board wakes up again and for all intents and purposes it's a lot like pressing the reset button and then we start back up as setup we enable the sleep timer see what I'm saying like that that's how this rolls and these are just the two lines of code you need but obviously here we're not really doing too much so I'm going to go ahead and add a little bit more code here and then we'll kind of talk about it [Music] [Music] okay so let's talk about what I did I added some variables up here at the top and a couple more functions down here okay so the first thing I did is just this little convenience code right up here okay so I say time to sleep so this is like in seconds it basically is what I'm trying to so I'm saying okay I actually want the device to sleep for five seconds and then I create a little conversion factor microsecond to Second and basically what this allows me to do is I can just multiply time to sleep times that factor so I can write something that makes more sense I'm not writing like all those zeros or whatever so that's what I just put down here in the ESP sleep enable wake-up timer so we've got time to sleep which is in seconds times that conversion factor in that that gives us seconds you can do whatever conversion factor you want you'll notice this is a really big data type and the idea is that you know this could be a really long number now one thing I didn't mention before and you know just real quick aside here there's like a lot of details that I'm leaving out here some of those details if you check out the description we've got in an entire article on this some of those details will be there but um there's definitely more more stuff going on here but with this function the longest amount of time like I don't know what that is I've trying to figure that out if you know what time that is let me know in the comments I know based on what I could gather from the interwebs it's like at least 12 hours that's a pretty long time to plot this thing to sleep there's also a minimum time I don't exactly know what that minimum time is either if you put in too small of a time here then your ESP deep sleep start function might actually fail like it might not be able to go in deep sleep and it will return an error code I don't know how short that is I imagine it's like you're talking you know a matter of uh microseconds there but okay anyhow all right so that's what this is this is just a convenience factor right here okay so the other thing I did is I wanted to add some code just to demonstrate like hey what are we doing and I'm not really doing much I'm starting serial communication and then I'm incrementing boot count so this value right here boot count I want to basically say like how many times have we woken up like I plugged my esp32 into power and then I want to know how many times has it rebooted you know woken up from sleep essentially okay so this boot count variable you'll notice I've got it up top it's an integer I set it equal to zero you'll notice this funky thing right here okay so basically when you this is the real time clock RTC real-time clock data and basically what this allows you to do is save the value of this variable into the real-time clock memory because when the esp32 goes to sleep all of the values you will have stored in your program will be lost they'll be wiped they'll be cleared so we can't increment boot count here and expect it to retain that value when the program wakes up from deep sleep so the way around that is just to use this RTC data attr it saves it into that RTC memory so it can persist from one wake up to the next and that's kind of what we're doing okay so we increment boot count and then I just print some stuff out so like hey what's the boot number so I just print this value to the serial Monitor and then I say all right we're going to sleep okay so this is kind of like what I need to do maybe what you need to do is like read some sensor values and need to open a Wi-Fi connection or a Bluetooth you know scan or something like that and whatever you got to do you do that here okay so then we're setting the wake up Source nothing new there so then for the prepare for sleep I just want to demonstrate something so I'm using the flush function from the serial library and what this function does is it holds the program in place until all of the data that's in the serial buffer has been sent so a little bit of details there that I won't get into because it's just not important I just want you to know that like this is the place where you want to prepare for sleep so if you've got some other peripherals that you're talking to you're going to want to like close those connections like time to clean up shop right okay and then we go to sleep so let me go ahead and upload this code and by the way I'm using an esp32 vroom Dev model really doesn't matter what esp32 you're using this code should work and you know another interesting point about this notice there's no library to include it's not like this is some additional functionality that you have to pull from a live Library it's just kind of built right in with the esp32 core that you need to upload when you're working with an esp32 with your Arduino IDE and we've got videos about getting that set up so maybe we can link to those in the description as well if you're wondering okay so now we've got this thing loaded actually let me I'm going to go ahead and clear the serial monitor here let's pull this up and let's see okay so let's see right now we're on boot number five you know what I'm gonna do I'm gonna reset the board let's see if let's see if I'm gonna reset it and uh actually let me clear that reset okay so now we're on boot number two should be five seconds boot three thousand one thousand two thousand three thousand four thousand five bam there it is it's like Bruce Lee timing that's amazing all right so the next video you're going to want to watch is this Arduino in 90 minutes video it is going to get you up and running learning how to program Arduino tons of folks have said it's been super helpful for getting them started or filling in gaps so this video right here just click on it I think you're gonna find it super helpful
Info
Channel: Programming Electronics Academy
Views: 4,878
Rating: undefined out of 5
Keywords: Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Learning Arduino, Microcontrollers, Electronics, Arduino IDE, Arduino Sketch, Computer programming, C++, Programming Electronics Academy
Id: II5YmGYIFJk
Channel Id: undefined
Length: 13min 35sec (815 seconds)
Published: Fri Aug 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.