Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

ESP32-Based Smart Temperature and Humidity Display with Automated Violet LED Control

Image of ESP32-Based Smart Temperature and Humidity Display with Automated Violet LED Control

Circuit Documentation

Summary

This circuit is designed to interface an ESP32 microcontroller with various peripherals including an LCD screen, a pushbutton, a relay module, LED light strips, and a DHT22 sensor. The ESP32 reads temperature and humidity data from the DHT22 sensor and displays it on the LCD screen. It also controls the state of the LED light strips through the relay module based on the pushbutton input. The LED light strips are powered by a 220V power source, which is switched on and off by the relay.

Component List

ESP32 (30 pin)

  • Microcontroller with WiFi and Bluetooth capabilities.
  • It has a variety of digital I/O pins and supports I2C communication.

Pushbutton

  • A simple switch mechanism for control of a machine or process.
  • Typically used to provide user input to the microcontroller.

LED Light Strips (Violet)

  • Strips of LEDs that emit violet light.
  • Powered by a 220V power source through a relay.

1 Channel 5V Relay Module

  • An electrically operated switch that allows you to turn on or off a circuit using voltage and/or current much higher than a microcontroller can handle.
  • It is used to control the LED light strips in this circuit.

DHT22

  • A sensor for measuring temperature and humidity.
  • Provides digital output that can be read by the microcontroller.

LCD screen 16x2 I2C

  • An alphanumeric liquid crystal display capable of displaying 16 characters per line in 2 lines.
  • Uses I2C communication protocol to interface with the microcontroller.

Power 220V

  • The main power source for the LED light strips.
  • Provides 220V through the hot wire and neutral wire.

Wiring Details

ESP32 (30 pin)

  • D12 connected to SCL on the LCD screen.
  • D13 connected to SDA on the LCD screen.
  • GND connected to GND on the LCD screen, GND on the DHT22, and VCC- (GND) on the Relay Module.
  • D21 connected to Pin 2 on the Pushbutton.
  • D15 connected to IN on the Relay Module.
  • 3V3 connected to VCC+ on the Relay Module, VCC on the LCD screen, and VCC on the DHT22.

Pushbutton

  • Pin 2 connected to D21 on the ESP32.
  • Pin 3 connected to GND on the ESP32.

LED Light Strips (Violet)

  • VCC connected to hot wire on the Power 220V.
  • GND connected to N.O. on the Relay Module.

1 Channel 5V Relay Module

  • IN connected to D15 on the ESP32.
  • VCC- (GND) connected to GND on the ESP32 and Pin 3 on the Pushbutton.
  • VCC+ connected to 3V3 on the ESP32.
  • N.O. connected to GND on the LED Light Strips.
  • COM connected to neutral wire on the Power 220V.

DHT22

  • GND connected to GND on the ESP32.
  • VCC connected to 3V3 on the ESP32.

LCD screen 16x2 I2C

  • SCL connected to D12 on the ESP32.
  • SDA connected to D13 on the ESP32.
  • VCC connected to 3V3 on the ESP32.
  • GND connected to GND on the ESP32.

Documented Code

#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Adafruit_Sensor.h>

#include <Wire.h>
#include <Adafruit_GFX.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET    -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN 4        // Pin for DHT22 Data
#define DHTTYPE DHT22   // DHT 22 (AM2302)

DHT dht(DHTPIN, DHTTYPE);

const int buttonPin = 12;     // Pin for Push Button
const int relayPin = 13;      // Pin for Relay
bool lampState = false;       // Lamp status (LED)
unsigned long startMillis;
bool buttonPressed = false;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Using internal pull-up
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Turn off relay at start

  Serial.begin(115200);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.display();
  delay(2000);
  display.clearDisplay();

  dht.begin();
}

void loop() {
  // Reading temperature and humidity
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Displaying results on OLED
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print(F("Temperature: "));
  display.print(t);
  display.println(F(" *C"));

  display.print(F("Humidity: "));
  display.print(h);
  display.println(F(" %"));

  display.display();

  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && !buttonPressed) {
    // Button pressed
    buttonPressed = true;
    lampState = !lampState; // Toggle lamp status
    if (lampState) {
      digitalWrite(relayPin, HIGH); // Turn on relay (LED on)
      startMillis = millis(); // Start timing
    }
  }

  if (lampState && (millis() - startMillis >= 10000)) { // 10 minutes = 600000 ms
    digitalWrite(relayPin, LOW); // Turn off relay (LED off)
    lampState = false;
    buttonPressed = false;
  }

  if (buttonState == HIGH) {
    buttonPressed = false; // Reset button status after release
  }

  delay(2000); // Delay for DHT sensor reading
}

This code is designed to run on the ESP32 microcontroller. It initializes the display and DHT22 sensor in the setup() function and continuously reads temperature and humidity data in the loop() function. The data is displayed on the OLED screen. The pushbutton is used to toggle the state of the relay, which controls the LED light strips. The relay is turned off automatically after 10 minutes if it was turned on by the pushbutton.