Go Down

Topic: DHT11 Sensor (Read 9911 times) previous topic - next topic

lalalalyra

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! (:

el_supremo

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

Pete

lalalalyra

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.

RIDDICK

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

what is the first error message?
-Arne

lalalalyra

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

lalalalyra

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!!!

robtillaart

Think you need to read about libraries again (and carefully ;) - 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 Tillaart

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

lalalalyra

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

Bajdi


Think you need to read about libraries again (and carefully ;) - 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 :)
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.
www.bajdi.com

MarkT

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).
[ I won't respond to messages, use the forum please ]

robtillaart

#10
Dec 31, 2011, 04:47 pm Last Edit: Dec 31, 2011, 05:33 pm by robtillaart Reason: 1
@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)
Rob Tillaart

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

Flash_Mechy

First, I would like to apologize in advance for bringing up a super old thread, but this was frustrating me to no end.

I just dealt with this same problem for the past few hours and finally figured out what the issue was. I was copying and pasting the library files off of a webpage, and then trying to save them from within a sketch, so every time I saved my ".h" and ".cpp" files, I was actually saving them as ".h.ino" and ".cpp.ino" files, which will of course not be recognized during compile. I rectified this by copying the library code into a text document, then "Save As..." and changing the file type to be "All Files" and they saved as the correct file types. Stick those two files into a named folder in your Arduino Library file, and you should be good to go. This took my far too long to figure out; I am somewhat ashamed.

Hope this isn't way too late and it helps somebody! All the best

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy