Author Topic: Power Saving Techniques and Libraries  (Read 31783 times)

KanyonKris

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Power Saving Techniques and Libraries
« on: January 20, 2014, 02:24:39 AM »
I thought it might be useful to have a thread on power saving techniques and libraries.

This article about making low power sketches seems like a good starting point - http://jeelabs.org/2011/12/13/developing-a-low-power-sketch/

Basically you save power by transmitting as little as possible and putting the radio and processor to sleep as often as you can.

There are JeeNodes running on a single lithium coin cell for months so it is possible to run a Moteino for a long time on battery power.

These two lines save a lot of power:

Code: [Select]
radio.Sleep();
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

The first line puts the radio module to sleep and is part of the Moteino RFM12B and RFM69 libraries. When the radio module is in Receive mode it draws 12 - 16 mA, in Sleep mode it draws 0.1 - 0.3 uA - huge difference.

For the 2nd line you will need the lowpower library - http://www.rocketscream.com/blog/2011/07/04/lightweight-low-power-arduino-library/
This line puts the ATmega processor in Power Down mode for 8 seconds, turns off the Analog to Digital Converter (ADC) and Brown Out Detector (BOD). Basically this shuts down most parts of the processor except the low power watchdog timer which wakes the process up after the given time interval. The maximum the watchdog timer can be set to is 8 seconds. If you need to sleep longer just put this function call in a loop.

For an example, see the Sump Pump Alert sketch - https://github.com/LowPowerLab/SumpPumpAlert/blob/master/SumpPumpAlert.ino

I've been using the above 2 lines for a while but only recently began avoiding the use of the delay command to save power. If the program needs to wait, it makes sense to power down instead of idling. For example, to blink the Moteino LED (turn the LED on for 60 miliseconds then turn it off):

Code: [Select]
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN,HIGH);
LowPower.powerDown(SLEEP_60MS, ADC_CONTROL_OFF, BOD_OFF);
digitalWrite(LEDPIN,LOW);

There are other low power arduino libraries you may want to use. Narcoleptic has some nice features, including sleeping for much longer times and correction for millis() - https://code.google.com/p/narcoleptic/source/browse/user+guide.txt

Jee Lab has a function similar to Narcoleptic called Sleepy that is in the ports library - https://github.com/jcw/jeelib/blob/master/Ports.h#L331-L362
The low power sketch pages shows how it's used - http://jeelabs.org/2011/12/13/developing-a-low-power-sketch/

Hopefully this is a good start.
« Last Edit: January 20, 2014, 11:41:23 AM by KanyonKris »

ltj

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #1 on: January 20, 2014, 02:59:42 AM »
Great idea, KanyonKris.
I'm using the Jeelib Sleepy functions with great results myself. I have JeeNodes running for months on end using just a 110mAh lipo.

Recently I found a great resource (a 'Low Power Cookbook') on the the Princeton QED wiki: http://qed.princeton.edu/main/CEE474/LowPower
It's quite detailed on reducing power consumption on AVR uC's using native register settings, but it's well written and contains some good links as well.

KanyonKris

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #2 on: January 20, 2014, 12:23:45 PM »
I'm using the Jeelib Sleepy functions with great results myself. I have JeeNodes running for months on end using just a 110mAh lipo.

To use Sleepy do you include the whole ports library or did you pull the Sleepy function out into its own library?

I have some questions about LiPo batteries. Does your LiPo go through the regulator or bypass it and connect directly to Vcc?

I'm a fan of LiPo, they pack a lot of charge into a small package. But what about self-discharge? I've read they range from 2-8% per month. What has been your experience?

Thanks for the link to the low power cookbook, it's an excellent reference.

ltj

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #3 on: January 20, 2014, 03:19:46 PM »
To use Sleepy do you include the whole ports library or did you pull the Sleepy function out into its own library?

I have some questions about LiPo batteries. Does your LiPo go through the regulator or bypass it and connect directly to Vcc?

I'm a fan of LiPo, they pack a lot of charge into a small package. But what about self-discharge? I've read they range from 2-8% per month. What has been your experience?
I haven't bothered to extract the Sleepy functions, since most of my sensor nodes are JeeNodes anyway. But this might change now :) Could be a nice little project to add some of the Sleepy convenience to LowPower.

