Hi,
I was trying to print Temperature and Humidity to an lcdscreen. I connected everything and after I
Uploaded the Sketch my Sensor is only showing nan.
I googled it but I coudn´t find an answer. My sensor was in a Box for a while. Then came the issue.
Is my Sensor damaged?
Or is it just a Programm issue?
Signalpin from DHT11 to Pin D2
Here is my Sketch:
#include "DHT.h" //DHT Bibliothek laden
#include <LiquidCrystal.h>
#define DHTPIN 2 //Der Sensor wird an PIN 2 angeschlossen
#define DHTTYPE DHT11 // Es handelt sich um den DHT11 Sensor
DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth“ angesprochen
LiquidCrystal lcd (12, 11, 9, 8, 7, 6);
void setup() {
Serial.begin(9600); //Serielle Verbindung starten
lcd.begin(16, 2);
dht.begin(); //DHT11 Sensor starten
}
void loop() {
delay(5000); //Zwei Sekunden Vorlaufzeit bis zur Messung (der Sensor ist etwas träge)
float Luftfeuchtigkeit = dht.readHumidity(); //die Luftfeuchtigkeit auslesen und unter „Luftfeutchtigkeit“ speichern
float Temperatur = dht.readTemperature();//die Temperatur auslesen und unter „Temperatur“ speichern
Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags: [code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
Post a link to where you got the DHT library from. Please use the chain links icon on the toolbar to make it clickable. Or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.
the library seems to test for a connection, if not connected, it results in nan
line 72
float DHT::readHumidity(bool force) {
float f = NAN;
if (read()) {
switch (_type) {
case DHT11:
f = data[0];
break;
case DHT22:
case DHT21:
f = data[0];
f *= 256;
f += data[1];
f *= 0.1;
break;
}
}
return f;
check your wiring.
check voltage at the sensor.
maybe try a different pin
PaulRB:
Did you use a pull-up resistor on the data pin? How long is your cable?
Try, add 4.7k to 10k, see what happens
Also, there are two example sketches in GitHub. One has the reporting to notify that the unit is not found.
you might want to use the example sketch and see what happens.
the other example requires a second library be in the sketch.
there are two packages with the device on the board.
one has a mounting hole between the pins and the blue enclosure.
this has signal on the middle pin.
then there is the Keyes version with the pins very close to the enclosure and there is a resistor on the board.
signal is marked with an S and is on the left.
I have a raw DHT11, not on a board, it has 4 pins.
I used the internal pull-up for pin 2
I remarked out all the LCD stuff.
Since your code worked for me after I remarked out the LCD bits, I suggest you try it and see if it works for you.
#include "DHT.h" //DHT Bibliothek laden
//#include <LiquidCrystal.h>
#define DHTPIN 2//Der Sensor wird an PIN 2 angeschlossen
#define DHTTYPE DHT11 // Es handelt sich um den DHT11 Sensor
DHT dht(DHTPIN, DHTTYPE); //Der Sensor wird ab jetzt mit „dth" angesprochen
//LiquidCrystal lcd (12, 11, 9, 8, 7, 6);
void setup() {
Serial.begin(9600); //Serielle Verbindung starten
// lcd.begin(16, 2);
pinMode(2, INPUT_PULLUP);
dht.begin(); //DHT11 Sensor starten
}
void loop() {
delay(5000); //Zwei Sekunden Vorlaufzeit bis zur Messung (der Sensor ist etwas träge)
float Luftfeuchtigkeit = dht.readHumidity(); //die Luftfeuchtigkeit auslesen und unter „Luftfeutchtigkeit" speichern
float Temperatur = dht.readTemperature();//die Temperatur auslesen und unter „Temperatur" speichern
//lcd.print(Luftfeuchtigkeit); //die Dazugehörigen Werte anzeigen
//lcd.setCursor(0, 1);
//lcd.print(Temperatur);
Serial.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und
Serial.print(Luftfeuchtigkeit); //die Dazugehörigen Werte anzeigen
Serial.println(" %");
Serial.print("Temperatur: ");
Serial.print(Temperatur);
Serial.println(" Grad Celsius");
}