[leonardo] sleeping mode and serial

Hi,

I try to save power on my leonardo. For that, I use the sleep mode.

Following code is working well:

#include <avr/sleep.h>

void wakeupFunction(){
}

void toSleep(){
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  
  sleep_enable();
  attachInterrupt(0,wakeupFunction,LOW); //wakeupbutton : pin 3 on leonardo!!!
  sleep_mode();
  detachInterrupt(0);
  sleep_disable(); //fully awake now
}

void setup()
{  
  Serial.begin(9600);	// Debugging only
  pinMode(3, INPUT); 
}

void loop()
{
  Serial.println("loop()");
  digitalWrite(13,HIGH) ;
  delay(1000);
  digitalWrite(13,LOW) ;
  toSleep();
}

I am happy, i can light my room with the little on board led. But!

I am on windows 7, and after uploading this useful sketch, port COM is not usable. Board is detected by windows, port COM exists, but arduino 1.0.2 says that "port com is already in use" or "port com is not reachable". And I have to burn the bootloader to rescue my leonardo.

What the duck with serial on leonardo in sleep mode ?

Phil

USB slave devices are not allowed to go to sleep without permission from the host controller. As you've discovered, sleeping without permission causes problems.

Put a delay in setup; I suggest five seconds. When you want to work with your Leonardo...

• Ensure the Leonardo is not connected to the computer
• Start the Arduino IDE
• Load and compile an empty sketch
• Connect the Leonardo to the computer and execute Upload

You will have to experiment with the timing to get it right. You may have to hold the RESET button on the Leonardo while connecting it to the computer then release RESET when you are ready to upload the empty sketch. What you want to do is much easier for boards with the USB interface and processor separated.

Thanks!

This is the way I choose to wait for a miracle. Indeed I wait for an ebay nano arduino.

I wonder if leonardo is good choice for beginners...

Phil

Last question:
In sleep mode (SLEEP_MODE_PWR_DOWN), leonardo eats 9mA.
How can I use a real power saving mode?

Phil

You probably have to turn off power to the USB pad. Instructions are in the datasheet. There may be an AVR Libc function to turn it off.

Hi Phil,

did you solved the issue? I got the same problem you described on an Arduino Micro (same MCU as Leonardo, atmega32U4): after wake up the COM connection via USB is not working anymore.

To upload a new sketch I have to press and hold the reset button on Arduino, click upload on the Arduino IDE and release the reset button only after the "Uploading..." message appears in the message area if the IDE. No need to burn the bootloader again, at least for me.

I suspect that the Serial object needs to be reinitialized after wake up, but I can't figure out how to do that. I tried to call Serial.begin(9600) after wake up, wait for Serial become "true", but that did not solve the problem.

Alessandro

Hi!

I have the same issue: after using set_sleep_mode(SLEEP_MODE_PWR_DOWN) and sleep_cpu() the board does not respond to USB traffic any more. However, it is alive since I can see LED blinking as I programmed it to do according to sleep periods and watchdog wake-ups.

I read that an USB device that wants to sleep should somehow inform USB host that it goes to sleep. How to do that? Is there a way to wake up USB from this situation?

I realize that this is an old thread but it is correct topic and several people raised the issue of correctly restarting the arduino after sleep. . I am using a ProMicro. On resume from sleep, is there a way to reinitialize the serial communications via the USB connector?

Appreciate any comments.

I can also reproduce this problem - there seems to be no way of deactivating the USB module - either explicity or going into anything more then the idle mode and getting a working usb serial connection after this.

Even going through the procedures specified In the Atmel specs does not seem to help, maybe the arduino USB stack is interfering.

Using the sleep mode leving usb enabled creates it own set of problems, because the usb interrupts occurring every odd milliseconds interrupt the idle mode - so you can't sleep for more than milliseconds.

Regards.

PS: Be sure to include a long enough delay in the setup function, to make sure you can falsh new firmware without going through the reset procedure.