brabl2 / narcoleptic

Sleep library for Arduino (compatible with arduino-tiny core)
58 stars 19 forks source link

Narcoleptic uses some features of the AVR microcontroller to reduce power consumption. These are:

Sleep mode: Freezes all microcontroller functions to dramatically reduce power. The CPU is in SLEEP_MODE_PWR_DOWN during this time.

128kHz internal oscillator: This is a very low power oscillator, used to inform the microcontroller when to wake up.

Power reduction: Unused microcontroller functions can be powered down to save additional power.

Power saving tips: Narcoleptic only reduces the current used by the microcontroller. It does not reduce the current of anything connected to it. A power-on LED can use 20mA. A voltage regulator can use 15mA. The FTDI USB to serial chip uses about 15mA, so Arduino Duemilanoves and Diecimilas are not suitable for low power use without modification. Running at 3.3V and 8MHz uses much less energy than 5V at 16MHz. Pull-up and pull-down resistors use energy if they are passing current. Prefer normally-open switches over normally-closed. Consider using software pullups, and only turning them on when you need them. The Atmel ATmega328P data sheet has a section on "Minimising Power Consumption".

Narcoleptic tricks: The 128kHz oscillator is not very accurate. It drifts by ±4.5% over the full voltage and temperature ranges. Narcoleptic measures its speed against the main clock, and bases all its timings on the corrected value. Provided the operating voltage and temperature remain stable, Narcoleptic should maintain < ±1% accuracy. The are two versions of the Narcoleptic delay function: 1) Uncalibrated narcoleptic.delay() (smaller code size) 2) Calibrated narcoleptic.delayCal() (higher accuracy) The calibration takes approx. 16ms, CPU is in SLEEP_MODE_IDLE during this time. The calibration result is remembered, so it is not necessary to run the calibration every time together with the narcoleptic.delayCal().

The 128kHz oscillator only provides sleep periods of 8192, 4096, 2048, 1024, 512, 256, 128, 64, 32 and 16ms. Narcoleptic performs multiple sleeps to achieve the requested timing.

Keeping track of time: Narcoleptic shuts down almost everything - even the millis() counting routine. To get around this, Narcoleptic keeps track of the time in sleep mode.

If you replace: millis() with: (millis() + Narcoleptic.millis())

your timing calculations will take into account the time in sleep mode.

Narcoleptic gotchas: When doing a Narcoleptic.delay(), the following systems are frozen: Serial communication PWM outputs millis() ticker All timer activity

Narcoleptic is best used when nothing is going on anyway, to simulate a power-off state.

In a well optimised circuit, Narcoleptic can run for several years off a couple of AAA batteries.