Electronic – Arduino Atmega328 sleep mode with internal timer interrupt

adcatmega328pinterruptssleeptimer

I am trying to reduce the current consumption of my Atmega328 -Arduino- by using the sleep modes. In my code, I want to wake-up every 10ms, read the value from ADC, and go to the sleep mode again.

There are some useful instructions (here and here), but most of them are either based on Watch Dog Timre or external interrupt from pins.

Here is the simplified code of mine, but doesn't work (it doesn't go to the interrupt handler function):

In the setup, I take care of the Timer and its handler:

void setup() {
   Serial.begin(19200);

   Timer1.initialize(10000);  //10ms sampling rate
   Timer1.attachInterrupt(ReadMyADC,10000);

   power_spi_disable(); // SPI
   power_timer0_disable();// Timer 0
}

The rest of the code including loop() is as follows:

void ReadMyADC(){
    sleep_disable();
    PRR = PRR & 0b00000000;
    Serial.print("sth");
    power_adc_enable(); // ebable ADC converter
    //Read ADC
    power_adc_disable();
}
void loop() {
    // ----------------low power ---------------
    PRR = PRR | 0b00100000;
    sleep_enable();
    set_sleep_mode(SLEEP_MODE_STANDBY);
    EIMSK |= _BV(INT0);            //enable INT0
    ADCSRA = 0;                    //disable ADC
    cli();
    mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE);  //turn off the brown-out detector
    mcucr2 = mcucr1 & ~_BV(BODSE);
    MCUCR = mcucr1;
    MCUCR = mcucr2;
    sei();                         //ensure interrupts enabled so we can wake up again
    sleep_cpu();                   //go to sleep
    sleep_disable();               //wake up here
    Serial.print("Wake\n");
}

Apparently, Timer1 is not able to exit the cpu from sleep mode.
Do you have any idea how to manage that, or where the problem is?

Best Answer

Deeper sleep modes turn off more of the chip to reduce current consumption.

The deeper modes turns off the timer counters since they consume quite a bit of current.

Use the watchdog timer in interrupt mode instead, or for max power down, use a micro driven external RC decay to interrupt the micro after a short period.