DHT sensor giving reading error.

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

Hi.

  • yes, pull up resistor for dht22 ( 4.7 or 10K ) is needed.

  • for the relays and the LCD don't uses arduino to power them. Use another power supply, even the 2 dht22 could be powered by an external power source rather than been from the arduino board.
    Assuming that relay board is 5v, get a 5v 1 amps ( maybe 750 mA could be enough, just check dht, relay, LCD specification ) to power dht22, LCD, and relays. Just share the GND from both of power supply.

Ups... 1st schematic was bad idea too..

Hello arssant,

Thanks for your awnser.

Could the fact that I am powering all of them from Arduino cause any instability in terms of the sensor, not allowong it to read correct values? How does this happens?

ren1:

  • 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

Ups... didn't saw that.
Sound very bad idea. DHT22 should be supplied from 5v or 3.3 vts pin ( if no more device are supplied from arduino ), never from a digital pin.
But you have a dual relay board a lcd, and 2 dht22. As I said, get a 5v 800mA/1A power supply to power all devices. Share the GND with both power supply's.

Hi arssant,

Thanks, I will try this and post the results!

Just a question: do I have to share the same GND pin on Arduino for all the external power source? Why is that? Is there any max current I can connect to the same GND pin on Arduino?

Lets see if I got your Idea: power relay and LCD from external 5V source, and the 2 DHT from Arduino? Can I power both DHT using the same 5V pin?

Best regards

Sharing GND, is a basic electronic-electricity concept-rule.
As you will use 2 power supply you need to share GND.

GND

Mike posts.

Attached schematic. paint mode, the best I could. :slight_smile:

dual.JPG

I see, I am not familiar with this basic concept, I will study more about it, still a noob in electronics. I thought as long as I provided a vcc and a ground for each sensor and component it was OK. Still don't get why it has to be the same GND.

So, Arduino UNO has three GND pins, if I am not mistaken. Are they connected somehow together? Or do I have to wire all components to the same pin to get a common ground?

Also I am lacking 5v external power supply. Do you think it is OK if I power the relay module from Arduino IOREF, 1 DHT from 5V pin and 1 DHT from 3.3v pin? I would then connect all of them to the same GND pin. And would power LCD from external 5V supply , and also connect the ground to the same arduino ground. Is that OK?

All 3 GND pins are in the same line, they are connected to the 2.1mm power jack of the power input and to the usb connector.

For the 5v power supply, do you have any smart-phone power adaptor ? most of them are rated to 5vt 450mA up to 2 Amp. It just enough for the relay, LCD and DHT.
Why use a 2nd power supply ? to not stress and reach over capacity ( amperage ) of the build in regulator, that convert your 12vts power to 5vts available over 5 vts pins.
I'm not a expert myself, but I understand the basics of current and voltage. The issue in your schematic is not lack of voltage, but current.
The arduino board have a limit of current available, and you ask more than it could provide, you will get instability even could burn your arduino regulator.

Wow, now you totally nailed it to me. I will apply this modifications to the circuit and see the result. I will post it when I finish it. Thanks a lot for your help arssant!

Best regards

To update this post for archive, I have implemented 10K resistors and all the trouble about not being able to initialize the sensors have disappearead. So, if you find yourself with unreliable behaviour with DHT22, make sure you have these resistors propely placed, such as illustrated above. They are essential for a reliable system. I, for example, was able to run correctly my system for a few times without the resistors, but them it stopped being able to talk to the sensors completely, and only these resistors were able to correct the problem.

Now, suddenly my relay is not receiving voltage from the 5V pin on arduino, and I am wondering why. I have still everything powered by the arduino board. I am wondering if my power source, and old PC font, is capping the necessary current to make the two sensors, the relay and LCD work properly. Which is again strange, since everything worked marvelously in previous sessions.

Also, I have read that the LCD with backlight on have a current drainagr arroind 200 mA, with the DHT sensors around 3mA and relay with 75mA per coil (not sure if these values re correct). I May be wrong, but from this (post I have understood that Max current drainage from Arduino with external power sourcing is 500mA. So I am under this limit. What are other possible facts that may be preventing the Arduino from providing 5V from the 5V pin to the relay module?

Anyway, tomorrow I will follow the suggestions by arssant and power everything from a external source, to see if arduino overloading might be the source of this problem.

Best regards