Course Content
📘 MODULE 6 – Smart Pet Feeder
0/1
📘 MODULE 7 – Smart Water Management System
0/2
📘 MODULE 8 – Water Quality Monitoring System
0/2
📘 MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

✅ ESP32 + DHT11 Serial Monitor Code (Copy–Paste)

 

#include "DHT.h"
// -------------------- DHT Pin & Type --------------------

#define DHTPIN 4
// ESP32 GPIO4 (D4 pin)
#define DHTTYPE DHT11
// DHT11 sensor type
// Create DHT object
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
// Start Serial Communication
Serial.begin(115200);
// Print a message once when ESP32 starts
Serial.println("DHT11 Sensor Reading...");
// Start DHT sensor
dht.begin();
}
void loop()
{
// DHT11 sensor needs minimum 2 seconds delay between readings
delay(2000);
// Read Humidity (%)
float humidity = dht.readHumidity();
// Read Temperature (°C)
float temperature = dht.readTemperature();
// If sensor reading fails, humidity/temperature becomes NAN
if (isnan(humidity) || isnan(temperature))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
//
Print Temperature
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | ");
// Print Humidity
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}

📌 Wiring (DHT11 3-pin module → ESP32)

DHT11 Pin ESP32
VCC 3.3V
GND GND
DATA GPIO4 (D4)

🧠 Detailed Code Explanation (Line by Line)


✅ 1) Library Include

 
#include "DHT.h"

This line adds the DHT sensor library.

Without this library, ESP32 cannot understand:

  • readHumidity()

  • readTemperature()

  • begin()


✅ 2) Define Pin and Sensor Type

 
#define DHTPIN 4
#define DHTTYPE DHT11

🔹 DHTPIN 4

This means the DHT11 data pin is connected to GPIO 4 of ESP32.

🔹 DHTTYPE DHT11

This tells the library which sensor you are using:

  • DHT11 (low cost, low accuracy)

  • DHT22 (better accuracy)


✅ 3) Create DHT Sensor Object

 
DHT dht(DHTPIN, DHTTYPE);

This creates an object named dht.

Now you can use:

  • dht.begin()

  • dht.readHumidity()

  • dht.readTemperature()


✅ 4) Setup Function

 
void setup() {

This runs only one time when ESP32 starts.


🔹 Start Serial Communication

 
Serial.begin(115200);

This starts serial communication between:

  • ESP32 ↔ Laptop/PC

Speed = 115200 baud.


🔹 Print message

 
Serial.println("DHT11 Sensor Reading...");

This prints a message once, to confirm code started.


🔹 Start DHT sensor

 
dht.begin();

This initializes the sensor.

If you don’t call this, DHT11 will not work.


✅ 5) Loop Function

 
void loop() {

This runs again and again forever.


🔹 Delay 2 seconds

 
delay(2000);

⚠️ DHT11 needs minimum 2 seconds between readings.

If you read too fast:

  • sensor gives wrong values

  • or returns NAN


🔹 Read humidity

 
float humidity = dht.readHumidity();

This reads humidity and stores it in a float variable.

Example:

  • 60.00 %


🔹 Read temperature

 
float temperature = dht.readTemperature();

This reads temperature in Celsius.

Example:

  • 28.00 °C


✅ 6) Check for Error

 
if (isnan(humidity) || isnan(temperature)) {

If sensor is not connected properly, or sensor fails, the library returns:

❌ NAN = Not A Number

So we check it.


🔹 Print error

 
Serial.println("Failed to read from DHT sensor!");
return;

This prints the error and stops this loop cycle.


✅ 7) Print Output on Serial Monitor

 
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

This prints values like:

 
Temperature: 28.00 °C | Humidity: 64.00 %

🖥 Serial Monitor Settings

In Arduino IDE:

✅ Tools → Serial Monitor

  • Baud rate: 115200


✅ Output Example

 
DHT11 Sensor Reading...
T
emperature: 28.00 °C | Humidity: 65.00 %
Temperature: 28.00 °C | Humidity: 64.00 %
Scroll to Top