# DHT11 temperature and humidity sensor

**URL:** <https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502>\
**Category:** Programming\
**Created:** [May 28, 2020, 8:22am UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502 "2020-05-28T08:22:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 8:22am UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/1 "2020-05-28T08:22:36Z")

</div>

Hi  
I am very new to arduino and am trying to use the DHT11 sensor and library but i keep getting error messages that i cant find a solution for  
#include \<DHT.h\>  
#include \<DHT\_U.h\>

#define dht\_apin A0;

void setup() {  
// put your setup code here, to run once:  
Serial.begin(9600);  
delay(500);  
Serial.print("DHT11 Humidity and Temperature sensor\n\n");  
delay(1000);  
}

void loop() {  
// put your main code here, to run repeatedly:  
DHT.read11(dht\_apin);  
Serial.print("Current Humidity");  
Serial.print(DHT.humidity);  
delay(1000);  
Serial.print("Current Temperature");  
Serial.print(DHT.temperature);  
delay(5000);  
}  
here is my code, it is probably attached incorrectly but i dont know how to attach it. Everything is probably wrong but it would be great is i could get started learning with some help

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 28, 2020, 8:34am UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/2 "2020-05-28T08:34:59Z")

</div>

And the errors you are seeing are . . ?

Please remember to use code tags when posting code

`#define dht_apin A0;`Lose the semicolon

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 8:39am UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/3 "2020-05-28T08:39:13Z")

</div>

Try this,

```cpp
#include <DHT.h>
//#include <DHT_U.h>
#define dht_pin A0
#define DHTTYPE DHT11 

float Temperature;
float Humidity;

DHT dht(dht_pin, DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
  Serial.print("DHT11 Humidity and Temperature sensor\n\n");
  delay(1000);
}

void loop() {
  // put your main code here, to run repeatedly:
  Temperature = dht.readTemperature(true); //true = fahrenheit
  Humidity = dht.readHumidity();
  Serial.print("Current Humidity: ");
  Serial.println(Humidity);
  delay(1000);
  Serial.print("Current Temperature: ");
  Serial.println(Temperature);
  delay(5000);
}

```

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 11:46am UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/4 "2020-05-28T11:46:29Z")

</div>

Thanks  
the code is much better than mine however in serial monitor it says temperature and humidity are nan

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 12:01pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/5 "2020-05-28T12:01:59Z")

</div>

> endermaker:  
> Thanks  
> the code is much better than mine however in serial monitor it says temperature and humidity are nan

Check your wires, make sure your using the pullup resistor

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 12:02pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/6 "2020-05-28T12:02:57Z")

</div>

How do i use pullup resistor?

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 12:05pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/7 "2020-05-28T12:05:33Z")

</div>

> endermaker:  
> How do i use pullup resistor?

Have a look at this for reference

![unnamed.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/9/0/990717fed6298d02a0ddd3155557dec014c5c4b2.png)

![unnamed.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/9/0/990717fed6298d02a0ddd3155557dec014c5c4b2.png)

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 12:12pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/8 "2020-05-28T12:12:25Z")

</div>

Still no luck

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 12:14pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/9 "2020-05-28T12:14:31Z")

</div>

Sorry i made a mistake, i forgot a part in setup. try this,

```cpp
#include <DHT.h>
//#include <DHT_U.h>
#define dht_pin A0
#define DHTTYPE DHT11

float Temperature;
float Humidity;

DHT dht(dht_pin, DHTTYPE);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
  Serial.print("DHT11 Humidity and Temperature sensor\n\n");
  delay(1000);
  pinMode(dht_pin , INPUT);
  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Temperature = dht.readTemperature(true); //true = fahrenheit
  Humidity = dht.readHumidity();
  Serial.print("Current Humidity: ");
  Serial.println(Humidity);
  delay(1000);
  Serial.print("Current Temperature: ");
  Serial.println(Temperature);
  delay(5000);
}

```

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 12:20pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/10 "2020-05-28T12:20:16Z")

</div>

Still not working

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 12:25pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/11 "2020-05-28T12:25:48Z")

</div>

> endermaker:  
> Still not working

Hmm im not sure. i use that code for dht22 i have never used a dht11 tho i think it should work. What value resistor are you using can you show your wiring?

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 12:28pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/12 "2020-05-28T12:28:57Z")

</div>

10k resistor

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 12:30pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/13 "2020-05-28T12:30:26Z")

</div>

Try changing A0 to 14? assuming your using an arduino.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 28, 2020, 12:35pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/14 "2020-05-28T12:35:55Z")

</div>

The value of A0 _is_ 14 on the Uno

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 12:40pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/15 "2020-05-28T12:40:16Z")

</div>

it still doesnt work

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 1:08pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/16 "2020-05-28T13:08:05Z")

</div>

What does nan stand for?

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [May 28, 2020, 1:09pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/17 "2020-05-28T13:09:59Z")

</div>

Not a Number

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 1:15pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/18 "2020-05-28T13:15:55Z")

</div>

Changing the 2 float values to integers make them both read as 0

---

<div class="post-metadata">

**Author:** ![notsolowki](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/notsolowki/32/1101411_2.png) [@notsolowki](https://forum.arduino.cc/u/notsolowki)\
**Post date:** [May 28, 2020, 1:16pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/19 "2020-05-28T13:16:37Z")

</div>

Usually NaN is problem with wires or sensor.. if you change the floats to int you will lose the decimal

---

<div class="post-metadata">

**Author:** ![endermaker](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@endermaker](https://forum.arduino.cc/u/endermaker)\
**Post date:** [May 28, 2020, 1:23pm UTC](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502/20 "2020-05-28T13:23:08Z")

</div>

What can I do about this?

[Next page](https://forum.arduino.cc/t/dht11-temperature-and-humidity-sensor/657502.md?page=2)
