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?
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It only takes a minute to sign up.Sign up to join this community
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".
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.
From the digital read tutorial schmatic:
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.