Lesson 12: ESP32 + LCD + LED + Buzzer — Wokwi Tutorial

Learning Objectives

  • Learn how to combine multiple components with the ESP32.

  • Understand how to connect and program an I2C LCD, LED, and buzzer.

  • Simulate a project that integrates visual, sound, and display outputs.

Components Needed

  • 1 × ESP32 board

  • 1 × LED

  • 1 × 220Ω resistor

  • 1 × Active buzzer

  • 1 × I2C LCD (16x2) 

Steps in Wokwi

  1. Go to https://wokwi.com.

  2. Click Start a new project and select ESP32 as your board.

  3. From the Add Part (+) menu, add an LED, resistor, buzzer, and I2C LCD (16x2).

  4. Wire the components:

    • LED: GPIO 14 → Resistor → LED anode (+); LED cathode (–) → GND.

    • Buzzer: GPIO 27 → Buzzer +; Buzzer – → GND.

    • LCD (I2C): VCC → 5V, GND → GND, SDA → GPIO 21, SCL → GPIO 22.

Running the Simulation

  • Click ▶ Play in Wokwi.

  • The LCD will display messages.

  • The LED will blink and the buzzer will sound in sync.

Key Takeaways

  • How to use I2C communication for LCDs.

  • Safe wiring of an LED with a resistor.

  • Controlling multiple outputs (LED, buzzer, LCD) with one ESP32 board.

  • How hardware and software work together in IoT projects.


CODE:

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

// --- Pins ---
#define LED_PIN     32
#define BUZZER_PIN  27
#define DHT_PIN     25

// --- Alert thresholds ---
const float TEMP_HIGH_C = 30.0;
const float HUM_HIGH_PCT = 70.0;

// DHT sensor
DHTesp dht;

// LCD setup (I2C address 0x27 is common for 16x2 LCDs)
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  // Start DHT sensor
  dht.setup(DHT_PIN, DHTesp::DHT22);

  // Initialize LCD
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("DHT22 Monitor");
  delay(2000);
  lcd.clear();
}

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

  if (isnan(t) || isnan(h)) {
    Serial.println("DHT22 Error!");
    lcd.setCursor(0,0);
    lcd.print("Sensor Error    ");
    delay(2000);
    return;
  }

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

  // Show on LCD
  lcd.setCursor(0,0);
  lcd.print("Temp: "); lcd.print(t,1); lcd.print("C   ");
  lcd.setCursor(0,1);
  lcd.print("Hum: "); lcd.print(h,1); lcd.print("%   ");

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

  delay(2000);
}



Comments