6

I noticed something unusual using the Low Power library today. When printing text in the loop and using LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); to sleep for one second, all the text in the loop gets messed up in the Serial Monitor as if you chose the wrong baud rate.

Example code:

#include "LowPower.h"

void setup() {
  Serial.begin(9600);
  Serial.println(F("HELLO"));
  Serial.println(F("HELLO")); 
}

void loop() {
  Serial.println(F("HELLO"));
  LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
}

This code just prints

����11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=C!�11=

even when using different baud rates. (I used the same baud rate on both the arduino and the monitor). Replacing the low power command with delay(1000); works fine

1 Answer 1

13

The problem is most likely that the system is going to sleep while it's still sending the serial data.

Forcing all the serial data to be sent before you go to sleep should fix the problem (serial data is sent in the background using an interrupt so as to keep sketch slowdown to a minimum):

Serial.flush();
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
5
  • Thank you very much for this fast answer. Would this affect the power saving of the library? I assume not but better be sure :)
    – qwertz
    Commented Aug 21, 2015 at 15:10
  • It will cause a slight delay before it goes to sleep while the serial data is sent out. It won't affect the actual sleeping though, no. It just means the "on" time will be slightly longer - long enough to finish what it's doing.
    – Majenko
    Commented Aug 21, 2015 at 15:16
  • I didn't know of that method. Thanks. @qwertz using a higher baudrate than 9600 will decrease the transmit time, so reduce power usage.
    – Gerben
    Commented Aug 21, 2015 at 19:05
  • 2
    @Gerben In pre-1.0.0 Serial.flush wiped the transmit buffer. In 1.0.0 and onwards it blocks until the transmit buffer is empty and the UART hardware has transmitted the last bit.
    – Majenko
    Commented Aug 21, 2015 at 19:15
  • Let us know if this doesn't totally solve it. There may be other issues as well like the serial line stopping being driven high.
    – Nick Gammon
    Commented Aug 21, 2015 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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