Delay() does not work (accurately)

Hi,

During debugging the DHT22 Lib I found out that the function delay(1) does not delay 1msec. To reproduce the error I wrote this short code for test.

void setup() {
    pinMode(A7, OUTPUT);
}

void loop() {
    digitalWrite(A7, LOW);
    delay(1);
    digitalWrite(A7, HIGH);
    delay(100);
}

The result ist this (video 2MB)
Oscilloscope, Timebase 200.0us

In words:delay(1) produces delays that are shorter than 1ms and the measured delay times jumps between 1ms to nearly zero.The delay gets shorter and shorter then jumps to a longer delay and get shorter again

I imagine this could be fixed in the firmware by having small delays <5ms delegate the handling to Delay_Microseconds() which is far more accurate.

Until then, you can replace delay(1) with Delay_Microseconds(1000) and check with your scope if is more accurate - I would expect it to have much less jitter and variance.

Yes, delayMicroseconds() works perfect.

1 Like

Issue here: https://github.com/spark/core-firmware/issues/260

1 Like