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.
#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.