Hi!
I am using a dht22 to monitor humidity and temperature, and I would like to print the values on a lcd.
here is the code
#include "DHT.h"
#define thresholdmax 27 //Setting the temperature threshold
#define thresholdmin 25 //Setting the temperature threshold
#define DHTPIN 13 // what pin we're connected to
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//const int relay1 = 8; // the number of the relay 1 pin
const int relay2 = 9; // the number of the relay 2 pin
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
// pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
delay(1000);
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
delay(1000);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
}
else {
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
if (t < thresholdmin) {
// digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
else if (t > thresholdmax) {
// digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
}
}
This code generates an erratic behaviour in which the relay is being constantly turned off and on. As soon as I remove the following part, the behaviour is normal.
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
Evidently it is not in the right part of the loop, or I am missing something else. Anybody sees my mistake?
thanks!