

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.
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.Pin 2 connected to D21 on the ESP32.Pin 3 connected to GND on the ESP32.VCC connected to hot wire on the Power 220V.GND connected to N.O. on the 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.GND connected to GND on the ESP32.VCC connected to 3V3 on the ESP32.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.#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.