Regarding lipo connections, I pondered too for a long time whether or not the regulator should be bypassed. In the end I chose to run it through the regulator. Reasons being that 1. It's simpler and the node/mote/whatever can still be used with a >5V source with the regulator left on (it should be removed or disconnected somehow for direct power, since reverse feeding it, might make it consume a substantial amount of power) 2. When fully charged the lipo is at 4.2V - too much for RFMXXB's and perhaps other devices. Besides, regulated 3.3V is nice and 3. The discharge profile of a lipo means that when the voltage drops below 3.3 + the Vdo of the regulator we are very close to the end of it anyway.

I don't have any exact figures on self-discharge but they are surely way better than standard NiMHs :) The jeelabs forum/blog has quite a few discussions on this topic.

Cheers
« Last Edit: January 20, 2014, 03:21:27 PM by ltj »

ssmall

  • Full Member
  • ***
  • Posts: 112
  • Country: us
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #4 on: January 21, 2014, 07:21:09 PM »
Would it be possible to sleep longer than 8 seconds?

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5138
  • Country: us
    • View Profile
    • LowPowerLab
Re: Power Saving Techniques and Libraries
« Reply #5 on: January 21, 2014, 09:28:13 PM »
I believe 8 seconds is the longest uninterrupted sleep period. Could be wrong but either way you'd have to wakeup, figure out that you want to sleep longer and go back to sleep until you have accumulated the amount of time you want to sleep. The narcoleptic library does that for you with a simple function call (just tell it how much time and it will divide it out for you).

ssmall

  • Full Member
  • ***
  • Posts: 112
  • Country: us
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #6 on: January 21, 2014, 11:30:21 PM »
Thanks for the tip I will take a look at the library.  I am building a weather station and I thought it might pay to conserve power.  Sending out measurements every 8 seconds or so seems too much.

KanyonKris

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #7 on: January 22, 2014, 12:46:47 AM »
As Felix noted, and I mentioned in the first post, you can sleep longer than 8 seconds by using LowPower in a loop or use a different library (i.e narcoleptic) that does this looping for you so you can just give it the total time you want to sleep. Here's some example code using a loop that will sleep for 80 seconds:

Code: [Select]
radio.Sleep();
var = 0;
while(var < 10){
  // do something 10 times
  var++;
  // put the processor to sleep for 8 seconds
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}

If you want to know the details, the watchdog timer (WDT) is one of the few things running when you put the processor to sleep. You give the WDT a value and it counts down from there, when it gets to zero it wakes up the processor and it starts executing code again below the sleep command. The LowPower library supports the following times for the WDT, from 15 miliseconds to 8 seconds:

SLEEP_15MS, SLEEP_30MS, SLEEP_60MS, SLEEP_120MS, SLEEP_250MS, SLEEP_500MS, SLEEP_1S, SLEEP_2S, SLEEP_4S, SLEEP_8S, SLEEP_FOREVER

If you use SLEEP_FOREVER you need to setup an interrupt to wake up the processor otherwise it will never wake up.

hexibot43

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #8 on: January 22, 2014, 01:34:13 AM »
Not sure why I didn't have luck with the Narcoleptic Library, but I'm having good luck with

https://github.com/n0m1/Sleep_n0m1

It has a very simple delay function that works like the Narcoleptic....

Code: [Select]
        radio.sleep();  // Put radio to sleep 
sleep.pwrDownMode();
sleep.sleepDelay(SleepTime);

I was thinking of moving to a lipo battery and some solar cells.   But with a battery lasting a year I just don't see the point anymore.  I wonder how long that lipo battery would last?  That one lipo battery costs more than 4 - 9 volt batteries.  Do you think a lipo battery would last longer than 4 years?   

I'm currently waking up once 1 minute to relay data, and then back to sleep.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5138
  • Country: us
    • View Profile
    • LowPowerLab
