Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ESP8266 & DHT22 problem reading values

Status
Not open for further replies.
Hello,

I am making a simple control system using technology that i have been using for about 2 years now with no problems. This is the first time i have a bit of difficulty.

Hardware:
1) ESP8266 Wemos D1, powered through 5.1V (external 12V 2A power suply + step-down to get 5.1V, step-down can supply up to 1.5A, much more than i need)
2) 4x DHT22 sensor

Alright so this is 4 sensors on an ESP8266. I power the sensors from the 5V pin on esp8266 board. To clarify, i tried also using 3.3V pin on wemos d1 board with no difference.

Pins used:
- D2, D3, D4, D5 (here i am talking about pins on wemos d1 board, that is, that is how pins are numbered on a board)

The sensor wires are about 1-1.5m long, i used old UTP cable for that.

This is the first time i am actualy using 4 sensors on 1 board, till now i only used 1 or 2 at most. As you can see on image in the attachment, first 3 sensors on pins D2, D3 and D4 are working ok, only the sensor on pin D5 is giving me trouble. I tried switching sensors/wires to see if it helps, i even tried connecting a separate sensor with only 10cm long wire to it, with no difference.

Software used:
- arduino ide and i am using a DHT library for arduino, gave me no problems so far.

Code:

Code:
for(int i=0;i<3;i++) {       
    sensor4_temperature = dht4.readTemperature(); // Gets the values of the temperature   
    if(!isnan(sensor4_temperature) && (0 < sensor4_temperature < 100)) break;
    else {
      delay(50);       
    }       
}

So what could be the problem in this case ? Right now pin D5 is giving me problems but yesterday it was pin D4. My first thought would be the long cables but i read that some people use DHT22 on cables as long as 20 meters and have no problems. And even if this is the reason, it makes no sense that when i connect the sensor with short cable, it makes no difference.
server.JPG
 
It maybe something to do with the library. On a project I did many years ago I read the DHT directly. Here's the code if it helps.
Code:
uint8_t error;

uint8_t readDHT(){
  error=false;
  pinMode(DHT,OUTPUT);
  digitalWrite(DHT, LOW); 
  delay(2);
  digitalWrite(DHT, HIGH); 
  delayMicroseconds(40);
  pinMode(DHT,INPUT_PULLUP);
  waitLow();
  if(error){     //is sensor pulling low
    //Serial.println("DHT not present");
    return false;             //no, so return
  }
  uint8_t i,j;
  waitHigh();
  waitLow();
  for(i=0;i<5;i++){
    for(j=0;j<8;j++){
      waitHigh();
      delayMicroseconds(40);
      DHTbuf[i]<<=1;
      if(digitalRead(DHT)==1)
        DHTbuf[i]++;
      waitLow();
    }
  }
  uint8_t sum = DHTbuf[0] + DHTbuf[1] + DHTbuf[2] + DHTbuf[3];
  if (DHTbuf[4] != sum) 
    return false;
  return !error;
}

void waitLow(){
  if(error)
    return;
  uint16_t timeout=1000;
  while(digitalRead(DHT)==1 && --timeout>0);
  if(timeout>0)
    return;
  error=true;
}

void waitHigh(){
  uint16_t timeout=1000;
  if(error)
    return;
  while(digitalRead(DHT)==0 && --timeout>0);
  if(timeout>0)
    return;
  error=true;
}
If you make DHT a variable containing the pin number then you should be able to read any sensor.

Mike.
 
Thank you for your reply, i will save that code right away ! As for my error readings, i got a bit of an update.

1) i connected a shorter sensor to D5 now and it seems to work, been reading for 15 hours in a row without a single error, which is amazing to me because this cheap sensors and cheap Wemos d1 simply tend to produce an occasional error. I am running first 3 sensors on 1-1.5m wires and 4th sensor on 10cm wires.

2) I just realised this, don't know how this got past me but .. i am powering the sensors from 5V pin on the board. And i am using the ground pin for ground and D2-D5 for data. I don't know how that got past me but, in the sensor, data line and VCC line are internaly connected. So that means i am effectively not only putting 5V on ground pin but also on pins D2-D5. There has been a lot of speculation whether IO pins are 5V resistant. Many say they aren't, then there was a screeshot of the actualy manufacturers of the hardware that said they actualy are 5V tolerant. I have been running 3 setups like this for 2 years now, putting 5V on data and ground of the board itself. It seems to work so far but i am not very comfortable since i am still unsure whether it is ok or not. And second, there is a problem. I can run sensors on 3.3V also, but DHT22 datasheet states that: DHT22 can do 30 meters on 5V and only 1 meter on 3.3V, talking about cable length.

I realised that i might need even longer cables, about 2m .. so i will have to test. And if possible i would like to avoid using different ESP8266 board for each sensor to keep things simple and to be able to collect data from only 1 server that ESP8266 is running.

Two questions:

1) I am using UTP cables right now for the sensors. I use 3 wires from the bulk of UTP wires: Ground,VCC and data. I read that 1 of the solutions might be to simply use more than 1 wire for data to reduce impedance of the wire. What if i use about 4 wires for the data, simply solder its ends together ? Do u think that might be useful ?

