Sub topic 15
Cutting Average Current with Sleep Modes and Smarter Parts
This chapter assumes you can already build a current budget and estimate runtime from capacity and average current. It also assumes you know which Arduino power pin feeds which part of the board. If not, review those chapters first.
Why average current is the lever
Runtime is roughly capacity times a derating factor, divided by average current. You usually can't change the capacity of a battery without making the project bigger or heavier. You can often change the average current by a factor of 100 or more.
Many projects spend most of their time waiting. A temperature logger needs to run for a few milliseconds per minute. If the processor idles at full speed between readings, you pay the active current 100% of the time for a job that needs it 0.3% of the time.
Average current is a time-weighted mean:
I_avg = (I_active * t_active + I_sleep * t_sleep) / (t_active + t_sleep)Two things follow from this formula. Shortening the active time helps. Lowering the sleep current helps more once the active fraction is small, because the sleep term dominates the sum.
Calculating the duty cycle
The script below extends the runtime function from the earlier chapter to handle a duty-cycled load.
def average_current_ma(active_ma, active_s, sleep_ma, sleep_s):
"""Time-weighted average current over one wake/sleep cycle."""
total_s = active_s + sleep_s
return (active_ma * active_s + sleep_ma * sleep_s) / total_s
def runtime_hours(capacity_mah, avg_current_ma, derate=0.7):
return capacity_mah * derate / avg_current_ma
# Wake for 0.2 s every 60 s at 15 mA, otherwise sleep at 0.1 mA
avg = average_current_ma(15.0, 0.2, 0.1, 59.8)
print(round(avg, 3)) # 0.15 mA average
print(round(runtime_hours(2000, avg))) # about 9350 hours
print(round(runtime_hours(2000, avg) / 24)) # about 390 daysAlways run this calculation with your own measured numbers. Try setting sleep_ma to 20 and watch the runtime collapse. A sleep current that looks negligible next to the active current can still dominate the budget.
Sleep modes on the ATmega328P
The ATmega328P used on the Uno and Nano has several sleep modes. Deeper modes switch off more of the chip, so they draw less current and take longer or need a more limited wake source.
| Mode | What keeps running | Typical use |
|---|---|---|
| Idle | Timers, serial, ADC; CPU clock stops | Short waits with peripherals active |
| ADC noise reduction | ADC, some timers | Cleaner analog readings |
| Power-save | Timer2 (asynchronous) | Real-time clock with a 32 kHz crystal |
| Standby | Main oscillator | Fast wake-up from deep sleep |
| Power-down | Watchdog timer and external interrupts only | Longest battery life |
Power-down is the usual choice for data loggers. In this mode a bare chip at 3 V can draw a few microamps with the watchdog timer running. Exact figures depend on the voltage, temperature, and fuse settings, so check the datasheet and measure.
A watchdog-timer sleep sketch
The watchdog timer is a small independent timer that can wake the chip from power-down. The sketch below sleeps in 8 second steps and does its work after every eighth wake-up, which is about once per minute.
#include <avr/sleep.h>
#include <avr/wdt.h>
const uint8_t WAKES_PER_CYCLE = 8; // 8 x 8 s = about 64 s
volatile uint8_t wakeCount = 0;
ISR(WDT_vect) {
wakeCount++; // runs on each watchdog interrupt
}
void setupWatchdog() {
noInterrupts();
wdt_reset();
MCUSR &= ~(1 << WDRF); // clear reset flag
WDTCSR |= (1 << WDCE) | (1 << WDE); // unlock changes
WDTCSR = (1 << WDIE) | (1 << WDP3) | (1 << WDP0); // interrupt mode, 8 s
interrupts();
}
void sleepUntilInterrupt() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
ADCSRA &= ~(1 << ADEN); // switch the ADC off while asleep
sleep_enable();
sleep_cpu(); // execution stops here until the interrupt
sleep_disable();
ADCSRA |= (1 << ADEN); // switch the ADC back on
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
setupWatchdog();
}
void loop() {
sleepUntilInterrupt();
if (wakeCount >= WAKES_PER_CYCLE) {
wakeCount = 0;
digitalWrite(LED_BUILTIN, HIGH); // stand-in for a real measurement
delay(50);
digitalWrite(LED_BUILTIN, LOW);
}
}The watchdog oscillator is not accurate. Its period shifts with temperature and supply voltage, so "8 seconds" can be off by 10% or more. If you need exact timing, use an external real-time clock (RTC) module that pulls an interrupt pin low at the right moment.
Smarter parts: where the current really goes
Software sleep only controls the microcontroller. In a typical battery project the other parts matter just as much.
Choose the board, not just the code
- A bare ATmega328P on a breadboard, or a minimal board such as a 3.3 V Pro Mini with the power LED and regulator removed, can sleep in microamps.
- Boards with native USB, Wi-Fi, or a modern low-power microcontroller often have much better sleep figures than an Uno.
- A lower supply voltage cuts current. Running the chip at 3.3 V instead of 5 V also reduces active current, though it limits the clock speed you can use safely.
Regulator quiescent current
A regulator draws current from the battery even with no load. This is the quiescent current, written as Iq. A classic 7805 can draw several milliamps by itself, which would swamp a 0.1 mA sleep budget. Modern low-Iq LDOs and buck converters specify Iq in the range of a few microamps. When you choose a regulator for a sleeping project, read the Iq line of the datasheet before the dropout line.
Some converters also have a power-save mode that holds efficiency high at very light loads. Check the efficiency curve at your sleep current, not just at the rated load.
Sensors and peripherals
- Pick sensors that have a shutdown or standby pin, and use it.
- Power a sensor from a GPIO pin if it draws less than the pin limit (20 mA is the safe figure on an ATmega328P). Set the pin LOW to cut power completely.
- For loads above that, switch them with a transistor or a load switch IC.
- Wireless modules are usually the biggest consumers. A radio that transmits for 100 ms at 100 mA costs more than a minute of sleep. Send less often, and send fewer bytes.
- Remove or avoid indicator LEDs. A 5 mA LED left on is 5 mA of average current, which is 50 times the sleep budget from the earlier example.
Software habits
- Leave pins in a defined state. A floating input can draw extra current, so enable the internal pull-up with
INPUT_PULLUPor drive the pin to a level. - Use interrupts rather than polling. A button wired to an external interrupt pin wakes the chip on demand, and no timer is needed.
- Finish quickly. Replace
delay()with a sleep call, and turn off the ADC, SPI, and TWI blocks when you don't use them.
Measuring instead of guessing
Datasheet numbers are best-case. To confirm the real average, put a multimeter in series with the battery. A meter in the milliamp range will usually show the awake current well, but its burden voltage can disturb a microamp-level reading, and it may be too slow to catch a short pulse. For a cycling load, measure the voltage across a small series resistor with an oscilloscope, or use a dedicated current profiler, and integrate over one full cycle. Then feed the result into the runtime calculation with a derating factor.
Common failure modes
- The sketch never wakes. Sleeping with interrupts disabled, or with no wake source configured, leaves the chip stuck. Confirm that the watchdog or pin interrupt is enabled before calling
sleep_cpu(). - Serial output is garbled after waking. Power-down stops the clock for peripherals. Allow a few milliseconds for them to restart before using them.
- Sleep current is far above the datasheet. Look for the brown-out detector (BOD), which adds roughly 20 µA if it stays enabled, plus pull-up resistors, leakage through a voltage divider, and sensors that stay powered.
- The battery divider is a hidden load. A 100 kΩ + 100 kΩ divider across a 4 V battery draws 20 µA continuously, which can be more than the chip draws asleep. Use a much larger resistance with a small capacitor on the ADC pin, or switch the divider with a GPIO pin only during a reading.
- Brown-out resets after waking. A radio burst on a weak or cold battery can pull the voltage below the minimum. Put a bulk capacitor near the load, as covered in the decoupling chapter.
Exercise
A board wakes for 0.5 s every 30 s and draws 20 mA while awake. It draws 0.05 mA while asleep. What is the average current, and how long does a 1000 mAh pack last with a 0.7 derating factor?
Show answer
The cycle is 30 s total, so sleep time is 29.5 s. Average = (20 x 0.5 + 0.05 x 29.5) / 30 = (10 + 1.475) / 30 = about 0.383 mA. Runtime = 1000 x 0.7 / 0.383 = about 1830 hours, which is roughly 76 days.
Exercise
You are using a regulator with a quiescent current of 5 mA to power a project that sleeps at 0.01 mA. Why does the sleep code barely extend battery life, and what would you change?
Show answer
The regulator's own 5 mA draw is 500 times the sleep current, so the average current stays near 5 mA no matter how well the microcontroller sleeps. Replace it with a regulator whose quiescent current is a few microamps or less, such as a low-Iq LDO or an efficient buck converter. Then measure the total sleep current again.