Lesson 11: ESP32 with DHT22 Sensor | LED & Buzzer

 Learning Objectives

By the end of this lesson, students will be able to:

  • Use Wokwi simulator to virtually prototype ESP32 projects.

  • Correctly wire a DHT22 sensor, LED, and buzzer to the ESP32.

  • Apply resistors properly in series with LED anode.

  • Set and test alert thresholds for temperature and humidity.

  • Trigger outputs (LED and buzzer) when environmental conditions are exceeded.


Key Concepts

  1. Virtual Prototyping

    • Why we use simulators before real hardware (saves cost, safe for beginners, quick testing).

    • Benefits of Wokwi for IoT and Arduino learning.

  2. DHT22 Sensor

    • What it measures: temperature and humidity.

    • Connections: VCC → 5V, GND → GND, DATA → GPIO pin (e.g., 25).

  3. LED with Resistor

    • LED as a visual alert.

    • Wiring: GPIO pin (e.g., 32) → Resistor → LED Anode (+); LED Cathode (–) → GND.

    • Why the resistor is important (limits current, protects LED).

  4. Buzzer as an Actuator

    • Produces sound when triggered.

    • Connections: GPIO pin (e.g., 27) → Buzzer +; Buzzer – → GND.

  5. Programming Logic

    • Read temperature and humidity values from DHT22.

    • If humidity > 70% or temperature > 30°C → turn ON LED + buzzer.

    • Else → keep them OFF. 



Hands-On Activity in Wokwi

  1. Open https://wokwi.com.

  2. Create a new ESP32 project.

  3. Add DHT22, LED, resistor, and buzzer from the part library.

  4. Wire components as described.

  5. Copy the provided code into the editor.

  6. Click ▶ Play to run the simulation.

  7. Adjust sensor values in Wokwi (DHT22 properties) to test different conditions. 


Code Example

#include <Arduino.h>
#include "DHTesp.h"

// --- Pins ---
#define LED_PIN     32       // LED through 220 Ω to GND
#define BUZZER_PIN  27       // Use a PWM-capable pin (25/26/27 are safe)
#define DHT_PIN     25       // DHT22 DATA

// --- Alert thresholds ---
const float TEMP_HIGH_C = 30.0;   // Trigger when temperature > 30°C
const float HUM_HIGH_PCT = 70.0;  // Trigger when humidity > 70%

DHTesp dht;

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  dht.setup(DHT_PIN, DHTesp::DHT22);
}

void loop() {
  TempAndHumidity data = dht.getTempAndHumidity();
  float t = data.temperature;
  float h = data.humidity;

  if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT22");
    delay(1500);
    return;
  }

  // Print values to Serial Monitor
  Serial.print("Temp: "); Serial.print(t); Serial.print(" °C   ");
  Serial.print("Humidity: "); Serial.print(h); Serial.println(" %");

  // --- Alert logic ---
  if (t > TEMP_HIGH_C || h > HUM_HIGH_PCT) {
    digitalWrite(LED_PIN, HIGH);   // Turn LED ON
    tone(BUZZER_PIN, 1800);        // Turn buzzer ON (steady tone)
  } else {
    digitalWrite(LED_PIN, LOW);    // Turn LED OFF
    noTone(BUZZER_PIN);            // Turn buzzer OFF
  }

  delay(2000); // Sample every 2 seconds
}


Wrap-Up

  • Students can now simulate a basic smart environment monitoring system.

  • This experiment introduces the concept of sensors + actuators + logic, which is the foundation of IoT projects.

Comments