Search code examples
arduinolcdarduino-c++

Arduino practice with a DHT 11 temp/humid sensor but having an issue


I am currently sending temperature data from the sensor to an lcd 16x2 display I am able to display the temperature but it keeps alternating from the temperature to -999 it displays the data like this too in the Serial monitor when I print it their too. Can anyone tell me what I'm doing wrong? Here is my sloppy code sorry it's not properly noted but I'm still grasping the basics.

#include <LiquidCrystal.h>
#include <dht.h>

dht DHT;

#define DHT11_PIN 12

LiquidCrystal lcd(13 ,11,10,9,8,7,6 ,5 , 4, 3 );

void setup(){
  Serial.begin(9600);
}

void loop()
{
 int chk = DHT.read11(DHT11_PIN);
 lcd.begin(16,2);
 lcd.print("Temperature =");
 lcd.setCursor(0,2);
 lcd.print(DHT.temperature);
 delay(1000);
}

Solution

  • first of all, just as an advice, place the lcd.begin() function in the setup function, and not in the loop, because the lcd must be initialized just one time.

    Then, reading the datasheet of the DHT11 sensor I found that the minimum time between two sensor readings should be at least 2 seconds, so probably changing delay(1000) to delay(2000) should do the job.