Low Power Library


I’m developing a small handheld arduino project that needs to have very long battery life. I’m using the low power library and the difference it makes on current draw paired with an atmega328p on a breadboard is huge.

As the timers won’t be running while it is asleep, I’ve created a variable, (sleep_time) that gets incremented by the sleep time after each sleep. Then when I test to see how long the device has been awake I add millis() to this variable.

 #include <LowPower.h>

void State1(){  //Idle state, sleep for 8s each time, check if above 700' agl

  sleep_time = sleep_time + 8000;

   if(altitude > 700){
    state = 2;

   if(sleep_time + millis() > 50400000){  //Auto turn off after 14hrs on. 
   LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

Only small issue is the calculated total time is running about 7.5% slow compared to how long it is actually awake. This isn’t a deal breaker as I can just re-adjust the time that I want the device to turn off at, but Im curious as to why it does it as I really don’t know.

Many thanks. Jim.

Snippets are SOOOO annoying.. Well.. If the low power library uses the watchdog timer, which it probably does, then the duration of the sleep cycle may vary a lot depending on the ambient temperature. The watchdog timer is thus completely useless for accurate timing.

If you want more accuracy you will need to attach a 32khz crystal to the XTAL pins on the 328P and use Timer2 to handle the sleep cycle. If the crystal (or its load capacitance) is calibrated correct, it will be (almost) as accurate as a RTC and (by far) much more accurate than the watchdog and it will AFAIK consume less power.

Sorry, its just the file is nearly 500 lines in total and not much if any relates directly to this. 99% of the time the code is running just within the loop I posted. And if its not in that loop then this issue doesn't come into effect.

Thanks for the heads up, I'll look into that.

I wanted to know how sleep_time was declared.
I wanted to know how altitude got set.
This information is not available in your snippet, so I moved on and will forever ignore this topic.

Next time (if ever), do not provide snippets.


Someone woke up on the wrong side of their keyboard this morning.