6

I've noticed most example snippets always have a delay in the loop even if it's not needed for proper execution of the program.

I can only assume this is added because people copy and paste without understanding what's going on but it got me thinking if adding a delay could possible save power too.

If I have a loop that checks some inputs for changes could it be possible that adding a delay(10) saves some cycles and thus power?

  • 4
    No. Even a delay "uses" cycles. – Majenko Feb 14 '18 at 15:51
  • 2
    Obligatory Nick Gammon reference: gammon.com.au/forum/?id=11497. All about energy. – user31481 Feb 14 '18 at 15:52
  • @MarkLakata. What about yield() – user31481 Feb 15 '18 at 10:10
  • 2
    @MarkLakata Multithreaded? In Arduino? 99.99% of the time there is no real scheduler, and in any case in the other 0.01% of cases the delay function does not "free" the calling thread (the delay implementation is just like a for loop doing nothing) – frarugi87 Feb 15 '18 at 13:32
15

No, it burns energy by doing nothing.

You definitively should send your MCU to sleep and wake up on events or use the watchdog timer to wake up after a predefined period of time.

8

The delay function doesn't do anything special. It basically just counts down milliseconds inside a while loop and times it with micros().

void delay(unsigned long ms)
{
    uint32_t start = micros();

    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}
  • 1
    Calling yield() is somewhat special, but yeah it's still a busy wait that spends most of the time running instructions. – Peter Cordes Feb 14 '18 at 22:49
  • 2
    @PeterCordes Calling yield() is nothing special. That is just a (weakly defined) function that does nothing. It's there to allow other libraries or code to override it and implement some kind of cooperative threads, but the default code doesn't, so it does nothing. It's not like calling yield() in something like VB where it releases the program flow back to the OS... – Majenko Feb 15 '18 at 10:35
  • @Majenko: Thanks, I assumed that yield implied a multi-tasking OS, not just a stub in case there was multi-tasking. (And yeah, I was thinking it literally was POSIX sched_yield(2), and that this implied Arduino ran a full OS like Linux. Hard to keep track of which dev boards (RPi / Arduino / whatever) run what when I don't use any of them myself. >.<) – Peter Cordes Feb 15 '18 at 10:58
4

No, a short delay (up to 100 millis) is usually inserted in a void loop() when you are attached to an external sensor, in order to stabilize the readings. But it has nothing to do with power consumption. As an example, here are lines 49-52 from the example sketch "AnalogInSerialOut":

      // wait 2 milliseconds before the next loop
      // for the analog-to-digital converter to settle
      // after the last reading:
      delay(2);
1

could it be possible that adding a delay(10) saves some cycles and thus power?

No, the delay() function just pauses the code for the time specified. It's stil running and consuming energy for counting the time and waiting it to finish.

If you need to conserve power, you could adjust your circuit components in a fashion that will save energy, or if you want to save energy on your Arduino, you could use a breadboard setup(Instructions here and here) or make use of the internal watchdog timer to sleep and wake at certain time intervals. More tutorial about watchdog timer is explained here.

Cheers!

-1

While using ESP8266, if your loop is long then delay or yield is used to give it some time to run WiFi maintenance things otherwise it crashes.

  • I'm not sure this actually answers the question, which is about whether a delay in a loop saves energy. (The answer is "No", as you can see from the other answers) – sempaiscuba Apr 4 at 13:59
  • People should read full question and not just the caption. OP says "I can only assume this is added because ....". So in some cases his assumption is wrong. – suresh hariramani Apr 4 at 14:30
  • @ssureshhariramani Yes, the OP's assumption is wrong, but that is not the question. In fact, in this case the question in the body matches that in the title: "If I have a loop that checks some inputs for changes could it be possible that adding a delay(10) saves some cycles and thus power?". Your observation may be a valid comment (when you have sufficient reputation) but it is not an answer. – sempaiscuba Apr 4 at 14:47

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.