How to use a ESP32 development board to read temperature and humidity from a DH11 sensor
When you have a DHT11 sensor, you will be able to get the temperature and humidity of your environment.
Given that, this post shows how to use ESP32 development board to read temperature and humidity from a DHT11 sensor.
Wiring your DHT11 sensor to your ESP32 development board
When you look at your DHT11 sensor, you can see three pins labeled +/VCC, OUT/S/DATA and -/GND. Connect the:
- +/VCC pin to a 3v3 pin on your board
- OUT/s/DATA pin to GPIO27 on your board
- -/GND pin to a GND pin on your board
For example, this is how I had wired my DHT11 sensor to my ESP32 development board:
Enabling ESP32 Development on Arduino IDE
At this point in time, you are ready to flash a program into your ESP32 board to read from the DHT11 sensor. In order to do so, we need to use a tool to write our program into the flash memory of the development board.
Since it is easy to write code and flash programs with Arduino IDE, we can use it to serve our purpose.
In order to use Arduino IDE for this guide, be sure to enable ESP32 development on Arduino IDE before continuing.
Installing the DHT sensor library for ESPx to read temperature and humidity from DHT11 sensor
After you had started your Arduino IDE, proceed to install a Arduino Library to read temperature and humidity from DHT11 sensor.
In order to do so, first go to Tools -> Manage Libraries.... After you had done so, the Library Manager window will appear. Search for DHT11 and install the DHT sensor library for ESPx by beegee_tokyo:
Arduino sketch example to read temperature and humidity from the DHT11 sensor
Once you had installed the DHT sensor library for ESPx, upload the following sketch to your ESP32 board:
#include "DHTesp.h" DHTesp dht; void setup() { Serial.begin(9600); dht.setup(27, DHTesp::DHT11); Serial.println(); delay(1000); } void loop() { float temperature = dht.getTemperature(); float humidity = dht.getHumidity(); Serial.print("Temperature: "); Serial.println(temperature); Serial.print("Humidity: "); Serial.println(humidity); delay(2000); }
After your Arduino IDE had uploaded the above sketch to your ESP32 board, you should see output similar to the following: