un code C pour avr-gcc :
#include <avr/io.h>
#include <util/delay.h>
#define DTH_ERR_TIMEOUT -1
#define DTH_ERR_CHECKSUM -2
typedef struct{
int temperature;
int humidity ;
} dht_values;
#define DTH_DDR DDRD
#define DTH_PORT PORTD
#define DTH_PIN PIND
#define DTH_IO 7
#define DTH_ANSWER_SIZE 40
#define DTH_TIMEOUT (F_CPU/40000) // 2,5 ms
#define DTH_WAIT_LOW_MS 20
#define DTH_WAIT_HIGH_US 40
#define DTH_LOOP_CYCLES 2
#define DTH_HIGH_DELAY_US 40
static void dht_trigger(void){
uint8_t mask=(1<<DTH_IO);
DTH_DDR |= mask;
DTH_PORT &= ~mask;
_delay_ms(DTH_WAIT_LOW_MS);
DTH_PORT |= mask;
_delay_us(DTH_WAIT_HIGH_US);
DTH_DDR &= ~mask;
}
int dht_get(dht_values *values){
uint8_t bits[DTH_ANSWER_SIZE/8];
int i,l;
dht_trigger(); // Trigger answer
uint8_t mask=(1<<DTH_IO); // Check acknowledgment
for(l=DTH_TIMEOUT;l>=0 && (DTH_PIN&mask)==0;l--); if(l<0) return DTH_ERR_TIMEOUT;
for(l=DTH_TIMEOUT;l>=0 && (DTH_PIN&mask)!=0;l--); if(l<0) return DTH_ERR_TIMEOUT;
for(i=0;i<DTH_ANSWER_SIZE;i++){ // Read values
if(i%8==0) bits[i/8]=0;
for(l=DTH_TIMEOUT;l>=0 && (DTH_PIN&mask)==0;l--); if(l<0) return DTH_ERR_TIMEOUT;
for(l=DTH_TIMEOUT;l>=0 && (DTH_PIN&mask)!=0;l--); if(l<0) return DTH_ERR_TIMEOUT;
uint32_t time=l*DTH_LOOP_CYCLES/(F_CPU/1000000L);
bits[i/8] <<= 1;
if(time<DTH_HIGH_DELAY_US) bits[i/8] |= 0x01;
}
uint8_t sum=bits[0]+bits[2]; // Verify checksum
if(sum!=bits[4]) return DTH_ERR_CHECKSUM;
values->humidity=bits[0]; // Convert values
values->temperature=bits[2];
return 0;
}