Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Enerlib for Arduino
by E.P.G. - 11/2010

CURRENT VERSION

1.0.1) 2012/12/29 First revision.

HISTORY

1.0.0) 2010/11/03 Initial Release
1.0.1) 2012/12/29 Added compatibility with Arduino 1.0.x

DESCRIPTION

Enerlib is a easy-to-use wrapper for AVR's Sleep library for Arduino. (Needs to by tested in Arduino Mega)

It makes easier the use of low power modes.

HOW TO IMPORT/INSTALL

Download here: Enerlib 1.0.1

Put the Enerlib folder in "libraries\".

In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch->Import Library->Enerlib".

Once the library is imported, a "#include <Enerlib.h>" line will appear at the top of your Sketch.

There's an example included which demonstrates the Enerlib usage.

CREATION

Energy energ

Creates an instance for an Energy object called energ.

METHODS

For more information about power modes, look at ATMega's datasheet. ATMega328P datasheet

void PowerDown(void)

Puts Arduino in PowerDown mode.

void Idle(void)

Puts Arduino in Idle mode.

void SleepADC(void)

Puts Arduino in SleepADC mode.

void PowerSave(void)

Puts Arduino in PowerSave mode.

void Standby(void)

Puts Arduino in Standby mode.

bool WasSleeping(void)

Check if Arduino was sleeping. Use it in any ISR which IRQ wakes up the microcontroller to see if the ISR was called by a wake up event.

EXAMPLE 1: Entering in Idle mode. Waking up with pin 2

/*
Enerlib: easy-to-use wrapper for AVR's Sleep library.
By E.P.G. - 11/2010 - Ver. 1.0.0

Example showing how to enter in Idle mode and exit from it with INT0.
*/

#include <Enerlib.h>

Energy energy;

void INT0_ISR(void)
{
  /*
  The WasSleeping function will return true if Arduino
  was sleeping before the IRQ. Subsequent calls to
  WasSleeping will return false until Arduino reenters
  in a low power state. The WasSleeping function should
  only be called in the ISR.
  */
  if (energy.WasSleeping())
  {
    /*
    Arduino was waked up by IRQ.

    If you shut down external peripherals before sleeping, you
    can reinitialize them here. Look on ATMega's datasheet for
    hardware limitations in the ISR when microcontroller just
    leave any low power state.
    */
  }
  else
  {
    /*
    The IRQ happened in awake state.

    This code is for the "normal" ISR.
    */
  }
}

void setup()
{
  attachInterrupt(0, INT0_ISR, LOW);
  /*
  Pin 2 will be the "wake button". Due to uC limitations,
  it needs to be a level interrupt.
  For experienced programmers:
    ATMega's datasheet contains information about the rest of
    wake up sources. The Extended Standby is not implemented.
  */

  energy.Idle();
}

void loop()
{
}