DHT22 running @ 8MHZ

    while(digitalRead(pin) == HIGH)
        if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;


loopCnt = 0;
    while(digitalRead(pin) == HIGH)
        if (loopCnt1++ ==DHTLIB_TIMEOUT ) return DHTLIB_ERROR_TIMEOUT;

are almost identical, the main difference is that the compiler can optimize the first as when a value is loaded in a register it automatically compares it with zero. Comparing the loopCount with a constant takes at least an additional subtraction.

but I understand that you use the ++ version to measure the loops LOW and HIGH and compare the ratio. No need for micros();
That could also be done with the – version (2 vars) , only the compare logic need to be inverted (TODO).

first : digitalRead() is too slow

digitalRead takes about 4 uSec on a 16 Mhz , so on a 8Mhz it takes about 8 uSec. That is fast enough to recognize the difference between a zero and a one. Already shown by others, see - http://playground.arduino.cc/Main/DHTLib -.

FastDigitalRead will definitely be better to recognize due to its higher sampling rate. But how about its portability?

The ultimate speed for polling is to use direct port manipulation in the inner loop as the pin is known. (TODO).

have some experiments todo

Latest AOSONG datasheet says (a least for AM2302/DHT22) that the time “tgo” is comprise between 20µ and 200µs with a mean of 30µs.

do you have a link?

Sorry I loss the link :drooling_face: but I join the files I have downloaded

digitalRead takes about 4 uSec on a 16 Mhz

So for a short “one” loopCnt equal 6 at maximum, but in fact you have rather a value of 2 or 3.
There is no margin and I think it is the reason of TIMOUT or Checksumm error.

I made measurement to evaluate the execution of a function like this :
I use Timer T2 : it is 8bit like the microcontoler, it is not use by too many arduino functions.
I set prescaler to 1 so one increment on TCNT2 equal 1 clock cycle.

To avoid having to handle counter overflow I reset TCNT2 before all measurement
TCCR2B = 1; // prescaler = 1
uint8_t cycle ;
uint8_t read ;
/* ----------------
TCNT2 =0 ; // Raz ( Reset)
read = digitalRead(x) ;
cycle = TCNT2;

Cycle give the number of system clock periods.
DigitalRead(x) take 60 periods.

AM2301.pdf (577 KB)

AM2302.pdf (570 KB)

AM2303.pdf (578 KB)



I'm doing this with a 8Mhz boot loader and all working good.

When you build and upload what board target are you using? Try using the 8Mhz lily pad board as this will include the correct libraries that ensure the timings are correct.



Hi all — this is only for your information, reflecting my experience with different libraries

Building a low power temp/hum sensor I would like to use an 3.3V/8MHz pro mini and a DHT22.

Usually I use the DHT library from Adafruit and the sensor runs on an Duemilanove running at 16MHz.
Using the pro mini it shows 0.00 for temp and humi.

Following this thread and Class for DHT11, DHT21 and DHT22 (temperature & humidity) (thanks) I rebuilt all with the DHTlib from Rob Tillaart and surprisingly it now worked on the pro mini.
Adding the NRF24L01 for data transmission was easy.