Pages: [1]   Go Down
Author Topic: DHT11 Sensor  (Read 6377 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Help! Does anyone have/know a DHT11 code? I tried using the one from ladyada.net. This is the tester code. It looks like this

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 2     // what pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");
 
  dht.begin();
}

void loop() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
  }
}

I'm not sure what's wrong. It says that DHT does not name a type. Im not sure what I'm doing wrong. if someone could tell me, or give me a code for the DHT11, I'd really appreciate it.

Thanks! (:
Logged

Offline Offline
Edison Member
*
Karma: 49
Posts: 1670
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you download and install the DHT library in the correct place?

Pete
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I did. I have a MacBook Air, but the instructions I usually see online are for PCs. I think I downloaded it to the right spot. I'm able to open the tester code in the menu bar of the Arduino program.
Logged

Brunsbüttel, SH, F.Rep.GERM
Offline Offline
God Member
*****
Karma: 4
Posts: 596
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

what if u type
#include <DHT.h>
instead of
#include "DHT.h" ?

what is the first error message?
Logged

-Arne

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. I'm not on my programming computer, but I'll try that once I get on (:
Hopefully it'll work...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, I tried changing the code to <DHT.h> & it didn't change. The error says:

DHTtester.cpp:4:17: error: DHT.h: No such file or directory
DHTtester.pde:-1: error: 'DHT' does not name a type
DHTtester.cpp: In function 'void setup()':
DHTtester.pde:-1: error: 'dht' was not declared in this scope
DHTtester.cpp: In function 'void loop()':
DHTtester.pde:-1: error: 'dht' was not declared in this scope

and in the orange bar it says:

'DHT' does not name a type.

I assumed this code would be legit since it came from ladyada.net

Heeellppp!!!
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13876
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Think you need to read about libraries again (and carefully smiley-wink - http://www.arduino.cc/en/Reference/Libraries -

If that does not help you may try - http://arduino.cc/playground/Main/DHTLib - tested for both DHT11 and DHT22  (with Arduino 0.22)
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The first link didn't help too much. Thanks though. I really appreciate it (:
Logged

Belgium
Offline Offline
Sr. Member
****
Karma: 1
Posts: 283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Think you need to read about libraries again (and carefully smiley-wink - http://www.arduino.cc/en/Reference/Libraries -

If that does not help you may try - http://arduino.cc/playground/Main/DHTLib - tested for both DHT11 and DHT22  (with Arduino 0.22)

Rob thanks for the library smiley
I've tried it in arduino 1.0 and I just had to change Wprogram.h to Arduino.h in dht.h and it worked.
Logged


0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12467
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Incidentally the < > syntax is for refering to libraries, the " " syntax for local .h files (but most compilers then check library directories too if not found locally).
Logged

[ I won't respond to messages, use the forum please ]

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13876
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@E40Racer

Quote
I've tried it in arduino 1.0 and I just had to change Wprogram.h to Arduino.h in dht.h and it worked.
Think I need to add a small note in the article / patch the code. Thanks for the tip.

(done)
« Last Edit: December 31, 2011, 11:33:51 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: