DHT11 Temperature Code Library

Hi everyone..

I'm using Arduino 1.0.6 (only compatible with a shield i've bought).

I've bought DHT11 temperature sensor and i'm not being able to charge the code into the arduino UNO.

I've already tried to import 5 different libraries of DHT, DHT11, DHTmasterlib, the one posted up here.. etc..

I've allways get the same error message:

'DHT' does not name a type.

My library is able to manage both Wprogram.h and Arduino.h with an if-else function..

I'm really desperate because i'm sure it's going to be a silly thing i'm avoiding.. but i need your help!!

Anyone here has the same problems and could give me another possible solution???

Thanks a lot in advance :wink:

ps: i've read full post DHT11 Post and tested all said there but nothing :confused:

Hi,
Make sure its DHT or dht, case makes a big difference.
DHT is totally different to dht.

Can you please post a copy of your sketch, using code tags?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png or pdf?

Tom..... :slight_smile:

Hi Tom and thanks for help.

the standard code:

/*Sensor de Temperatura y Humedad DHT11
Instrucciones:
Recuerda descargar la libreria DHT para poder utilizar este sensor
Conectaremos el Sensor DHT11 a 5v y el pin de señal a la entrada digital 7
*/
#include "DHT.h"
#define DHTPIN 7
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {  
int h = dht.readHumidity();// Lee la humedad
int t= dht.readTemperature();//Lee la temperatura
//////////////////////////////////////////////////Humedad
Serial.print("Humedad Relativa: ");                 
Serial.print(h);//Escribe la humedad
Serial.println(" %");                     
delay (2500);
///////////////////////////////////////////////////Temperatura              
Serial.print("Temperatura: ");                  
Serial.print(t);//Escribe la temperatura
Serial.println(" C'");                   
delay (2500);
///////////////////////////////////////////////////             
Serial.println("ElectroCrea.com");                     
delay (3000);
Serial.println ();
}

DHT.h library:

// 
//    FILE: dht11.h
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
//     URL: http://playground.arduino.cc/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
// 

#ifndef dht11_h
#define dht11_h

#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#define DHT11LIB_VERSION "0.4.1"

#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2

class dht11
{
public:
    int read(int pin);
 int humidity;
 int temperature;
};
#endif
//
// END OF FILE
//

DHT.cpp library

//
//    FILE: dht11.cpp
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// HISTORY:
// George Hadjikyriacou - Original version (??)
// Mod by SimKard - Version 0.2 (24/11/2010)
// Mod by Rob Tillaart - Version 0.3 (28/03/2011)
// + added comments
// + removed all non DHT11 specific code
// + added references
// Mod by Rob Tillaart - Version 0.4 (17/03/2012)
// + added 1.0 support
// Mod by Rob Tillaart - Version 0.4.1 (19/05/2012)
// + added error codes
//

#include "dht11.h"

// Return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht11::read(int pin)
{
 // BUFFER TO RECEIVE
 uint8_t bits[5];
 uint8_t cnt = 7;
 uint8_t idx = 0;

 // EMPTY BUFFER
 for (int i=0; i< 5; i++) bits[i] = 0;

 // REQUEST SAMPLE
 pinMode(pin, OUTPUT);
 digitalWrite(pin, LOW);
 delay(18);
 digitalWrite(pin, HIGH);
 delayMicroseconds(40);
 pinMode(pin, INPUT);

 // ACKNOWLEDGE or TIMEOUT
 unsigned int loopCnt = 10000;
 while(digitalRead(pin) == LOW)
 if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

 loopCnt = 10000;
 while(digitalRead(pin) == HIGH)
 if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
 for (int i=0; i<40; i++)
 {
 loopCnt = 10000;
 while(digitalRead(pin) == LOW)
 if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

 unsigned long t = micros();

 loopCnt = 10000;
 while(digitalRead(pin) == HIGH)
 if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;

 if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
 if (cnt == 0)   // next byte?
 {
 cnt = 7;    // restart at MSB
 idx++;      // next byte!
 }
 else cnt--;
 }

 // WRITE TO RIGHT VARS
        // as bits[1] and bits[3] are allways zero they are omitted in formulas.
 humidity    = bits[0]; 
 temperature = bits[2]; 

 uint8_t sum = bits[0] + bits[2];  

 if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
 return DHTLIB_OK;
}
//
// END OF FILE
//

All taken from arduino.cc but it doesn’t works for me… i’ve tried a lot of different libraries, codes, etc… but there’s no way :sob: :sob: :sob: :sob:

the sensor is like this one:

alfcasmo:
the sensor is like this one:

Circuit diagram?

Some DHT libraries activate and use the internal pull-up resistor of the Atmega controller to pull the data line HIGH.

And some other DHT libraries rely on an external pull-up resistor.

To make sure that your schematic does not lack the pull-up resistor, use an external pull-up resistor, connected to "data". Resistor value something like 3.3K to 10K.

Does the library manual tell you that you will need no external pull-up with that library?

Or do you have the external pull-up resistor connected to the data pin?

It's AOSONG DHT11 AOSONG DHT11

There's full specs DHT11 AOSONG full specs

I've also tried another code for aosong trademark AOSONG DHT11 CODE

But another time more i'm unable to use DHT libraries :frowning:

Also tested to pull up with 10k resistor at data pin. :frowning:

alfcasmo:
I’ve also tried another code for aosong trademark AOSONG DHT11 CODE

But another time more i’m unable to use DHT libraries :frowning:

Also tested to pull up with 10k resistor at data pin. :frowning:

Perhaps try this code (no library required):

// DHT11 and DHT22 sensor reading demo
// by 'jurs' for Arduino Forum

