0-ESP32 ESP32 guide Project Tech

ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE

ESP32 with DHT11 DHT22 Temperature and Humidity Sensor using Arduino IDE

This tutorial exhibits how you can use the DHT11 and DHT22 temperature and humidity sensors with the ESP32 utilizing Arduino IDE. We’ll undergo a fast introduction to those sensors, pinout, wiring diagram, and eventually the Arduino sketch.

Discover ways to show temperature and humidity readings on an internet server utilizing the ESP32 or ESP8266 boards:

DHT11 and DHT22 Temperature and Humidity Sensors

The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are extremely popular amongst makers and electronics hobbyists.

DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE

These sensors include a chip that does analog to digital conversion and spit out a digital sign with the temperature and humidity. This makes them very straightforward to make use of with any microcontroller.

Should you’re wanting to make use of these sensors with the Arduino board, you possibly can learn the next tutorial:

DHT11 vs DHT22

The DHT11 and DHT22 are very comparable, however differ of their specs. The next desk compares a few of the most necessary specs of the DHT11 and DHT22 temperature and humidity sensors. For a extra in-depth evaluation of those sensors, please examine the sensors’ datasheet.

DHT11 DHT22
Temperature vary Zero to 50 ºC +/-2 ºC -40 to 80 ºC +/-Zero.5ºC
Humidity vary 20 to 90% +/-5% Zero to 100% +/-2%
Decision Humidity: 1%
Temperature: 1ºC
Humidity: Zero.1%
Temperature: Zero.1ºC
Working voltage three – 5.5 V DC three – 6 V DC
Present provide Zero.5 – 2.5 mA 1 – 1.5 mA
Sampling interval 1 second 2 seconds
Worth $1 to $5 $four to $10
The place to purchase Verify costs Verify costs

The DHT22 sensor has a greater decision and a wider temperature and humidity measurement vary. Nevertheless, it is a little more costly, and you may solely request readings with 2 seconds interval.

The DHT11 has a smaller vary and it’s much less correct. Nevertheless, you possibly can request sensor readings each second. It’s additionally a bit cheaper.

Regardless of their variations, they work in an identical approach, and you need to use the identical code to learn temperature and humidity. You simply want to pick within the code the sensor sort you’re utilizing.

DHT Pinout

DHT sensors have 4 pins as proven within the following determine. Nevertheless, in the event you get your DHT sensor in a breakout board, it comes with solely three pins and with an inner pull-up resistor on pin 2.

DHT22 Temperature and Humidity Sensor using Arduino IDE

The next desk exhibits the DHT22/DHT11 pinout. When the sensor is dealing with you, pin numbering begins at 1 from left to proper

DHT pin Hook up with
1 three.3V
2 Any digital GPIO; additionally join a 10okay Ohm pull-up resistor
three Don’t join
four GND

Elements Required

To comply with this tutorial it’s essential wire the DHT11 or DHT22 temperature sensor to the ESP32. You have to use a 10okay Ohm pull-up resistor.

Right here’s an inventory of elements you could construct the circuit:

You need to use the previous hyperlinks or go on to MakerAdvisor.com/instruments to seek out all of the elements on your tasks at one of the best worth!

Schematic Diagram

Wire the DHT22 or DHT11 sensor to the ESP32 improvement board as proven within the following schematic diagram.

ESP32 with DHT11/DHT22 Temperature and Humidity Sensor using Arduino IDE

On this instance, we’re connecting the DHT knowledge pin to GPIO four. Nevertheless, you need to use some other appropriate digital pin.

Discover ways to use the ESP32 GPIOs with our information: ESP32 Pinout Reference: Which GPIO pins do you have to use?

Putting in Libraries

To learn from the DHT sensor, we’ll use the DHT library from Adafruit. To make use of this library you additionally want to put in the Adafruit Unified Sensor library. Comply with the subsequent steps to put in these libraries.

Open your Arduino IDE and go to Sketch > Embrace Library > Handle Libraries. The Library Supervisor ought to open.

Seek for “DHT” on the Search field and set up the DHT library from Adafruit.

Installing Adafruit DHT library

After putting in the DHT library from Adafruit, sort “Adafruit Unified Sensor” within the search field. Scroll all the best way down to seek out the library and set up it.

Installing Adafruit Unified Sensor driver library

After putting in the libraries, restart your Arduino IDE.

ESP32 Studying Temperature and Humidity Sketch

To learn temperature and humidity from the DHT sensor, we’ll use an instance based mostly on the Adafruit DHT library. Copy the next code to your Arduino IDE.

