I am creating a grow box for tropical plants or mushrooms. I have a program (that works thanks to another fine gentleman on the forum) that controls a relay with an RTC and potentiometer, it controls how many hours the relay is on in a 24 hour stretch (for lighting). The number of hours is printed to an LCD that is connected. This whole program, when separate, works. I also have a program that reads a DHT11 for temperature and humidity then prints the result to the LCD. This also works when separate. However, when I combine these programs everything falls apart. The DHT fails to read, the relay does not activate when it is supposed to, and NOTHING prints to the LCD. I am pretty convinced it is a conflict between the DHT ant RTC libraries but I figured I should get a more knowledgeable opinion before I try to re-write the program with a different library. I hope it's something simple that I overlooked.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#include <DS3231.h>
#include <Wire.h>
#define DHTPIN A5
#define DHTTYPE DHT11
DHT dht = DHT(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DS3231 clock;
RTCDateTime dt;
int D = 2000;
const int RelayS = 8;
const byte potPin = A2;
byte number, oldNumber;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
Serial.println("Initialize RTC module");
clock.begin();
pinMode(8,OUTPUT);
clock.setDateTime(__DATE__, __TIME__);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("H%: ");
Serial.print(t);
Serial.println(" ");
Serial.print("F: ");
Serial.print(h);
Serial.println(" ");
lcd.print("F: ");
lcd.println(h);
delay(D);
lcd.clear();
lcd.print("H%: ");
lcd.println(t);
delay(D);
lcd.clear();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Relay Control & LCD/Serial Print // Relay Control & LCD/Serial Print // Relay Control & LCD/Serial Print
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
number = map(analogRead(potPin), 0, 1024, 1, 25);
if (number != oldNumber) { // only print if changed
Serial.println(number);
oldNumber = number; // update
delay(10); // stability
}
lcd.print("Light HRS: ");
lcd.print(number);
delay(D);
lcd.clear();
if (dt.hour > 00 && dt.hour <= number) {
digitalWrite(RelayS, HIGH);}
else {
digitalWrite(RelayS, LOW);}
}
The DS3231 is an I2C device? Pin A5 is one of the I2C pins. The DHT is NOT a I2C device. I'd try a DIGITAL pin.
Which Arduino are you using? It matters a lot!
That did fix the LCD problem however the relay is still not functioning. Everything displays and changes correctly but the relay will not turn on or off with the right settings applied.
Post your changed code.
If you change your code, how will I know the thing do's and dont's.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#include <DS3231.h>
#include <Wire.h>
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht = DHT(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
DS3231 clock;
RTCDateTime dt;
int D = 2000;
const int RelayS = 8;
const byte potPin = A2;
byte number, oldNumber;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
Serial.println("Initialize RTC module");
clock.begin();
pinMode(8,OUTPUT);
clock.setDateTime(__DATE__, __TIME__);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR // DHT SENSOR
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
delay(10);
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("H%: ");
Serial.print(t);
Serial.println(" ");
Serial.print("F: ");
Serial.print(h);
Serial.println(" ");
lcd.print("F: ");
lcd.println(h);
delay(D);
lcd.clear();
lcd.print("H%: ");
lcd.println(t);
delay(D);
lcd.clear();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Relay Control & LCD/Serial Print // Relay Control & LCD/Serial Print // Relay Control & LCD/Serial Print
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
number = map(analogRead(potPin), 0, 1024, 1, 25);
if (number != oldNumber) { // only print if changed
Serial.println(number);
oldNumber = number; // update
delay(10); // stability
}
lcd.print("Light HRS: ");
lcd.print(number);
delay(D);
lcd.clear();
if (dt.hour > 00 && dt.hour <= number) {
digitalWrite(RelayS, HIGH);}
else {
digitalWrite(RelayS, LOW);}
}
You should use millis() to get the DHT things. Reading a DHT quicker than once every 2000 ms may cause issues.
Have you printed the dt.hour see what is the value the if statement is working with?
That was the problem! I thought because I was still getting the number that the clock was reading correctly. I was missing dt = clock.getDateTime();. Thank you for helping me get this cleared up! I'm still not very fluent with C so a lot of this is guesswork for me.
So now that it works I tested it and found that the dht11 was going down in temperature the hotter it was. For example, ambient said 50 degrees and when I put it directly in front of a heater it read 32... any idea what would cause that?
See post#8.
I do not use the DHT sensors anymore for, as you'll find out, obvious reasons.
Most likely your code is processing the DHT data faster than once every 2000ms. As I wrote before use millis() to do the dht reads.
Once again.
The DHT sensor needs 2000ms between reads. Understand?
Yeah I understand, do you have a recommendation for an accurate temp and humidity sensor to replace the DHT with in the future?
I have used the BME this and BMP that to have settled on the BME680's.
Heck there is a BME680 in there.
ryanide
January 6, 2022, 10:58pm
14
I'll look into replacing the DHT with them! Thank you so much for all of your help!