0

I managed to get the LowPower libary working on my Arduino Micro. This code works perfectly:

#include <LowPower.h>

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                  // wait for a second
}

However. I also tried it with a serial connection, to put out some text periodically. But I think the lowpower-libary does - in order to save power - disable this function. So now I am worried that it also disables some other things I might have to use but won't work. Like OneWire or I2C. It also would be nice to work with a functioning standby-mode while developing where serial output is vital, altough it's not neccessary, because I can simulate it with a simple delay. So my question is: How can I use the lowpower-libary AND have a serial connection. And will this libary affect OneWire or I2C? If so: Is there a way on how to keep these "services" running? Or is there maybe another libary wich also saves power in a similar way, but where you have more abilities to customize what you want to have shut down?

Cowboy_Patrick
  • 174
  • 1
  • 16

1 Answers1

4

Yes, activating low power does disable the UART - but only while it's sleeping.

To use serial you just need to ensure that you only use it while it's not sleeping - and due to the interrupt and buffered nature of the Serial driver it's not as easy as just printing.

However, it's simple to achieve: just flush the serial before sleeping. This ensures that any pending serial data is sent out through the serial port before you go to sleep:

void loop() {
  Serial.println("Turning LED on");
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial.flush();
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                      // wait for a second
  Serial.println("Turning LED off");
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  Serial.flush();
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                  // wait for a second
}

If that doesn't help you may need to turn serial off and on yourself either side of your powerdown:

void loop() {
  Serial.begin(115200);
  Serial.println("Turning LED on");
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial.flush();
  Serial.end();
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                      // wait for a second
  Serial.begin(115200);
  Serial.println("Turning LED off");
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  Serial.flush();
  Serial.end();
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);                  // wait for a second
}

Update:

I just noticed you're using the Micro. That makes a big difference. The Micro and the Leonardo's Serial object is actually a USB CDC/ACM serial emulation, not UART. You cannot sustain a USB connection while sleeping. So it will be killed.

To use communication while sleeping you will have to switch to a proper UART connection with a USB to TTL UART (e.g., FT232RL) adaptor to connect to the TX/RX pins and use Serial1 instead.

Majenko
  • 105,095
  • 5
  • 79
  • 137
  • Unfortunately that didn't work. When looking into the device-manager of windows there isn't any COM-Port shown. Just when resetting the arduino it shortly appears, but then - in both code versions - it gets disabled. Then windows is already blocking my request accessing COMx, responding that there is no such port. – Cowboy_Patrick Jul 27 '18 at 19:18
  • Ah, I see you've tagged it as you're using a Micro. That makes a big difference. That's USB, not UART. And yes, USB will be killed. You will need to use UART not USB. – Majenko Jul 27 '18 at 19:20
  • Well CAN i use UART on the micro somehow?? – Cowboy_Patrick Jul 27 '18 at 19:29
  • Yes. I say how in the addendum to my answer. – Majenko Jul 27 '18 at 19:30
  • Or just stick to Idle instead of going into Power-down. The savings will be less, but USB will stay up. – Ignacio Vazquez-Abrams Jul 27 '18 at 20:06