// Instance testing sketch for numerous DHT humidity/temperature sensors written by ladyada
// REQUIRES the next Arduino libraries:
// – DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// – Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#embrace “DHT.h”

#outline DHTPIN four // Digital pin related to the DHT sensor
// Feather HUZZAH ESP8266 word: use pins three, four, 5, 12, 13 or 14 —
// Pin 15 can work however DHT have to be disconnected throughout program add.

// Uncomment no matter sort you are utilizing!
//#outline DHTTYPE DHT11 // DHT 11
#outline DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#outline DHTTYPE DHT21 // DHT 21 (AM2301)

// Join pin 1 (on the left) of the sensor to +5V
// NOTE: If utilizing a board with three.3V logic like an Arduino Due join pin 1
// to three.3V as an alternative of 5V!
// Join pin 2 of the sensor to no matter your DHTPIN is
// Join pin four (on the fitting) of the sensor to GROUND
// Join a 10Okay resistor from pin 2 (knowledge) to pin 1 (energy) of the sensor

// Initialize DHT sensor.
// Notice that older variations of this library took an non-compulsory third parameter to
// tweak the timings for quicker processors. This parameter is not wanted
// as the present DHT studying algorithm adjusts itself to work on quicker procs.
DHT dht(DHTPIN, DHTTYPE);

void setup()
Serial.start(9600);
Serial.println(F(“DHTxx check!”));

dht.start();

void loop()
// Wait a couple of seconds between measurements.
delay(2000);

// Studying temperature or humidity takes about 250 milliseconds!
// Sensor readings can also be as much as 2 seconds ‘previous’ (its a really sluggish sensor)
float h = dht.readHumidity();
// Learn temperature as Celsius (the default)
float t = dht.readTemperature();
// Learn temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Verify if any reads failed and exit early (to attempt once more).
if (isnan(h) || isnan(t) || isnan(f))
Serial.println(F(“Did not learn from DHT sensor!”));
return;

