#define PIN_DHT 8
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Start DHT reading…");
sendStartSignal();
Serial.println("Reading data…");
waitForResponsePulse();
printRawData();
Serial.println("End of data!");
delay(5000);
}
void sendStartSignal() {
pinMode(PIN_DHT, OUTPUT);
digitalWrite(PIN_DHT, LOW);
delay(20);
digitalWrite(PIN_DHT, HIGH);
}
void waitForResponsePulse() {
pinMode(PIN_DHT, INPUT);
while (digitalRead(PIN_DHT) != LOW) {}
while (digitalRead(PIN_DHT) != HIGH) {}
while (digitalRead(PIN_DHT) != LOW) {}
}
void printRawData() {
const uint8_t NB_BYTES = 8;
uint8_t results[NB_BYTES][8];
for (uint8_t i = 0; i < NB_BYTES; i++) {
readRawByte(results[i]);
}
for (uint8_t i = 0; i < NB_BYTES; i++) {
for (uint8_t j = 0; j < 8; j++) {
Serial.print(results[i][j]);
}
Serial.println("");
}
}
void readRawByte(uint8_t* results) {
for (uint8_t i = 0; i < 8; i++) {
while (digitalRead(PIN_DHT) != HIGH) {}
delayMicroseconds(50);
if (digitalRead(PIN_DHT) == HIGH) {
results[i] = 1;
} else {
results[i] = 0;
}
// while (digitalRead(PIN_DHT) != LOW) {}
}
}