Skip to content

Instantly share code, notes, and snippets.

@TrickSumo
Created April 17, 2019 16:35
Show Gist options
  • Save TrickSumo/f1aecd17972aa3b6c0668750c3309714 to your computer and use it in GitHub Desktop.
Save TrickSumo/f1aecd17972aa3b6c0668750c3309714 to your computer and use it in GitHub Desktop.
Interface DHT11 temperature sensor with NodeMCU ESP8266
/* By TrickSumo.com
Libraries used:-
https://github.com/adafruit/DHT-sensor-library
https://github.com/adafruit/DHT-sensor-library
*/
#include <DHT.h>
#define DHTTYPE DHT11
#define dht_dpin D1
DHT dht(dht_dpin, DHTTYPE);
void setup() {
dht.begin();
Serial.begin(9600);
Serial.println("Serial Communication Started\n");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity = ");
Serial.print(h);
Serial.print("\n ");
Serial.print("Temperature = ");
Serial.print(t);
Serial.println("C \n \n \n ");
delay(2000 );
}
@TrickSumo
Copy link
Author

Works fine. Thank you

You're Welcome :)

@yocki24
Copy link

yocki24 commented Mar 15, 2022

Works fine. Thank you

You're Welcome :)

i was try dht11 with arduino uno and thats works fine, but when i try dht11 with esp8266 the output is nan. so where is the problem? and what i must to do for fix this problem?

@TrickSumo
Copy link
Author

Hi @yocki24

Its not working because DHT11 need 5V input.

Either you can use VV pin of NodeMCU ESP8266 or 9V battery with 7805 voltage regulator ic.

https://www.youtube.com/watch?v=pA_Rfop7kDc&list=PLqbNDdqyX9E0H_V-X80L_iFvFtG431Lqd&index=4

Let me know if it helps!

@yocki24
Copy link

yocki24 commented Mar 16, 2022

Hi @yocki24

Its not working because DHT11 need 5V input.

Either you can use VV pin of NodeMCU ESP8266 or 9V battery with 7805 voltage regulator ic.

https://www.youtube.com/watch?v=pA_Rfop7kDc&list=PLqbNDdqyX9E0H_V-X80L_iFvFtG431Lqd&index=4

Let me know if it helps!

i was try it but the output still failed

@TrickSumo
Copy link
Author

Weird!

Tried with battery also? https://www.youtube.com/watch?v=hPv9uX3rUWc&t=3s

Are you using same code mentioned above?

@yocki24
Copy link

yocki24 commented Mar 16, 2022

hmm not yet
i just tried without battery

@TrickSumo
Copy link
Author

oh ok. Try with +5v, it will work.

For without battery, refer this:-

NodeMCU Amica model
https://github.com/TrickSumo/water-level-indicator-controller/blob/master/Circuit%20Diagram%20For%20NodeMCU%20ESP8266%20Amica%20Model.JPG

NodeMCU Lolin model
https://github.com/TrickSumo/water-level-indicator-controller/blob/master/Circuit%20Diagram%20For%20NodeMCU%20ESP8266%20LoLin%20Model.jpg

If your NodeMCU do not have VV or Vin (or sensor not working even with those). Then using battery with voltage regulator is best solution.

@yocki24
Copy link

yocki24 commented Mar 16, 2022

oh ok. thanks about that

@yocki24
Copy link

yocki24 commented Mar 16, 2022

i want ask one question, should i use a resistor for dht11 sensor?

@TrickSumo
Copy link
Author

should i use a resistor for dht11 sensor

If you are using 7805 IC with battery then no need to use resistor.

Else you can use battery and two resistors as voltage divider (or use rheostat).

In any case make sure to not feed digital IC's (including some sensors) more than 5v.

@yocki24
Copy link

yocki24 commented Mar 16, 2022

ok, i was search a scematic design and i found it use resistor in data pin. so should i doing this?

@TrickSumo
Copy link
Author

You can use it.

@yocki24
Copy link

yocki24 commented Mar 16, 2022

oke thank you so much

@TrickSumo
Copy link
Author

Welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment