How long does Atmega take to wake up from Sleep mode?

How long would the Arduino's Atmega328 chip take in terms of "startup time" to wakeup from sleep mode, for example upon a timer- or pushbutton-interrupt? (assuming the standard external crystal)

Is it on the order of milliseconds, or 100s of milliseconds?

I ask because I am playing with a barebones-Arduino project where I would like the Atmega328 to wake up every 100 ms (based on an interrupt from Timer2), do a quick task, then go back to sleep again so as to conserve power.

Obviously, with this in mind, I would like the chip's wakeup action to be as immediate (as low latency) as possible.

milliseconds, but check the datasheet to be sure.

It varies depending on the fuse settings, which are designed to allow crystals to get back into full oscillation. From memory the slowest ones are x clock cycles (a few hundred or so) plus 65 mS.

It also depends on the sleep mode.

Nick is right..
However, It will be hard to wake up the ATMEGA328P-PU if you gave her sleeping pills.. :grin:

It's pretty quick.
From 10.1 of the '328 datasheet:

"If an enabled interrupt occurs while the MCU is in a sleep mode, the MCU wakes up. The MCU is then halted for
four cycles in addition to the start-up time [see 9.2.2], executes the interrupt routine, and resumes execution from the instruction
following SLEEP. The contents of the Register File and SRAM are unaltered when the device wakes up from
sleep. If a reset occurs during sleep mode, the MCU wakes up and executes from the Reset Vector."'

9.2.2:
"To ensure sufficient VCC, the device issues an internal reset with a time-out delay (tTOUT) after the device reset is
released by all other reset sources. ”System Control and Reset” on page 46 describes the start conditions for the
internal reset. The delay (tTOUT) is timed from the Watchdog Oscillator and the number of cycles in the delay is set
by the SUTx and CKSELx fuse bits. The selectable delays are shown in Table 9-2. The frequency of the Watchdog
Oscillator is voltage dependent as shown in ”Typical Characteristics” on page 318."

Table 9-2 shows:
Start up time is fuse programmable for 512 clock cycles, or 8192 clock cycles, approx. 4.1mS [edit - had mA] and 65mS with 5V supply.

"Main purpose of the delay is to keep the AVR in reset until it is supplied with minimum VCC. The delay will not monitor
the actual voltage and it will be required to select a delay longer than the VCC rise time. If this is not possible, an
internal or external Brown-Out Detection circuit should be used. A BOD circuit will ensure sufficient VCC before it
releases the reset, and the time-out delay can be disabled. Disabling the time-out delay without utilizing a Brown-
Out Detection circuit is not recommended."

However, this is the part that seems to apply when waking from Sleep:

"The oscillator is required to oscillate for a minimum number of cycles before the clock is considered stable. An
internal ripple counter monitors the oscillator output clock, and keeps the internal reset active for a given number of
clock cycles. The reset is then released and the device will start to execute. The recommended oscillator start-up
time is dependent on the clock type, and varies from 6 cycles for an externally applied clock to 32K cycles for a low
frequency crystal.
The start-up sequence for the clock includes both the time-out delay and the start-up time when the device starts
up from reset. When starting up from Power-save or Power-down mode, VCC is assumed to be at a sufficient level
and only the start-up time is included."

@Nick and @Crossroads:

It appears, then, that the minimum time is 65 ms (in addition to possibly the number of clock cycles depending on which sleep mode).

But this seems to be very, very long (speaking with respect to typical computer time scales, of course).

For example, suppose I want to accomplish a simple repetitive cycle like this:
--Every 100 ms or so, wake up (via a Timer2 interrupt)
--Do a task that takes about 50 ms
--Go back to sleep
--Repeat

Then, unless I am missing something, the startup time of 65 ms would make the above task impossible (or at least not so efficient in terms of power optimization).

What alternative method could, or is typically, applied for a repetitive, periodic sleep-wakeup-task-then-sleep situation like this? I suppose I'm shooting for a strategy where, after the wakeup-interrupt, the MCU would "startup" in less than 10 ms (as opposed to 65).

--Every 100 ms or so, wake up (via a Timer2 interrupt)

Were you planning on attaching an external crystal to timer2? If not, you are going to need to use one of the power-saving modes that leaves the crystal oscillator running, and these are not subject to the 65ms oscillator startup time.

Standby Mode
When the SM2...0 bits are 110 and an external crystal/resonator clock option is selected, the SLEEP instruction makes the MCU enter Standby mode. This mode is identical to Power-down with the exception that the Oscillator is kept running. From Standby mode, the device wakes up in six clock cycles.

Since most Arduino boards use a ceramic resonator for the main clock, you might also consider using a different fuse setting with a faster startup time (resonators start up faster than real crystals.)

