A temperature-based fan control system automatically regulates the operation of a fan based on ambient temperature. It ensures efficient cooling and energy savings by activating the fan only when required.
Objective: To control a fan automatically based on real-time temperature.
Method: Using a DHT11 sensor to monitor temperature, an Arduino to process data, and a relay module to control the fan.
Features:
Real-time temperature display.
Automatic fan control.
LCD display for system status.
Hardware:
DHT11 Sensor:
Measures temperature and humidity.
Output: Digital signal.
Relay Module:
Acts as a switch to control high-power devices (fan).
Operates on 5V signal from Arduino.
16x2 LCD:
Displays temperature, fan status, and elapsed time.
Arduino Board:
Microcontroller to process temperature data and control the relay.
Fan:
Controlled based on temperature.
Resistors and Wires:
For stable connections.
Software:
Arduino IDE for programming the Arduino.
Libraries:
DHT.h: For interacting with the DHT sensor.
LiquidCrystal.h: For controlling the LCD.
DHT Sensor Library: For DHT11/DHT22 sensors.
DallasTemperature Library: For DS18B20 sensors.
Adafruit Unified Sensor Library: For additional support.
Install these libraries using the Library Manager in Arduino IDE (Sketch > Include Library > Manage Libraries).
4.Circuit Diagram
5.Complete Pinout and Connection Guide:
Power Source: Ensure your Arduino is connected to a USB cable or an external power supply.
Use the 5V and GND pins on the Arduino as the power source for the DHT11 sensor, relay module, and LCD.
Step 2: Connect the DHT11 Sensor
DHT11 Pins:
VCC: Connect to Arduino’s 5V pin.
GND: Connect to Arduino’s GND.
Data Pin: Connect to Arduino pin 7.
Optional Resistor:
Place a 10kΩ pull-up resistor between the DHT11’s VCC and Data Pin to stabilize the signal.
Step 3: Connect the Relay Module
Relay Control Pins:
VCC: Connect to Arduino’s 5V.
GND: Connect to Arduino’s GND.
IN: Connect to Arduino pin 9.
Relay Output Pins (Fan Control):
COM (Common): Connect to the positive terminal of the fan’s power supply.
NO (Normally Open): Connect to the fan’s positive terminal.
GND (Fan Power Supply): Connect the negative terminal of the fan’s power supply to the fan’s negative terminal.
Step 4: Connect the Fan
Connect the fan as described in Step 3:
Fan’s positive wire to NO on the relay.
Fan’s negative wire to the power supply ground.
DC Fan Example:
Use a 12V power supply for a 12V DC fan.
Ensure the relay module can handle the fan’s current and voltage rating.
AC Fan Example:
Wire the AC fan’s live wire through the relay (COM and NO) and directly connect the neutral wire to the AC power source.
Step 5: Connect the 16x2 LCD
There are two ways to connect the LCD: with an I2C module or without it.
Option 1: With I2C Module
I2C Pins:
SDA: Connect to Arduino A4.
SCL: Connect to Arduino A5.
VCC: Connect to Arduino’s 5V.
GND: Connect to Arduino’s GND.
Option 2: Without I2C Module
LCD Pins:
RS: Connect to pin 12 on the Arduino.
E (Enable): Connect to pin 11.
D4: Connect to pin 5.
D5: Connect to pin 4.
D6: Connect to pin 3.
D7: Connect to pin 2.
Power:
VCC: Connect to Arduino’s 5V.
GND: Connect to Arduino’s GND.
Contrast Adjustment:
Connect a 10kΩ potentiometer:
One side to Arduino 5V.
The other side to GND.
Middle pin to the VO (Contrast) pin of the LCD.
Step 6: Verify All Connections
Here’s how your connections should look:
Arduino 5V and GND power the DHT11, relay module, and LCD.
DHT11 Data Pin is connected to pin 7.
Relay IN Pin is connected to pin 9.
LCD is connected either through an I2C module (SDA, SCL) or directly via its control pins.
Fan is connected to the relay module, which is connected to the power supply.
6.code
#include <DHT.h>
#include <LiquidCrystal.h>
// Define DHT11 Sensor pin
#define DHTPIN 7 // Pin where the DHT11 data pin is connected
#define DHTTYPE DHT11 // Define the type of DHT sensor (DHT11 or DHT22)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
// Initialize LCD with your pin configuration
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// Define relay pin
const int relayPin = 9;
// Timer variables
unsigned long previousMillis = 0; // Stores the last time fan state was updated
unsigned long currentMillis = 0; // Current time
unsigned long elapsedTime = 0; // Timer for how long the fan is ON/OFF
bool fanStatus = false; // Fan status (ON = true, OFF = false)
void setup() {
lcd.begin(16, 2); // Initialize the LCD
lcd.print("Fan Control"); // Display message on the LCD
pinMode(relayPin, OUTPUT); // Set relay pin as output
digitalWrite(relayPin, LOW); // Ensure the fan is off initially
dht.begin(); // Start DHT sensor
}
void loop() {
// Reading temperature from DHT11
float temperature = dht.readTemperature(); // Temperature in Celsius
// Check if the reading is successful
if (isnan(temperature)) {
lcd.setCursor(0, 1);
lcd.print("Failed to read!");
return;
}
// Get the current time in milliseconds
currentMillis = millis();
// Fan control logic based on temperature
if (temperature > 32) {
if (!fanStatus) { // If the fan was OFF and now needs to turn ON
fanStatus = true; // Fan turned ON
elapsedTime = 0; // Reset timer when the fan turns ON
}
digitalWrite(relayPin, HIGH); // Turn ON the fan if temperature > 32°C
} else {
if (fanStatus) { // If the fan was ON and now needs to turn OFF
fanStatus = false; // Fan turned OFF
elapsedTime = 0; // Reset timer when the fan turns OFF
}
digitalWrite(relayPin, LOW); // Turn OFF the fan if temperature <= 32°C
}
// If the fan is ON, update the elapsed time
if (fanStatus) {
elapsedTime = (currentMillis - previousMillis) / 1000; // Convert to seconds
}
// Display temperature and elapsed time on the LCD
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
// Display fan status and elapsed time (ON/OFF time)
lcd.setCursor(0, 0);
if (fanStatus) {
lcd.print("Fan ON | Time: ");
} else {
lcd.print("Fan OFF| Time: ");
}
lcd.print(elapsedTime);
lcd.print("s");
// Update previousMillis only if fan state has changed
if (fanStatus != (elapsedTime > 0)) {
previousMillis = currentMillis;
}
delay(1000); // Delay for 1 second before the next reading
}
Library Imports
#include <DHT.h>
#include <LiquidCrystal.h>
DHT.h: Enables communication with the DHT sensor.
LiquidCrystal.h: Simplifies interfacing with a 16x2 LCD.
Defining Pins and Initializing Components
#define DHTPIN 7 // DHT11 data pin connected to pin 7
#define DHTTYPE DHT11 // Type of DHT sensor
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD pins
const int relayPin = 9; // Relay connected to pin 9
Defines which pins are connected to each component.
dht object: Handles communication with the sensor.
lcd object: Manages the LCD display.
Setup
void setup() {
lcd.begin(16, 2); // Initialize the 16x2 LCD
lcd.print("Fan Control"); // Display initial message
pinMode(relayPin, OUTPUT); // Relay as output
digitalWrite(relayPin, LOW); // Fan initially off
dht.begin(); // Start DHT sensor
}
Initializes components for operation.
Displays "Fan Control" message on the LCD.
Loop
Read Temperature:
float temperature = dht.readTemperature();
if (isnan(temperature)) {
lcd.setCursor(0, 1);
lcd.print("Failed to read!");
return;
}
Reads temperature from DHT11.
Displays an error if the reading fails.
Fan Control Logic:
if (temperature > 32) {
fanStatus = true;
digitalWrite(relayPin, HIGH); // Turn fan ON
} else {
fanStatus = false;
digitalWrite(relayPin, LOW); // Turn fan OFF
}
Activates or deactivates the fan based on temperature.
Threshold: 32°C.
Display Status:
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 0);
if (fanStatus) {
lcd.print("Fan ON | Time: ");
} else {
lcd.print("Fan OFF| Time: ");
}
lcd.print(elapsedTime);
lcd.print("s");
Displays the temperature and fan status on the LCD.
DHT11 Sensor:
Outputs temperature data via its data pin.
Relay:
Acts as an intermediary switch to control the fan.
Arduino:
Processes sensor data and drives the relay.
Smart Home Systems: Automatically adjusts fans in response to room temperature.
Industrial Cooling: Ensures optimal temperature control in sensitive environments.
Energy Saving Solutions: Minimizes power consumption in various applications.
How to interface sensors and displays with Arduino.
Logical decision-making using microcontrollers.
Practical use of relays for high-power devices.
Saves energy by running the fan only when needed.
Automates temperature control without manual effort.
Provides real-time temperature and fan status on an LCD.
Affordable and easy to build using readily available components.
Great for learning automation and Arduino basics.
Limited temperature accuracy due to the DHT11 sensor.
Cannot handle high-power fans without additional hardware.
Requires constant power supply to operate.
Fixed temperature threshold needs reprogramming to adjust.
Limited to single fan control without further expansion.
IoT Integration:
Add Wi-Fi or Bluetooth modules (e.g., ESP8266 or HC-05) for remote monitoring and control using smartphones or web interfaces.
Multiple Device Control:
Expand the system to control multiple fans or other appliances based on different temperature thresholds.
Adjustable Threshold:
Implement a potentiometer or keypad to allow users to set and adjust the temperature threshold without reprogramming.
Humidity Monitoring:
Use the DHT11’s humidity data to control a dehumidifier or additional devices.
Smart Notifications:
Send alerts via SMS or mobile apps when temperature exceeds critical limits.
Battery Backup:
Add a rechargeable battery or UPS system to ensure uninterrupted operation during power outages.
Improved Sensors:
Replace the DHT11 with a more accurate sensor like the DHT22 or DS18B20 for better temperature precision.
Fan Speed Control:
Use a PWM (Pulse Width Modulation) technique to control the fan speed dynamically based on the temperature.
Data Logging:
Record temperature and fan activity data on an SD card or cloud platform for analysis and trend monitoring.
Integration with Smart Home Systems:
Add compatibility with smart home platforms like Alexa, Google Home, or Home Assistant for voice commands.
14.video:
video will upload as soon.......🔜
Encourage visitors to explore more or connect with you:
"Want to learn more or build a similar project? Feel free to contact me or explore the resources on ElectroMentor!"
Have questions or need more information?
📞 Contact Mohammed shafwan at 6360322698
🌐 Visit us at https://sites.google.com/view/electromentor/project?authuser=0 for exciting updates and resources!