// DHT functions enumerated
enum {DHT11_SAMPLE, DHT22_SAMPLE, DHT_TEMPERATURE, DHT_HUMIDITY, DHT_DATAPTR};

// Begin of user configuration area
#define DHT_SAMPLE DHT11_SAMPLE // must be DHT11_SAMPLE or DHT22_SAMPLE
#define DHT_PIN 2
// End of user configuration area

// DHT error codes enumerated
enum {DHT_OK=0, DHT_ERROR_TIMEOUT=-1, DHT_ERROR_CRC=-2, DHT_ERROR_UNKNOWN=-3};

void setup()
{
  Serial.begin(9600);
  Serial.println("DHT Temperature and Huminity Measurement");
}

// DHT sensor pinout from left to right looking at the gridded side
// 1-VCC  2-DATA  3-NC  4-GND

int dhtCall(byte pin, byte function)
// input parameters are the data pin and one of the DHT functions
// return value is DHT error code with function DHT11_SAMPLE or DHT22_SAMPLE
// alsways do sampling with DHT_OK result before calling other functions
// return value is temperature with function DHT_TEMPERATURE
// return value is humidity with function DHT_HUMIDITY
// return value is pointer to byte array containing raw data with function DHT_DATAPTR
{
  static int temperature=-999;
  static int humidity=-999;
  static byte data[5]; // 5 bytes to receive 40 data bits
  unsigned int loopCnt; // loop counter
  byte sum;  // checksum
  #define DHT_LOOPS 1800
  int triggerTime;
  switch (function)
  {
    case DHT11_SAMPLE: // REQUEST DHT11 SAMPLE
    case DHT22_SAMPLE: // REQUEST DHT22 SAMPLE
      if (function==DHT11_SAMPLE) triggerTime=20000; // 20000µs trigger time for DHT11
      else triggerTime=1000; // 1000µs trigger time for DHT22
      pinMode(pin, OUTPUT); 
      digitalWrite(pin, LOW);
      delayMicroseconds(triggerTime); 
      pinMode(pin,INPUT_PULLUP);
      loopCnt = DHT_LOOPS;
      while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      loopCnt = DHT_LOOPS;
      while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      loopCnt = DHT_LOOPS;
      while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      for (byte bitNum=0;bitNum<40;bitNum++) // try reading 40 bits
      {
        loopCnt = DHT_LOOPS;
        while(digitalRead(pin) == LOW) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
        delayMicroseconds(32);
        boolean dhtBit=digitalRead(pin);
        bitWrite(data[bitNum/8],7-bitNum%8,dhtBit);
        loopCnt = DHT_LOOPS;
        while(digitalRead(pin) == HIGH) if (loopCnt-- == 0) return DHT_ERROR_TIMEOUT;
      }
      sum = data[0] + data[1] + data[2] + data[3];  
      if (data[4] != sum) return DHT_ERROR_CRC;
      if (function==DHT11_SAMPLE)
      {
        humidity=data[0];
        temperature=data[2];
      }
      else
      {
        humidity=data[0]*256+data[1];
        temperature= (data[2] & 0x7F) * 256 + data[3];
        if (data[2] & 0x80) temperature= -temperature;
      }
      return DHT_OK;
    case DHT_TEMPERATURE:
      return temperature;
    case DHT_HUMIDITY:  
      return humidity;
    case DHT_DATAPTR:
      return (int)data;
    default:
      return DHT_ERROR_UNKNOWN;
  }  
}

void loop()
{
  byte* b; // byte pointer for showing raw data
  switch (dhtCall(DHT_PIN, DHT_SAMPLE)) // always request a sample first
  {
    case DHT_OK: // only if DHT_OK is true, get temperature, humidity and possibly raw data
      Serial.print("Humidity: ");
      Serial.print(dhtCall(DHT_PIN, DHT_HUMIDITY));
      Serial.print("   Temp: ");
      Serial.print(dhtCall(DHT_PIN, DHT_TEMPERATURE));
      Serial.print("     raw data:  ");
      b=(byte*)dhtCall(DHT_PIN, DHT_DATAPTR);
      for (int i=0;i<5;i++)
      {
        Serial.print(b[i]);Serial.print('\t');
      }
      Serial.println();
      break;
    case DHT_ERROR_TIMEOUT:
      Serial.println("Timeout Error");
      break;
    case DHT_ERROR_CRC:
      Serial.println("CRC Error");
      break;
    default:
      Serial.println("Unknown Error");
  }
  delay(2000); // minimum time between two DHT samples is two seconds
}

Connect:
VCC to VCC
GND to GND
DHT11 data pin to pin-2 of the Arduino UNO.

No pull-up resistor needed, code will use internal pull-up.

Open the serial monitor at 9600 baud. What does the serial monitor show?

The DHT11 lib is pretty old code and I have not tested it with newer IDE's lately.

my reference implementation is now this 0.1.13 version, which is attached to the first post in this thread

Jurs !! you are again my new god... wow :smiley: impressive!

Humidity: 71   Temp: 22     raw data:  71	0	22	0	93	
Humidity: 70   Temp: 22     raw data:  70	0	22	0	92	
Humidity: 70   Temp: 22     raw data:  70	0	22	0	92	
Humidity: 69   Temp: 21     raw data:  69	0	21	0	90

:smiley:

Can i know how? why doesn't works with lib dht and yes with your code?

Thanks man.. I will finally have to pay you ^^

alfcasmo:
why doesn't works with lib dht and yes with your code?

I don't know for sure.

It must be either an issue with the timing and/or the protocol of the libraries. Seems to be dependent on the Arduino IDE software version: Newer Arduino versions and older DHT libraries seem not to work in several cases.

Perhaps I should make a DHT library from my code and offer as an alternative for downloading.
:wink: