Hello,
I am making an automated system to control humidity inside two chambers, maintaining its level on the range of 65-80 by turning on or off dehumidifiers inside each chamber. To do that, my system is composed by 2 DHT22, 1 2-Relay Module, 1 LCD screen, 1 Arduino UNO. The wiring is "newbly" draw in the attachment.
Some observations are required:
- The Arduino power is coming from a 12V source, not 9V. Could not find this part in the drawing program.
- The sensors are powered from Arduino digital pins. I have read some posts around that said for this DHT22 sensor, with low current drainage, it is ok to do so.
- The 2-relay module is powered from arduino even though I read it is better to power it from external source for isolation. I will install a external power source connection for it, but I think this is not the source of the problem at first glance.
The code is as it follows. I tried to translate most of it and add English comments, but some of the variables are still in my native language, sorry about that.
#include <LiquidCrystal.h>
#include <DHT.h>
//Define Sensor 1 wiring
#define DHTTYPE1 DHT22
#define power1 3
#define DHTPIN1 2
#define gnd1 1
//Define Sensor 2 wiring
#define DHTTYPE2 DHT22
#define power2 13
#define DHTPIN2 12
#define gnd2 11
//Define relay module wiring
int relay_direita = 0; //turns on right hand side machine
int relay_esquerda = 10; //turns on left hand side machine
//Define variables which will tell if machine is on or off
int relay_direita_ligado = 0;
int relay_esquerda_ligado = 0;
//Define max and min values for humidity
int umidade_maxima = 80;
int umidade_minima = 65;
//Define general variables (max, min, present value) form temperature and umidity
float umidade_direita;
float umidade_esquerda;
float temperatura_direita = 0;
float temperatura_esquerda = 0;
float temperatura_esquerda_max = 0;
float temperatura_esquerda_min = 100;
float temperatura_direita_max = 0;
float temperatura_direita_min = 100;
//Define Error variables, which will keep how many reading errors have ocurred for each sensor.
int Erro1 = 0;
int Erro2 = 0;
DHT dht1(DHTPIN1, DHTTYPE1);
DHT dht2(DHTPIN2, DHTTYPE2);
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
//Initialize digital pins for power and gnd for sensor 1.
pinMode(power1,OUTPUT);
digitalWrite(power1,HIGH);
pinMode(gnd1,OUTPUT);
digitalWrite(gnd1,LOW);
//Initialize digital pins for power and gnd for sensor 1.
pinMode(power2,OUTPUT);
digitalWrite(power2,HIGH);
pinMode(gnd2,OUTPUT);
digitalWrite(gnd2,LOW);
delay(1000);
dht1.begin();
dht2.begin();
lcd.begin(16, 2);
//Initial messages on LCD
lcd.setCursor( 0, 0);
lcd.print("Hello");
lcd.setCursor( 0, 1);
lcd.print("Lets start");
delay(3000);
lcd.clear();
lcd.setCursor( 0, 0);
lcd.print("Beginning");
lcd.setCursor( 0, 1);
lcd.print("system...");
/*Initialize relay pins. They are Active low. */
digitalWrite(relay_direita, HIGH);
digitalWrite(relay_esquerda, HIGH);
pinMode(relay_direita, OUTPUT);
pinMode(relay_esquerda, OUTPUT);
}
void loop() {
// Wait for DHT22 to stabilize.
delay(2000);
//Read humidity
umidade_direita = dht1.readHumidity();
umidade_esquerda = dht2.readHumidity();
//Ready temperature
temperatura_direita = dht1.readTemperature();
temperatura_esquerda = dht2.readTemperature();
//Check if any max or min value has been read.
if (temperatura_direita > temperatura_direita_max) {
temperatura_direita_max = temperatura_direita;
}
if (temperatura_direita < temperatura_direita_min) {
temperatura_direita_min = temperatura_direita;
}
if (temperatura_esquerda > temperatura_esquerda_max) {
temperatura_esquerda_max = temperatura_esquerda;
}
if (temperatura_esquerda < temperatura_esquerda_min) {
temperatura_esquerda_min = temperatura_esquerda;
}
// Check for reading error.
if (isnan(umidade_direita) || isnan(temperatura_direita)) {
Erro1 ++;
lcd.clear();
lcd.setCursor( 0, 0);
lcd.print("Erro no sensor 1");
lcd.setCursor( 0, 1);
lcd.print("Qtde erros: ");
lcd.print(Erro1);
delay(2000);
}
if (isnan(umidade_esquerda) || isnan(temperatura_esquerda)) {
Erro2 ++;
lcd.clear();
lcd.setCursor( 0, 0);
lcd.print("Erro no sensor 2");
lcd.setCursor( 0, 1);
lcd.print("Qtde erros: ");
lcd.print(Erro2);
delay(2000);
}
//Check if it is necessary to turn on/off any relay. It should be turn on if humidity > umidade_maxima and off if humidity < umidade_minima
if(umidade_direita > umidade_maxima) {
digitalWrite(relay_direita, LOW); //Modificar de acordo com a característica do relay: se ele é Active HIGH ou Active LOW. Aqui deve-se garantir que o relay será ligado (umidificador será ligado).
relay_direita_ligado = 1;
} else if (umidade_direita < umidade_minima) {
digitalWrite(relay_direita, HIGH); //Modificar de acordo com a característica do relay: se ele é Active HIGH ou Active LOW. Aqui deve-se garantir que o relay será desligado (umidificador será desligado).
relay_direita_ligado = 0;
}
if(umidade_esquerda > umidade_maxima) {
digitalWrite(relay_esquerda, LOW); //Modificar de acordo com a característica do relay: se ele é Active HIGH ou Active LOW. Aqui deve-se garantir que o relay será ligado (umidificador será ligado).
relay_esquerda_ligado = 1;
} else if (umidade_esquerda < umidade_minima) {
digitalWrite(relay_esquerda, HIGH); //Modificar de acordo com a característica do relay: se ele é Active HIGH ou Active LOW. Aqui deve-se garantir que o relay será desligado (umidificador será desligado).
relay_esquerda_ligado = 0;
}
//print results on LCD for sensor 1.
lcd.clear();
lcd.setCursor( 0, 0);
lcd.print("CamDir ");
lcd.print("h");
lcd.print(umidade_direita, 0);
lcd.print("% ");
lcd.print("U");
if (relay_direita_ligado == 0) {
lcd.print("D");
} else if (relay_direita_ligado == 1) {
lcd.print("L");
} else {
lcd.print("E");
}
lcd.setCursor( 0, 1);
lcd.print("T");
lcd.print(temperatura_direita,0);
lcd.print("C ");
lcd.print("T+");
lcd.print(temperatura_direita_max,0);
lcd.print("C ");
lcd.print("T-");
lcd.print(temperatura_direita_min,0);
lcd.print("C ");
delay(4000);
//print results on LCD for sensor 1.
lcd.clear();
lcd.setCursor( 0, 0);
lcd.print("CamEsq ");
lcd.print("h");
lcd.print(umidade_esquerda, 0);
lcd.print("% ");
lcd.print("U");
if (relay_esquerda_ligado == 0) {
lcd.print("D");
} else if (relay_esquerda_ligado == 1) {
lcd.print("L");
} else {
lcd.print("E");
}
lcd.setCursor( 0, 1);
lcd.print("T");
lcd.print(temperatura_esquerda, 0);
lcd.print("C ");
lcd.print("T+");
lcd.print(temperatura_esquerda_max, 0);
lcd.print("C ");
lcd.print("T-");
lcd.print(temperatura_esquerda_min, 0);
lcd.print("C ");
delay(2000);
}
So, the historic of the problem is:
1 - I have mounted, wired and tested, everything worked perfectly and reliably for a good time.
2 - Recently I turned the system again and the Sensor 1 (dht1, on the right-hand side) was always giving reading errors (actually it did not give me any reading at all, just errors). I checked the connections, power, but everything was apparently fine. I then changed positions between Sensor 2 and Sensor 1 and voila, Sensor 1 worked (wired to 11,12,13) but Sensor 2 stopped working (wired to 1,2,3). Since I checked the power between 1 and 3 and it gave me close to 5v, which was expected, I concluded that PIN2 (data pin for sensor) was bad.
3 - So I changed to a new UNO board and wired everything again such as in the image attached. But for my surprise now the Sensor 2 (left hand side) does not work and Sensor 1 is fine.
What are your suggestions of the source of the problem? It seems to be some instability with my circuit or code, but I have no clue what is could be. I am starting to think that it is not about dead pins, since I think 2 boards with dead pins is quite a rare thing, isn't it? Is there any way I can check if the pins are good or bad?
I have read it is recommended a 4,7K to 10K pull-up resistor (even though I dont know what it means) connecting VCC pin to Data pin in each sensor. My system has worked weell without it previously, is it necessary? Could it be the source of the problems? I have read some vague (to me) explanations about it, talking about 2 way communications etc, but it was not clear (to me) if it was necessary.
Best regards,
Renan


