6

When I learn the tutorial in

both of the code examples have a line of code of delay(1) and I cannot understand the comments clearly:

 delay(1);        // delay in between reads for stability

The stability for/of what?

4
  • Cross-posted to Arduino Forum.
    – Nick Gammon
    Commented Feb 17, 2016 at 5:15
  • yes, i was afraid that no one will answer my question, since i think there is no much active user as much as Stack Exchange. Sorry!
    – Victor He
    Commented Feb 17, 2016 at 5:46
  • 1
    You can't have been too afraid, as you posted the questions within an hour of each other. Maybe cross-post if a day or two elapsed with no answers. The Arduino Forum has quite a fast response to questions. You got a response there 8 minutes after posting. And another one 5 minutes later. So I don't quite believe this "fear" of yours.
    – Nick Gammon
    Commented Feb 17, 2016 at 6:35
  • sorry,it is my fault, i wont do it again
    – Victor He
    Commented Feb 17, 2016 at 6:39

2 Answers 2

9

Those delays are nonsense. They don't add to the stability of anything. The best you could say is that they stop you spamming your serial port. (However 1 ms is not very long).

Sometimes people throw in a delay because "it seems to work better" without examining the underlying cause. I would speculate that in this case the author may have been using delays to improve receiving serial data, and got the idea into their head that the occasional delay now and then "improves stability".

0

Analog Stability

From the Datasheet of ATMEGA328 Page 240:

When the bandgap reference voltage is used as input to the ADC, it will take a certain time for the voltage to stabilize. If not stabilized, the first value read after the first conversion may be wrong.

So if you are doing multiple reads, you need to wait a few cycles.

Delays are not required when using auto-triggering:

When Auto Triggering is used, the prescaler is reset when the trigger event occurs. This assures a fixed delay from the trigger event to the start of conversion. In this mode, the sample-and-hold takes place two ADC clock cycles after the rising edge on the trigger source signal. Three additional CPU clock cycles are used for synchronization logic.

Also if reading different channels of the same ADC module, you need to provide a delay between the reads so that the analog MUX is positioned and the S&H capacitor is charged.

This information is from hardware perspective and I'm not sure if Arduino internal ADC routines compensate for those delays.

Digital Stability

From the digital read tutorial schmatic:

enter image description here

In the schematic, the push button is not electrically de-bounced. A delay in reading states would help de-bounce it in software.

As Nick Gammon suggested, they also improve serial flooding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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