Re: Power Saving Techniques and Libraries
« Reply #9 on: January 22, 2014, 08:26:20 AM »
Do you think you won't change anything in 4 years?
I respect the desire of so many people the really prolong battery life, however in reality I think the technology changes much faster than that, and at some point you will innevitably change something. Even with a Lipo it will discharge by itself at a slow rate under little or no load.
I haven't done extensive testing with Lipos but they should last a good amount. Maybe others can pinch in if they have more experience with Lipos.

hexibot43

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #10 on: January 22, 2014, 11:25:55 AM »
1.     I would love to be able to deploy and forget some of the nodes I'm making.  I don't see any reason to mess with my backyard sensors for ground temperature, air temperature, and humidity.  I just want that thing to pump out this information for as long as possible without any work on my part.  And no further investment.

2.    I would agree though that things change so fast.  So a year of service without having to worry about a node is more than good enough.

3.   But I still have a desire for No. 1.

4.   I would love to see a day when a solar panel with some capacitors or something in between a battery and a capacitor would eliminate the need for batteries.  The batteries are the weak link.  I've got solar calculators I've had for years that just work, period.  The closer we get to not needing batteries the better.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5138
  • Country: us
    • View Profile
    • LowPowerLab
Re: Power Saving Techniques and Libraries
« Reply #11 on: January 22, 2014, 12:41:38 PM »
You bet, I have the same desire, deploy and forget.
Solar would be great. I have yet to explore more about solar and how effective it is to power a low power node.

KanyonKris

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #12 on: January 22, 2014, 03:11:35 PM »
hexibot43, thanks for bringing up the Sleep_n0m1 library. It looks like it has some nice features.

Jee Lab did some testing with a few solar panels. He had some encouraging results but there were caveats, seems he didn't come up with a system he really liked. http://jeelabs.org/tag/solar/

Regarding LiPos, they can hold a big charge but when looking at run-times of years I think self-discharge will be the problem. I went looking for self-discharge data for LiPos and found claimed rates of 2-8% per month. If it's 8% then half the LiPo charge is gone in 6 months. 2% looks a lot better at 25 months (2 years). If 2% LiPos exist they would work very well for powering a Moteino. The other cool thing about LiPos is: because they can hold so much charge, you might as well use that charge to run the processor longer and use the radio more rather than let the charge be lost in self-discharge.

Eneloops (or similar low self-discharge NiMH batteries) are a very attractive choice for powering a Moteino for a long time. The 3rd generation Eneloops (October 2011 or later) claim capacity of up to 90% after one year, 80% after 3 years and 70% after five years (http://en.wikipedia.org/wiki/Eneloop). Comparison of low self-discharge batteries - http://www.stefanv.com/electronics/low_self_discharge.html

hexibot43

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
Re: Power Saving Techniques and Libraries
« Reply #13 on: January 22, 2014, 03:41:25 PM »
Kanyon Kris,
     I checked out that JeeLabs page on using Solar.  I'm actually very encouraged.  He was talking indoor usage with small cells.  I think with good power conservation, and a similar setup using two cells this could be very viable for outdoor usage.  With the weather we're having here in the San Francisco Bay Area no problem.  Haven't seen a "real" rainy day in over a year.  Omitting the battery all together.   
     I'm going to have to check out some of those SuperCaps. 

Quote
One conclusion is that only the lightest load of 0.15..0.20 ľA allows the supercap to retain a decent amount of charge through each night. On the other end of the range: with two solar cells in parallel, a 15..20 ľA load might be able to stay alive for a few hours each day.

MikesTechBlog

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Mike's Tech Blog
Re: Power Saving Techniques and Libraries
« Reply #14 on: April 19, 2014, 10:23:04 PM »
Also just to add a little heads up when using the Rocketscream LowPower.powerDown function and sending data out the serial port, such as in debugging. I kept getting garbled data on my Serial.print functions.

I don't recall where I saw the solution (Reference anyone??) but I saw in another forum somewhere that the Arduino / Motenio can go to sleep before all of the serial data is completely sent. Then the next time it wakes up, and sends another stream of serial data, there is still residual from the previous time, which gets garbled.

The solution this other forum suggests is to perform a Serial.flush(); just prior to the  LowPower.p(SLEEP_8S, ADC_OFF, BOD_OFF); call.

This has cleared up the garbled data issue for me.
My Tech Blog:

Mike's Tech Blog