I'm trying to read temperature and humidity from a DHT11 sensor and write values to a 20x4 LCD. I hit a behaviour I don't understand. When I use the DHT tester program that come with the DHT library, everything works well. I get the DHT information on the Serial console. This DHT tester program use the pin 2 to read the data from the DHT.
I wrote the following code who works perfectly:
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 13 // Digital
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup() {
Serial.begin(9600);
while (!Serial) {
}
Serial.println("Serial OK!");
dht.begin();
lcd.begin(20,4);
lcd.setBacklight(0);
lcd.clear();
lcd.print("Hello! ");
Serial.println("Init finished!");
}
void loop() {
delay(2000);
float DHT_hum = dht.readHumidity();
float DHT_temp_c = dht.readTemperature(false);
float DHT_temp_f = dht.readTemperature(true);
if (isnan(DHT_hum) || isnan(DHT_temp_c) || isnan(DHT_temp_f)) {
Serial.println("Failed to read from DHT sensor!!");
lcd.clear();
lcd.print("fail! ");
return;
}
float heat_index = dht.computeHeatIndex(DHT_temp_f, DHT_hum);
Serial.print("Humidity: ");
Serial.print(DHT_hum);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(DHT_temp_c);
Serial.print(" *C ");
Serial.print(DHT_temp_f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(heat_index);
Serial.println(" *F");
lcd.clear();
lcd.print("Humi: ");
lcd.print(DHT_hum);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(DHT_temp_c);
lcd.print("C");
lcd.setCursor(14, 0);
lcd.print("HI: ");
lcd.setCursor(14, 1);
lcd.print(dht.convertFtoC(heat_index));
lcd.print("C");
}
As you can see, I use pin 13 for the DHT11 (DHTPIN). If I set to it to "2" then my program is no longer working (having the pin connected or not doesn't change the result). The serial display
Serial OK!
Init finished!
Failed to read from DHT sensor!!
and that's all, it even doesn't loop to display "Failed to read from DHT sensor!!" again. LCD text doesn't change too. Like if the program is totally exiting after "Serial.println("Failed to read from DHT sensor!!");"
I'm lost... The LCD lib I use is the very common one from fm https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home