I believe the fuses be set for an effective start up time of 4.1mS when 5V power is not removed.
See table 9-2 that I referenced earlier.

I made a remote control with a Promini that wakes up from power-down sleep mode after every key press. The key press causes an interrupt, the processor wakes up, scans the 4x4 keypad, and never misses a key press - I think if it was taking 65mS the presses would be missed as they would debounce themselves in that time.

westfw:

--Every 100 ms or so, wake up (via a Timer2 interrupt)
Were you planning on attaching an external crystal to timer2? If not, you are going to need to use one of the power-saving modes that leaves the crystal oscillator running, and these are not subject to the 65ms oscillator startup time.

westfw, yes, I was planning on connecting a 32 Khz crystal. Would that preclude the startup time? If so, I'm a little unclear on why the 65ms startup time would only apply in case of the main oscillator.

giantsfan3:
It appears, then, that the minimum time is 65 ms (in addition to possibly the number of clock cycles depending on which sleep mode).

Not at all. See page 33 of the data sheet. For example, if you use the internal oscillator and have BOD enabled, the start-up time from power-down sleep is 6 clock cycles (6 * 125 nS) which is 750 nS, under one millisecond (at 8 MHz).

CrossRoads:
I believe the fuses be set for an effective start up time of 4.1mS when 5V power is not removed.
See table 9-2 that I referenced earlier.

I made a remote control with a Promini that wakes up from power-down sleep mode after every key press. The key press causes an interrupt, the processor wakes up, scans the 4x4 keypad, and never misses a key press - I think if it was taking 65mS the presses would be missed as they would debounce themselves in that time.

CrossRoads, if I understand you correctly, you're using the sleep mode formally named Power-Down. Why is the 65ms startup time absent in this case? It would seem this is the most-power-saving mode, in which case I'd guess the oscillator startup time WOULD add to making it the laziest wakeup.

Nick, thanks for the page reference. (Actually, going by the table on page 35, even in the case of the external clocl, the number of clock cycles seems to be identical, and there is no 65 ms.)

So, am I correct in my interpretation (again, going by what the table headings say) that the 65 ms is only present in the case of a full-reset, and not in the cases of waking up from Power-down/Power-save modes?

Also, here in the comments by you and @Coding Badly on this page (Arduino Forum), it is mentioned that the delay-from-wakeup time as long as 65 ms does apply -- I suppose I am asking if you could distinguish exactly in which cases the 65 ms startup does apply and in which it doesn't.

Yes, it looks like that. I was quoting the 65 mS from memory, as I am not at my usual PC. I just got the datasheet onto this laptop and it looks like you are right, the extra time is for reset rather than sleep.

since this topic popped up in a google search for me.

I thought it would be helpful to add:

from the data sheet: 9.2 BOD Disable (p.40)
"When the BOD has been disabled, the wake-up time from sleep mode will be approximately 60
µs to ensure that the BOD is working correctly before the MCU continues executing code."

so... wake-up time from the deepest sleep mode (Power Down), is going to be less than 1 uS, regardless of the ATMEG328P's start-up time fuse settings.

However:, disabling BOD in software (just before sleep) is going to add 60 uS to your wake-up time.

The irony is that disabling BOD in software (or even fuse settings) allows for significant power savings during sleep: (18.3 uA w/ BOD vs 0.1 uA w/o BOD) *according to my at-home tests w/ bare 328P-pu running internal 8mHz.

So it seems if you are looking for the best possible power-savings in sleep mode AND the fastest possible wake-up times... then your only option would be to permanently disable BOD (by changing the fuses).

Since there doesn't seem to be a final answer to the question of this thread, I've taken some measurements on a real Arduino Mini Pro 8MHz, 3.3V.

First, I put the device to 8-second deep sleep 8 times in a row to have a reference. The result: 66 seconds, which is perfectly correct, as a single 8s deep sleep round is 1048576 ticks of the WDT timer powered by the 128kHz internal source - it equals to 8.192 s (it should be 65.536 secs for eight sleep rounds).

Then, I put the same device to 16-millis deep sleep 4096 times. It should boil down to the same time, as 16-ms sleep equals to 2048 WDT clock cycles (1048576/2048=4096). The result: 74 seconds, so there's a 8-sec overhead. This overhead is exactly the wake-up time repeated 4096 times (loop handling time negligible here).

So, my measurements show that the wake-up time for a 8MHz Arduino is ~1.95ms. And it perfectly matches the datasheet, as it states the Start-up Time from Powerdown and Power-save is 16k CPU cycles for a crystal oscillator, 2ms with the 8MHz crystal. Arduinos with 16MHz clocks should have the wake-up time of around 1ms.