2

Along all the "advanced" methods described in this article, does simple method as downscaling processor while delay makes a difference on power consumption? How much time does the clock alternation takes itself?

Is below example valid?

#include <avr/power.h>

void setup() {}

void loop {

    /* 
       here some jobs to do...
    */

    clock_prescale_set(clock_div_256); // downscale clock
    delay(200); // wait
    clock_prescale_set(clock_div_1); // restore clock
}
  • i don't think that will accomplish anything; it's not like there are background threads... – dandavis Apr 2 '17 at 18:30
  • It's not running background threads, no, but the delay function doesn't just 'pause'. It runs a loop waiting for the time to pass. If you scale down the clock the cpu will process slower and use less power. I'm not super familiar with the clock prescaler, so perhaps someone could verify, but a delay(200) with a div 265 prescale would actually take just over 51 seconds, i think. ? – Mazaryk Apr 2 '17 at 18:53
  • 1
    @Mazaryk: if that's the case, power-saving mode would be better right? I just don't see shaving empty cycles as a big savings, but i could be wrong; most of my experience is with ESP8266s... – dandavis Apr 2 '17 at 19:21
  • @dandavis: I'm not sure, but I would think sleeping would yield better power savings but that's just a guess. Best way to know for sure would be to test and measure it. – Mazaryk Apr 3 '17 at 4:23
  • @dandavis setting the clock divider is major power-saver, see the article i linked (esp. reply #7 gammon.com.au/forum/?id=11497&reply=7#reply7) – madneon Jun 11 '17 at 18:30
2

Reducing the CPU frequency should work, but I would expect better savings by sleeping during the busy loop:

#include <avr/sleep.h>

void sleepy_delay(uint32_t duration)
{
    uint32_t start = millis();
    while (millis() - start < duration) {
        sleep_mode();
    }
}

Note that the sleep will be interrupted at least on every Timer 0 overflow interrupt, which happens every 1024 µs (that's what increments the value returned by millis()), so there is no risk of oversleeping. You could add some power_*_disable() calls for better savings.

  • 1024 µs "wakeup" thats 1 ms, so your loop will execute every milisecond assuming that it takes less then 1 ms to execute millis() with 2 operations (substract, compare+jump), right? So does this method actually saves power, aka is Arduino that fast? ;) – madneon Apr 4 '17 at 17:49
  • @madneon: millis() is fast, and subtract+compare+jump is only a few CPU cycles. With this loop the Arduino will be awake only a tiny fraction of the time, mostly executing the Timer 0 overflow ISR. – Edgar Bonet Apr 4 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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