2) I read in the DHT22 datasheet that it might help putting 100nF capacitor between VCC and ground to filter the noise. Does that apply only to VCC and ground line and on which part: on the part of wemos d1 output or right infront of sensor or maybe both ? Could you tell me more about this ?
 
Last edited:
I'm also not sure about the esp being 5V tolerant but did run a DHT for over a year on one. One thing not often mentioed is the need for a pullup on the data line, do you have this? I tied the pullup to the 3.3V supply.

Mike.
 
One of the Espressif people has publicly posted the the ESP8266 pins are 5 volt tolerant.

He went on to say that they removed this from the documentation because people are idiots – too many people destroyed ESP8266s by powering them from 5 volts. "If the pins are 5 volt tolerant, that means I can power the chip from 5 volts, right?" Poof!
 
Pommie: I am using a DHT22 board that already has that pullup on it. I am not sure if i would get anything from the sensor without the pull up. In my case its a problem that sensors work for hours and then suddenly i get nan from certain sensors. I will try to use a thicker wire to see if that solves it. I am guessing this is the reason. If i wont be able to solve it any other way, i will simply program the esp8266 to restart if a certain sensor produces errors for 5 minutes in a row. Restarting often seems to solve it. I will play around with the code a bit and see what helps. I guess thicker wire couldn't hurt ?

Visitor: That is what i was talking about yes, there was a post that they are tolerant and then it was removed just to make sure people dont wrongly assume they can power it by 3.3V. Like i said i have 3 systems running right now, each using a DHT22 sensor and all 3 sensors are powered by 5V pin on wemos d1 board. And other 2 wires from sensor go to ground and to IO pin. So the fact that it runs like this for a year with no problem can either be a proff that pins are indeed 5V tolerant OR that somehow i am extremely lucky and didn't burn any of the boards yet. I am hoping it is the first thing. As for powering the ESP8266 i don't have problems since i am using a micro-usb to power the wemos d1 board and i am using 5.1V to power it (12V -> 5.1V through step-up and then just a simple convertor from 2 pins into microusb).

I decided for my home systems where i need wire length only about 30cm, i will just use 3.3V pin on board. But for those where i need 2 meters length, i simply cannot go any other way but using 5V (which gives me 4.6V btw, not 5V ... while 3.3V pin on wemos board is always 3.3V)
 
I just wanted to post some findings, i find them interesting. I did some more testing and things are interesting. Right now i have the ESP8266 wemos d1 connected to usb port of my computer. And for some reason sensor 3 isn't working. I tried swaping the existing 1m cable with a short 10cm wires and it still isn't working. But there are 2 ways i CAN make it work. First is simply disconecting and reconecting the sensor while the program is running. That will fix it and it will run no problem. And second is using anything other than computer to power the board. So far i tried a mini 5V DC phone charger, i tried powerbank and i tried using step-down to get from 12V to 5V. It all works without a single error. As soon as i use computer, sensor 3 is giving me problems. I tried different usb ports, front ones, back ones on motherboard, i even tried the USB 3.0 port just to make sure its not running out of juice. Since all USB ports are able to give out 100x more amps than the sensors need, i am thinking, could it be the problem of interference ? Swapping sensor makes no difference, its the same with any sensor and any wire length.

Now this won't be an issue since i will be using external power suply. But it still makes me wonder what could be the reason.

To sum it up:

1) Sensor 3 is giving problems (Sensor 3 is on D4 pin of the ESP8266 wemos board). That is supposed to be GPIO2 pin so i am wondering if there is anything special about it that is giving the problems.
2) Using a different sensor OR using a short 10cm wires makes no difference.
3) Only see this problem if using ANY USB port on pc to power the ESP8266 wemos d1 mini board. If i use anything else, any external power suply, the problem disapears and all sensors read with no errors.
4) Even if i run it from computer usb and i get errors AKA not even 1 succesful value read from sensor ... if i disconect the sensor while the program is running and i reconnect it, the sensor will work properly from that moment on. But if i disconect USB cable and reconnect it, i will get errors again with not a single succesful read (nan value on sensor non stop).

So what do you say, is it pc interference ? Because as far as amps go ... usb 3.0 is supposed to be able to provice 0.9A .... while the gsm charger i used is 1.55A ... both of them should be able to handle not 4, but 100 of those sensors no problem.
 
I just spent hours finding a solution to my problem and i forgot that i already knew the problem 2 years ago.

So, i implemented the ESP.Restart(); in my code for when i get error on sensor for 5 minutes in a row. Ok, so i upload the code and its stuck in reboot. Replug the cable back in and it suddenly works. Ok, i upload new code again .. on first restart, it locks. I tried unplugging every single wire and i have tons, i tried changing all settings, trying to figure out if i am pulling something low or high where i shouldnt. Anyway, best spent 3 hours i have to say. I only remembered of this because i finaly stumbled on a webpage where i remember seeing this for the first time. You upload the code and the first ESP.Restart() will simply lock up. You unplug cable and plug it in again to restart the board and from now on ESP.Restart() works no problem. So its cool, no problem.

But has there been any solution to this ? Or is this hardcoded in such level that it cant be solved by software. What is the reason for this anyway ?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top