3

I am trying to connect to my Arduino Uno board two temperature sensors: DHT11 and DS18B20. The problem is with the DHT11 sensor: apparently there's an error with reading the temperature every two measurements.

Here's what I get on the serial monitor (temperature 1 is from the DHT sensor):

enter image description here

As I am new to this, I was wondering what the problem may be. I have read about the one wire interface used by the DS18B20, but I noticed that the DHT is not using the same protocol.

If I use the DHT11 alone, it works perfectly fine.

Also, I am using 4k7 pull-up resistors for both sensors.

Here's the code:

#include <DallasTemperature.h>
#include <OneWire.h>
#include <dht.h>

dht DHT;

#define ONE_WIRE_BUS 8
#define DHT11_PIN 7

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

void setup(void) {
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");
  sensors.begin();
}

void loop(void) {
  int chk = DHT.read11(DHT11_PIN);  
  sensors.requestTemperatures(); 
  Serial.print("Temperature1: ");
  Serial.print(DHT.temperature);
  Serial.print(" Temperature 2 : ");
  Serial.println(sensors.getTempCByIndex(0)); 
}
  • Are the OneWire and dht libraries compatible? They may be using the same resources (for example, the same processor timer). An easy way to tell is if they work independently. And you have already proved that the DHT will work if you are not using the DS chip. Now, if you can explain exactly what you did ... we would have a better idea if there really is a conflict between the two libraries. Or, you could review the code from both libraries and see if there is a conflict. – st2000 Jul 2 '16 at 13:27
  • I am not really able to understand the code behind those libraries, but as far as I looked I can't really find any conflicts. Here's the link for the OneWire library: github.com/ntruchsess/arduino-OneWire and the link for the DHT library github.com/adafruit/DHT-sensor-library – elena A Jul 2 '16 at 13:41
  • 1
    Linked DHT library looks differently then one you use. There is no method read11 for instance. Are you sure you use that one? – Darko Jul 2 '16 at 20:39
  • My bad, you can download the one I'm using from here: arduino-info.wikispaces.com/DHT11-Humidity-TempSensor – elena A Jul 3 '16 at 12:12
  • @Darko, you're right, elena is using this project's libraries: github.com/RobTillaart/Arduino/blob/master/libraries/DHTlib/… – st2000 Jul 3 '16 at 12:27
2

There appears to be several DHT sensor Arduino libraries available:

You might try an alternate library to see if that solves your current problem. Note, some if not all of these libraries need to know which DHT model you are using.

Before going through the DHT and OneWire source code line by line looking for conflicts, try to narrow down what is causing the problem:

  1. Try only instantiating the OneWire class and skip calling the read method within the class.

If this works, then the conflict likely resides in the OneWire read method and not in the OneWire initialization code (that is, the OneWire's Constructor).

  1. Calling the DHT read method multiple times consecutively to see if the problem resolves it self.

Actually, you have done this and exposed a curious patter of good-bad-bad-good-bad-bad... which is worth looking into. The point here is that if only the 1st call to the DHT read method after a call the the OneWire read method was bad, then there might be a setup problem in the DHT read method.

  1. Call the DHT Destructor, the DHT Constuctor then make the call to the DHT read method every time you read the DHT sensor.

This is a test case only and an extreme measure. What you are doing here is re-initializing the DHT library (driver) every time you use it. If this works it points to an initialization problem where the DHT library is initialization something which should probably be re-initialized again inside the DHT read method. As the OneWire library is using the same "something" (resource).

One last thing, the OneWire example I looked at here: PaulStoffregen/OneWire ...did not use the DallasTemperature library. I doubt this will make a difference, but it is usually best to keep your code simple and avoid unnecessary complexities.

Added later...

As pointed out in the comments, it turns out the DHT11 only takes new readings every few seconds. Guessing, the DHT11 library you are using either knowingly returns an unreasonable value of -999.0 until this time passes. Or is returning exactly what the DHT11 reports which, again, is an unreasonable value of -999.0.

In this DHT11 library, there is code:

boolean DHT::read(bool force) {
  // Check if sensor was read less than two seconds ago and return early
  // to use last reading.
  uint32_t currenttime = millis();
  if (!force && ((currenttime - _lastreadtime) < 2000)) {
    return _lastresult; // return last correct measurement
}

...which returns the last good value for 2 seconds after a DHT11 "real reading" should the library user call upon it faster then the DHT11 can respond.

2

DHT11 can only be read every 1 second. See manual.

  • Would you mind linking to the manual you're mentioning and provide exact reference within it for the "1 second" you mention? – jfpoilpret Feb 5 '17 at 22:26
  • According to Adafruit "No more than 1 Hz sampling rate (once every second)" (adafruit.com/product/386). Search for "DHT11 Datasheet" – user31481 Mar 2 '17 at 13:54
0

To confirm the answers above, in my case https://github.com/adafruit/TinyDHT library worked fine with Arduino Uno, while http://playground.arduino.cc/Main/DHTLib refused to work. Same setup with DHT11 and DS1820.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.