1

I have the following program which is supposed to print the words "Program is on" once and then go to sleep. However it doesn't go to sleep and continues to print "Program is on" over and over again.

#include <avr/sleep.h>

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Program is on");
  sleepNow();
}

void sleepNow()
{
  // Choose our preferred sleep mode:
  set_sleep_mode(SLEEP_MODE_IDLE);
  // Set sleep enable (SE) bit:
  sleep_enable();
  // Put the device to sleep:
  sleep_mode();
  // Upon waking up, sketch continues from this point.
  sleep_disable();
}
  • 2
    Because any interrupt will wake it up from Idle, including those used by the Arduino libraries. – Ignacio Vazquez-Abrams Jun 15 '16 at 20:39
  • So what is waking up my Arduino in the code I have above? I don't have anything there besides the the sleepNow() function. – sgmm Jun 16 '16 at 14:29
  • What is waking it up is what you don't see in the code above nonetheless exists. – Ignacio Vazquez-Abrams Jun 16 '16 at 16:36
1

In idle sleep, the timer interrupts will wake the processor, as Ignacio Vazquez-Abrams said in his comment.

A "higher" sleep mode works, however I had to add Serial.flush () otherwise it only printed "P" because the processor goes to sleep before the serial output buffer has been emptied.

#include <avr/sleep.h>

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.println("Program is on");
  Serial.flush ();
  sleepNow();
}

void sleepNow()
{
  // Choose our preferred sleep mode:
  set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  // Set sleep enable (SE) bit:
  sleep_enable();
  // Put the device to sleep:
  sleep_mode();
  // Upon waking up, sketch continues from this point.
  sleep_disable();
}

See my page about power savings.

  • Do you know if there is anyway to wake it up with you are using the SLEEP_MODE_PWR_SAVE mode? – sgmm Jun 16 '16 at 14:37
  • @CrystalPritzker: There are a few. See the "Power-save" row in the "Sleep Modes" section of the datasheet. – Ignacio Vazquez-Abrams Jun 16 '16 at 16:38
  • See my page about power savings. Pin-change and external interrupts wake the processor from any sleep state. I think a serial interrupt will too. Also a watchdog timer interrupt can be used. – Nick Gammon Jun 16 '16 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.