// Compute warmth index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute warmth index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Serial.print(F(“Humidity: “));
Serial.print(h);
Serial.print(F(“% Temperature: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(f);
Serial.print(F(“°F Warmth index: “));
Serial.print(hic);
Serial.print(F(“°C “));
Serial.print(hif);
Serial.println(F(“°F”));

View uncooked code

There are various feedback all through the code with helpful info. So, you may want to try the feedback. Proceed studying to find out how the code works.

How the Code Works

First, you want to import the DHT library:

#embrace “DHT.h”

Then, outline the digital pin that the DHT sensor knowledge pin is related to. On this case, it’s related to GPIO four.

#outline DHTPIN four // Digital pin related to the DHT sensor

Then, it is advisable to choose the DHT sensor sort you’re utilizing. The library helps DHT11, DHT22, and DHT21. Uncomment the sensor sort you’re utilizing and remark all of the others. On this case, we’re utilizing the DHT22 sensor.

//#outline DHTTYPE DHT11 // DHT 11
#outline DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#outline DHTTYPE DHT21 // DHT 21 (AM2301)

Create a DHT object referred to as dht on the pin and with the sensor sort you’ve specified beforehand.

DHT dht(DHTPIN, DHTTYPE);

Within the setup(), initialize the Serial debugging at a baud fee of 9600, and print a message within the Serial Monitor.

Serial.start(9600);
Serial.println(F(“DHTxx check!”));

Lastly, initialize the DHT sensor.

dht.start();

The loop() begins with a 2000 ms (2 seconds) delay, as a result of the DHT22 most sampling interval is 2 seconds. So, we will solely get readings each two seconds.

delay(2000);

The temperature and humidity are returned in float format. We create float variables h, t, and f to save lots of the humidity, temperature in Celsius and temperature in Fahrenheit, respectively.

Getting the humidity and temperature is as straightforward as utilizing the readHumidity() and readTemperature() strategies on the dht object, as proven under:

float h = dht.readHumidity();
// Learn temperature as Celsius (the default)
float t = dht.readTemperature();

If you wish to get the temperature in Fahrenheit levels, you might want to move the true parameter as argument to the readTemperature() technique.

float f = dht.readTemperature(true);

There’s additionally an if assertion that checks if the sensor returned legitimate temperature and humidity readings.

if (isnan(h) || isnan(t) || isnan(f))
Serial.println(F(“Did not learn from DHT sensor!”));
return;

After getting the humidity and temperature, the library has a way that computes the warmth index. You will get the warmth index each in Celsius and Fahrenheit as proven under:

// Compute warmth index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute warmth index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);

Lastly, print all of the readings on the Serial Monitor with the next instructions:

Serial.print(F(“Humidity: “));
Serial.print(h);
Serial.print(F(“% Temperature: “));
Serial.print(t);
Serial.print(F(“°C “));
Serial.print(f);
Serial.print(F(“°F Warmth index: “));
Serial.print(hic);
Serial.print(F(“°C “));
Serial.print(hif);
Serial.println(F(“°F”));

Demonstration

Add the code to your ESP32 board. Be sure to have the appropriate board and COM port chosen in your Arduino IDE settings.

After importing the code, open the Serial Monitor at a baud fee of 9600. You need to get the newest temperature and humidity readings within the Serial Monitor each two seconds.

ESP32 DHT11 DHT22 AM2302 AM2301 read temperature humidity sensor

Troubleshooting – Did not learn from DHT sensor

When you’re making an attempt to learn the temperature and humidity from the DHT11/DHT22 sensor and also you get an error message in your Serial Monitor, comply with the subsequent steps to see if you can also make your sensor work (or learn our devoted DHT Troubleshooting Information).

“Did not learn from DHT sensor!” or Nan readings

In case your DHT sensor returns the error message “Did not learn from DHT sensor!” or the DHT readings return “Nan”:

Solved Troubleshooting DHT11 DHT22 AM2302 AM2301 Failed to read from DHT sensor! or Nan

Attempt one of many following troubleshooting ideas:

  • Wiring: whenever you’re constructing an electronics challenge, you must double-check the wiring or pin task. After checking and testing that your circuit is correctly related, if it nonetheless doesn’t work, proceed studying the subsequent troubleshooting ideas.
  • Energy: the DHT sensor has an working vary of 3V to five.5V (DHT11) or 3V to 6V (DHT22). In the event you’re powering the sensor from the ESP32 three.3V pin, in some instances powering the DHT with 5V solves the issue.
  • Dangerous USB port or USB cable: typically powering the ESP32 immediately from a PC USB port shouldn’t be sufficient. Attempt to plug it to a USB hub powered by an exterior energy supply. It may additionally assist changing the USB cable with a greater or shorter one. Having a USB port that provides sufficient energy or utilizing a great USB cable typically fixes this drawback.
  • Energy supply: as talked about within the earlier tip, your ESP won’t be supplying sufficient energy to correctly learn from the DHT sensor. In some instances, you may have to energy the ESP with an influence supply that gives extra present.
  • Sensor sort: double-check that you simply’ve uncommented/commented in your code the fitting sensor on your challenge. On this venture, we have been utilizing the DHT22:

//#outline DHTTYPE DHT11 // DHT 11
#outline DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#outline DHTTYPE DHT21 // DHT 21 (AM2301)

  • Sampling price: the DHT sensor could be very sluggish getting the readings (the sensor readings might take as much as 2 seconds). In some instances, growing the time between readings solves the issue.
  • DHT sensor is fried or damaged: sadly, these low cost sensors typically look completely nice, however they’re fried/damaged. So, regardless that you assembled the proper circuit and code, it’ll nonetheless fail to get the readings. Attempt to use a special sensor to see if it fixes your drawback.
  • Mistaken baud price or did not add code: in the event you don’t see something in your Arduino IDE Serial Monitor double-check that you simply’ve chosen the correct baud fee, COM port or that you simply’ve uploaded the code efficiently.

Whereas constructing our tasks, we’ve skilled comparable points with the DHT and it was all the time solved by following one of many strategies described earlier.

Deadly error: Adafruit_Sensor.h: No such file or listing

There’s additionally a standard error that occurs whenever you attempt to compile the code. In the event you obtain the next error:

deadly error: Adafruit_Sensor.h: No such file or listing
#embrace

You should set up the Adafruit Unified Sensor driver library. In your Arduino IDE, sort within the search field “Adafruit Unified Sensor“, scroll all the best way down to seek out the library and set up it.

Installing Adafruit Unified Sensor driver library

After putting in the library, restart your Arduino IDE and the code ought to compile with out the error message.

Wrapping Up

With this tutorial you’ve discovered the right way to get temperature and humidity readings from a DHT11 or DHT22 sensor utilizing the ESP32 with Arduino IDE. Getting temperature and humidity readings with the Adafruit DHT library could be very easy, you simply use the readTemperature() and readHumidity() strategies on a DHT object.

Now, you’ll be able to take this undertaking to the subsequent degree and show your sensor readings in an internet server which you could seek the advice of utilizing your smartphone’s browser. Discover ways to construct an internet server with the ESP32 to show your sensor readings: ESP32 DHT11/DHT22 Net Server – Temperature and Humidity utilizing Arduino IDE.

When you like ESP32, you might also like the next assets:

We hope you’ve discovered this information helpful.

Thanks for studying.