I'm using an Arduino Pro Mini - 5V equipped with a 16Mhz ATmega328.
My needs are quite simple: I want to cyclically make Arduino sleeping to save power for a certain period of time. After that period has expired it has:
sleep (let's say 10 or 12 seconds)
"wake up"
check if it has to return to work or not (i.e. by sending a msg over the serial)
return to step 1 if check in step 3 fails.
In my case Arduino doesn't need to be waked up by interrupts, comms and/or any kind of external inputs during the sleep-period.
I have read with big interests all of the trips&trick about power saving written by the great Nick Gammon on his site.
At the end of the page he propose the simplest solution: decrease the CPU frequency. In this way it obtains more than 10 times of less power consumption.
I wrote a simple "lethargy" function based upon that I'm not sure if the sequence of instructions is right or not.
void lethargy(unsigned long millis)
{
// 1: calculate the new delay time as multple of 256 ticks
unsigned long delay_time = millis/256;
// 2: slow clock to divide by 256
clock_prescale_set (clock_div_256);
// 3: disable ADC and power
byte previous_ADCSRA = ADCSRA;
ADCSRA = 0;
power_all_disable ();
delay(delay_time);
// 4: enable ADC and power
ADCSRA =previous_ADCSRA;
power_all_enable ();
// 5: restore normal frequency for the CPU
clock_prescale_set (clock_div_1);
}
My doubt: after invoking the clock_prescale_set macro (step 2) the CPU will executes all instructions in an extremely slower way. I'm right? It would not be better to put that kind of instructions just immediately before and after the delay call?
I mean:
.
.
.
clock_prescale_set (clock_div_256);
delay(delay_time);
clock_prescale_set (clock_div_1);
.
.
:-[
jremington:
Not much energy (power*time) is saved with a slower clock, because all processes take that much longer to run. Sleep is much more effective.
Yes, absolutely. However the "Gammon" approach is a little bit more complicated for me to understand... :-[
As I understand, the biggest limitation is the maximum sleep time allowed by a Watchdog time (around 8 secs.) It's still not clear to me if I could arrange multiple calls to a "gammon-like-sleep()" function in order to get the desired sleep-time duration.
BTW, a very important (and stupid) question: if I do a mess playing with the Gammon-code there is there any risk to make my arduino unusable? In other words: by turning the power off and on again it will restart with factory settings even if I do a mistake playing with power registers?
Anyway any comment about my code posted up? Is the sequence of instructions in the right order? If I move the two clock_prescale_set() calls just before and after the delay() instruction it will work the same?
I suggest to forget the clock prescaler, as it is a waste of time, and spend some time with Gammon's tutorial, and the ATMega328 data sheet. It will sink in eventually!
You cannot damage the Arduino by putting it to sleep. Reset will wake it up again, in the worst case.
Low power means fast clock and sleep whenever possible - slow clock means having to turn on
peripherals for longer while the processor is using them, and this normally consumes the most
power. When processing is finished for now, power down peripherals and sleep.
Having said that crystal oscillators take a few ms to start running, so it may pay to use RC oscillator
instead which is quick to start.
Also you need to start worrying about the quiescent current draw of your voltage regulator, this
can become the dominant power use if you don't have a micro-power regulator.
MarkT:
Also you need to start worrying about the quiescent current draw of your voltage regulator, this
can become the dominant power use if you don't have a micro-power regulator.
Yes, I read the Gammon's page and I saw that disabling the builted-in voltage regulator is very important. As I understand the following instructions does the trick, I'm right?
BTW, I've designed my own external voltage regulator using EAGLE software, so now I'm powering my Arduino Pro Mini using the Vcc pin (not the one marked "raw"). Disabling the voltage regulator is still crucial in my case ? (I will disable it in any case, but I'm curious.)
Yes, I read the Gammon's page and I saw that disabling the builted-in voltage regulator is very important. As I understand the following instructions does the trick, I'm right?
No, those commands turn off the power to the various peripheral devices that are built in to the microprocessor.
There is no "built in" voltage regulator, but for lowest power, you do not want to use the external voltage regulator that is on commercial Arduino modules.
jremington:
No, those commands turn off the power to the various peripheral devices that are built in to the microprocessor.
You're right, as I understand, the instruction
ADCSRA = 0;
alone is enough to switch down the ADC converter. power_all_disable is a different function that does not affect the ADC.
My question is still floating in the air: do I really need to switch off the ADC converter off if I supply power via the Vcc pin in place of the raw pin?