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

ESP32-Based Smart Temperature-Controlled Fan with OLED Display and DHT11 Sensor

Image of ESP32-Based Smart Temperature-Controlled Fan with OLED Display and DHT11 Sensor

Circuit Documentation

Summary of the Circuit

This circuit is designed to monitor environmental conditions and control a fan based on temperature readings. It features an ESP32 microcontroller that interfaces with a DHT11 temperature and humidity sensor, an OLED display for user feedback, a buzzer for audible alerts, and a fan controlled via a transistor acting as a switch. The ESP32 uses I2C communication to interact with the OLED display and GPIO pins to read sensor data, control the fan, and drive the buzzer.

Component List

ESP32 38 PINS

  • Description: A microcontroller with Wi-Fi and Bluetooth capabilities.
  • Pins: GND, G23, G22, TXD, RXD, G21, G19, G18, G5, G17, G16, G4, G0, G2, G15, SDI, SD0, CLK, 3V3, EN, SP, SN, G34, G35, G32, 33, G25, G26, G27, G14, G12, G13, SD2, SD3, 5V.

DHT11 Sensor V2

  • Description: A basic, low-cost digital temperature and humidity sensor.
  • Pins: GND, DAT, VCC.

0.96" OLED

  • Description: A small display for visual output.
  • Pins: GND, VDD, SCK, SDA.

Fan

  • Description: An electric fan for cooling.
  • Pins: GND, 5V.

NPN Transistor (EBC)

  • Description: A bipolar junction transistor used as a switch or amplifier.
  • Pins: emitter, base, collector.

Buzzer

  • Description: An electronic buzzer for audible alerts.
  • Pins: PIN, GND.

Wiring Details

ESP32 38 PINS

  • G22 connected to OLED SCK
  • G21 connected to OLED SDA
  • G4 connected to DHT11 Sensor DAT
  • G25 connected to NPN Transistor base
  • G26 connected to Buzzer PIN
  • GND connected to GND of all components
  • 5V connected to VCC of DHT11 Sensor, VDD of OLED, and collector of NPN Transistor

DHT11 Sensor V2

  • DAT connected to ESP32 G4
  • GND connected to ESP32 GND
  • VCC connected to ESP32 5V

0.96" OLED

  • SCK connected to ESP32 G22
  • SDA connected to ESP32 G21
  • GND connected to ESP32 GND
  • VDD connected to ESP32 5V

Fan

  • GND connected to NPN Transistor emitter
  • 5V connected to NPN Transistor collector

NPN Transistor (EBC)

  • Base connected to ESP32 G25
  • Emitter connected to Fan GND
  • Collector connected to ESP32 5V and Fan 5V

Buzzer

  • PIN connected to ESP32 G26
  • GND connected to ESP32 GND

Documented Code

#include "DHT.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN 4     // DHT11 data pin connected to GPIO4 of ESP32
#define DHTTYPE DHT11 // DHT11 sensor type

#define pwm 25      // PWM pin for fan control (GPIO25)
#define buzzer 26   // Buzzer connected to GPIO26

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// PWM channel for the fan
int fanChannel = 0;

void setup() {
  pinMode(buzzer, OUTPUT);  // Buzzer pin as output
  
  // Setup PWM for fan control
  ledcSetup(fanChannel, 5000, 8);  // 5 kHz PWM, 8-bit resolution
  ledcAttachPin(pwm, fanChannel);  // Attach fan to PWM channel
  
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {  // Address 0x3C for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;) ;  // Loop forever if display fails
  }
  
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.print(F("Auto Fan Speed"));
  display.setCursor(0, 10);
  display.print(F("Controller"));
  display.display();
  delay(2000);
  
  ledcWrite(fanChannel, 255);  // Set fan speed to 100% initially
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print(F("Using DHT Sensor"));
  display.setCursor(0, 10);
  display.print(F("John Carlo D. Martin"));
  display.display();
  delay(2000);
  display.clearDisplay();
  
  Serial.begin(115200);
  dht.begin();
}

void loop() {
  delay(2000);  // Wait for sensor readings
  
  float h = dht.readHumidity();  // Read humidity
  float t = dht.readTemperature();  // Read temperature (Celsius)
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C");
  
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print(F("temp: "));
  display.print(t);
  display.print(F(" *C"));
  display.setCursor(0, 10);
  
  if (t < 28) {
    ledcWrite(fanChannel, 0);  // Fan OFF
    display.print(F("Fan OFF"));
    tone(buzzer, 1000);  // Sound buzzer
    delay(3000);
    noTone(buzzer);  // Turn off buzzer
    delay(100);
  } else if (t >= 28 && t <= 29) {
    ledcWrite(fanChannel, 51);  // Fan at 20% speed
    display.print(F("Fan Speed: 20%"));
    tone(buzzer, 1000);
    delay(1000);
    noTone(buzzer);
    delay(100);
  } else if (t >= 29 && t <= 30) {
    ledcWrite(fanChannel, 102);  // Fan at 40% speed
    display.print(F("Fan Speed: 40%"));
    tone(buzzer, 1000);
    delay(1000);
    noTone(buzzer);
    delay(100);
  } else if (t >= 30 && t <= 31) {
    ledcWrite(fanChannel, 153);  // Fan at 60% speed
    display.print(F("Fan Speed: 60%"));
    tone(buzzer, 1000);
    delay(1000);
    noTone(buzzer);
    delay(100);
  } else if (t >= 31 && t <= 32) {
    ledcWrite(fanChannel, 204);  // Fan at 80% speed
    display.print(F("Fan Speed: 80%"));
    tone(buzzer, 1000);
    delay(1000);
    noTone(buzzer);
    delay(100);
  } else if (t > 32) {
    ledcWrite(fanChannel, 255);  // Fan at 100% speed
    display.print(F("Fan Speed: 100%"));
    tone(buzzer, 1000);
    delay(3000);
    noTone(buzzer);
    delay(100);
  }
  
  display.display();
  delay(3000);
}

This code is designed to run on the ESP32 microcontroller. It initializes the DHT11 sensor and the OLED display, reads temperature and humidity data, and controls the fan speed and buzzer based on the temperature readings. The fan speed is controlled using PWM, and the buzzer is activated at different temperature thresholds to alert the user. The OLED display shows the current temperature and fan status.