Back to Tutorial

ESP32 with a DHT11 sensor module

ESP32 with a DHT11 sensor module:

Hardware Setup:

  1. Gather the necessary components:
  2. Connect the DHT11 sensor to the ESP32:
    • Connect the VCC pin of the sensor to the 3.3V or 5V output of the ESP32.
    • Connect the GND pin of the sensor to the ground pin of the ESP32.
    • Connect the data pin of the sensor (usually labeled “D”) to a digital input pin on the ESP32 (e.g., GPIO13).

Software Setup:

  1. Install the Arduino IDE: Download and install the Arduino IDE from https://support.arduino.cc/hc/en-us/articles/360019833020-Download-and-install-Arduino-IDE.  
  2. Install the ESP32 board support package: Open the Arduino IDE, go to File > Preferences, and add the following URL to the Additional Boards Manager URLs field: https://dl.espressif.com/package/esp32/index.json. Then, go to Tools > Board > Boards Manager and install the “ESP32” board.  
  3. Install the DHT11 library: Search for “DHT11 from adafruit” in the Library Manager and install it.

Code:

C++

#include <DHT.h>

#define DHTTYPE DHT11

#define DHTPIN 18 //change pin as per your board 

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  dht.begin();
}

void loop() {
  // Wait for sensor reading
  delay(1000);

  // Read temperature and humidity
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check for errors
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Print the values
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print("    %  Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
}

Explanation:

  • The code includes the necessary DHT11 library.
  • The DHTTYPE and DHTPIN constants define the DHT11 sensor type and the GPIO pin connected to it.
  • In the setup() function, the DHT11 sensor is initialized.
  • In the loop() function, the readHumidity() and readTemperature() functions are used to read the humidity and temperature values from the sensor.
  • The isnan() function is used to check for errors in the sensor readings.
  • The values are then printed to the serial monitor.

Uploading the Code:

  1. Connect the ESP32 to your computer.
  2. Select the correct board and port in the Arduino IDE.
  3. Compile and upload the code to the ESP32.

Testing:

  1. Open the serial monitor in the Arduino IDE.
  2. Observe the humidity and temperature values being printed.

Additional Notes:

  • The DHT11 sensor has a relatively slow reading rate. If you need faster readings, consider using a DHT22 or DHT21 sensor.
  • You can customize the code to perform different actions based on the sensor readings (e.g., triggering alarms, controlling devices).
  • Consider using a library or framework like ThingSpeak or Blynk to visualize and analyze the sensor data.

By following these steps, you can effectively use an ESP32 to measure temperature and humidity using a DHT11 sensor.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial

You were not leaving your cart just like that, right?

Enter your details below to save your shopping cart for later. And, who knows, maybe we will even send you a sweet discount code :)