Here are relevant parts of code for how I achieved 3.9uA current consumption for a mega 328P based design (chip is placed on its own PCB, not in any Arduino board). 3.9uA IS AS GOOD AS IT GETS for resting design so I'm happy!
Application is a remote transmitter, has no power switch, just two buttons. Sleeps until a button is pressed... does it job (transmits) then goes to sleep again.
Runs off 3 x AA batteries, and spends most of its time doing nothing, so basically the unit has power for the shelf life of the batteries!
#include <avr/sleep.h>
#include <avr/wdt.h>
void buttonRedInt() {
signalBits = B00100000 ;
// there is no return for functions attached to an interrupt, they get RETI inserted
}
void buttonGreenInt() {
signalBits = B10000000 ;
}
void setup() {
noInterrupts();
// turn off watchdog, prevent interrupting, don't change order of anything in this block of code:
wdt_reset();
MCUSR = 0;
WDTCSR |= _BV(WDCE) | _BV(WDE);
WDTCSR = 0;
// change button I/O pins to input w/pullups
pinMode(buttonRed, INPUT);
digitalWrite(buttonRed, HIGH); // enable internal pullup
pinMode(buttonGreen, INPUT);
digitalWrite(buttonGreen, HIGH);
// set I/O pins to a state with least power consumed, this will depend on your h/w design
DDRD = 0b00010000 ; // direction 0 input
PORTD = 0b11101111 ; // 1 pullups
DDRB = 0b00000000 ;
PORTB = 0b11111111 ;
DDRC = 0x00 ;
PORTC = 0xFF ;
// turn OFF analog parts
ACSR = ACSR & 0b11110111 ; // clearing ACIE prevent analog interupts happening during next command
ACSR = ACSR | 0b10000000 ; // set ACD bit powers off analog comparator
ADCSRA = ADCSRA & 0b01111111 ; // clearing ADEN turns off analog digital converter
PRR = PRR | 0b00000001 ; // set PRADC shuts down power to analog digital converter
/* Now it is time to enable an interrupt. In the function call
* attachInterrupt(A, B, C)
* A can be either 0 or 1 for interrupts on pin 2 or 3.
*
* B Name of a function you want to execute while in interrupt A.
*
* C Trigger mode of the interrupt pin. can be:
* LOW a low level trigger
* CHANGE a change in level trigger
* RISING a rising edge of a level trigger
* FALLING a falling edge of a level trigger
*
* In all but the IDLE sleep modes only LOW can be used.
*/
attachInterrupt(0, buttonRedInt, LOW);
attachInterrupt(1, buttonGreenInt, LOW);
/* The 5 different modes are:
* SLEEP_MODE_IDLE -the least power savings
* SLEEP_MODE_ADC
* SLEEP_MODE_PWR_SAVE
* SLEEP_MODE_STANDBY
* SLEEP_MODE_PWR_DOWN -the most power savings
*/
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables sleep bit in mcucr register, a safety pin
interrupts();
sleep_mode(); // here the device is actually goes to sleep, saving power