How to sleep and wake up arduino in software w/o any external hardware

Hi, I am planning to make Aruino Mega 2560 board sleep and wake up after few hours. I was looking into the AVR libraries and watch dog timer. I saw there are quite a lot of research saying watch dog timer is not accurate enough, for example, it could wake up half hour late or early from time set.. but I need high accuracy for my project.. Anyone could provide suggestion on this..? I need it to be purely done in software..

So 'high accuracy' (whatever that means) and in software only and with no additional hardware such as a RTC.

An RTC would normally be the tool of choice here, and if it were possible to do the high accuracy wakeup without one, you do wonder why an RTC is normally used.

hmmm how about low power sleep.. does it work well too..? I saw this, but yet to try it out

addition: Does it work well if I am putting milliseconds in the bracket, to wake it up after certain time..?

Without using an external hardware, you can use one of the timers as wakeup source. The most you'll get will be around a 4 second sleep. You can use that to accumulate a counter so that after X many wake-up/sleep cycles, you can do whatever you need to do. So lets say you need to "sleep" for 24 hrs, you then count from 0 to 21600 (the number of 4 second sleep/wakeup cycles).

Another more advance option is use the clock pre-scaler and slow down the CPU to 62500 Hz (from 16 MHz). This will yield you a 1000+ second sleep before needing to wakeup.

you mean the watch dog timer..? or there is any timer can be used and provide high accuracy..?

@hzrnbgy how about this?

The other timers (TIM1, TIM3, TIM4, TIM5). The accuracy will be as good as the crystal in the Arduino. You can still do IDLE SLEEP mode, but not as GOOD as POWER DOWN mode when using the WDT as the wake-up source.

There's also option to "shutdown" individual peripherals, which will give you more power saving if need be.

1 Like

so just to confirm, I can use any timers(TIM1,TIM3,TIM3,TIM4 and TIM5) for both sleep and wake up mode right? but is only for using IDLE Sleep mode? POWER DOWN mode is only for WDT..?

shutdown seems like not applicable for my case..

In POWER DOWN sleep mode, there are a few wake-up source. One of them is the WDT. It runs on a dedicated ULP clock (Ultra Low Power), hence the low accuracy.

In IDLE sleep mode, all peripherals can wake-up the CPU. This also means that IDLE sleep mode uses more power than POWER DOWN sleep mode. But you have the option to shutdown individual peripherals so you can almost achieve the same power consumption as POWER DOWN using IDLE sleep mode.

Another option is you can slow down the CPU considerably, so your timer wake-up does not have to be more often. Like a stated, you can have a sleep cycle of 1000+ seconds. You can always increase the CPU speed again after wakeup, then slow down before going to sleep.

Required reading. If it was a book, we'd say Nick Gammon wrote the book on low power…

It's very detailed but not a slog at all; it will be well worth as much time as you need to work through it. Trust me!

a7

1 Like

how about sleep mode..? Not deep sleep. I saw there is a table showing the power consumption for different sleep modes.. sleep mode consumes much lower power compared to IDLE, but I am not sure if it is like deep sleep, which requires WDT or other timers to wake it up.. I thought of putting milliseconds in the SLEEP mode.. does it work as I want..?

No offense.. I am wondering if it is a little bit outdated..? I am not sure, that's why I am asking.. I did see alot of people suggested this but all were few years back ago..

No offense taken. I don't think it is outdated.

I can only base that on my 100 % reliance and recent 100 % success getting an ultra low power, wakes up from RTC, project done. Using that link and little else except some help on these fora.

Well, not done, my projects never seem to finish themselves. :wink:

Sometimes it takes more than a minute for things to go out of date.

a7

DEEP SLEEP/POWER-DOWN means pretty much all the hardware is turned off except the WDT and some few. That is why you can only wake up from few sources.

IDLE is only shutting down the CPU and flash but everything else is running, hence higher power consumption.

If your absolute requirement is lowest power, the POWER DOWN sleep gets you there, but in this mode, you can only use the WDT (plus few) to wake up, and the clock is not very accurate.

If what you need is low power but accurate time keeping, IDLE sleep mode is okay. But you can save more power by shutting down components manually and maybe slowing the CPU too.

If I am following this, there is separate command for sleep and deep sleep as well as idle,, but sleep consume less power as compared to Idle.. that's why I am wondering if I could use that for lower power consumption.. but I can't find any libraries as of now.. or maybe I missed out.. do you mind to share if you have..?

That link refers to a different CPU (SAMD21) than what you have in the Arduino Mega 2560. They are two different architecture. One is a Honda Civic, the other is a Ferrari.

I don't have a library but the code is pretty easy if you've been working with the AVRs for a long time. I'll share some code later, since I got too many mouths to feed and have to do work and earn my bacon.

No offense, but I am wondering if you’ve even glanced at Nick Gammon’s page… didn’t realize you are the OP, that only makes me want to reiterate my recommendation.

There’s a time for libraries and a time for knowing WTF is going on “under the hood”. IMO this is a time to dig in and learn something.

a7

1 Like

Alright.. then I should go for AVR libraries.. thanks for sharing!

sry but,, except WDT ,what should I use to wake it up..? I know is needed to put the idle command in the for loop to work in counter.. then it does not come out after that..

Sry @stephanie9, this is a detail I overlooked - and being in the midst of moving a project to a new chip I should have sensitized myself to the problem it makes…

So not so much out of date as not directly applicable.

Perhaps someone has done all and the same things with the Mega. In my case, it looks like sitting down with the data sheet only knowing that what one processor can do, so probably can another even if all the dets differ in pesky and annoying ways.

It raises one's appreciation for high(er) level programming in standard languages.

But there’s more fun closer to the metal. :wink:

a7