I have a duinotech sensor (identical to keyes ky-015 Temperature Humidity sensor module. Sitting on my desk is my enviromental DMM. the Multimeter is reporting 60%rh and 20.2degC, the weather station across the room is reporting 6%rh at 19.5 yet the duinotech/keyes module is giving me 42.1% humidity and flicking between 30.1 and 31.1deg C.
The code I got was from a github page for the sensor. I have the readings coming up on a oled screen.
Top of code:
//
//KY015 DHT11 Temperature and humidity sensor setup (code snippet from sample code)
int DHpin =8;
byte dat [5];
byte read_data (){
byte data;
for(int i =0;i < 8;i ++) {
if(digitalRead (DHpin)==LOW) {
while (digitalRead (DHpin)==LOW); // wait for 50us
delayMicroseconds (30);// determine the duration of the high level to determine the data is '0 'or '1'
if (digitalRead (DHpin)==HIGH)
data |= (1<< (7-i));// high front and low in the post
while (digitalRead (DHpin)==HIGH);// data '1 ', wait for the next one receiver
}
}
return data;
}
part of setup that deals with sensors:
// ******** config sensor pins
pinMode (ldr,INPUT);
pinMode (DHpin,OUTPUT);
delay(20000); // delay to alow all sensors to stabilise. this MUST be last line in setup AFTER pin configs
the part of the code that reads the sensor:
void temp_hum_read(){ // rerad digital temp and humidity sensor
digitalWrite (DHpin,LOW);// bus down, send start signal
delay (30);// delay greater than 18ms, so DHT11 start signal can be detected
digitalWrite (DHpin,HIGH);
delayMicroseconds (40); // Wait for DHT11 response
pinMode (DHpin,INPUT);
while(digitalRead (DHpin)==HIGH);
delayMicroseconds (80); // DHT11 response, pulled the bus 80us
if(digitalRead (DHpin) ==LOW);
delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data
for(int i =0;i < 4;i ++) // receive temperature and humidity data, the parity bit is not considered
dat[i] =read_data ();
pinMode (DHpin,OUTPUT);
digitalWrite (DHpin,HIGH);// send data once after releasing the bus, wait for the host to open the next Start signal
}