Arduino UNO-Based Temperature and Humidity Controlled Fan with LCD Display
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is a fan speed control system that uses an Arduino UNO microcontroller to read temperature and humidity data from a DHT11 sensor and display the information on a 16x2 I2C LCD. The fan speed is controlled based on the temperature readings using an NPN transistor as a switch.
Component List
Arduino UNO
- Description: Microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
DHT11
- Description: Temperature and humidity sensor.
- Pins: DATA, GND, VCC
16x2 I2C LCD
- Description: 16x2 character LCD with I2C interface.
- Pins: GND, VCC, SDA, SCL
Fan
- Description: DC fan.
- Pins: GND, 5V
NPN Transistor (EBC)
- Description: NPN transistor used for switching.
- Pins: emitter, base, collector
9V Battery
- Description: Power source.
- Pins: -, +
Wiring Details
Arduino UNO
- 5V: Connected to VCC of 16x2 I2C LCD, VCC of DHT11, and collector of NPN Transistor.
- GND: Connected to GND of 16x2 I2C LCD, GND of DHT11, GND of Fan, and - of 9V Battery.
- Vin: Connected to + of 9V Battery.
- A4: Connected to SDA of 16x2 I2C LCD.
- A5: Connected to SCL of 16x2 I2C LCD.
- D9: Connected to base of NPN Transistor.
- D7: Connected to DATA of DHT11.
DHT11
- DATA: Connected to D7 of Arduino UNO.
- GND: Connected to GND of Arduino UNO.
- VCC: Connected to 5V of Arduino UNO.
16x2 I2C LCD
- GND: Connected to GND of Arduino UNO.
- VCC: Connected to 5V of Arduino UNO.
- SDA: Connected to A4 of Arduino UNO.
- SCL: Connected to A5 of Arduino UNO.
Fan
- GND: Connected to GND of Arduino UNO.
- 5V: Connected to emitter of NPN Transistor.
NPN Transistor (EBC)
- emitter: Connected to 5V of Fan.
- base: Connected to D9 of Arduino UNO.
- collector: Connected to 5V of Arduino UNO.
9V Battery
- -: Connected to GND of Arduino UNO.
- +: Connected to Vin of Arduino UNO.
Code Documentation
#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin and type definitions
#define DHTPIN 7 // DHT sensor pin
#define DHTTYPE DHT11 // DHT 11 type
#define pwm 9 // Fan control PWM pin
// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize I2C LCD (update address if necessary)
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Fan Speed Ctrl");
delay(2000);
// Display initialization message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Initializing...");
analogWrite(pwm, 255); // Initialize fan at full speed
delay(2000);
// System ready message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("System Ready");
delay(2000);
// Initialize Serial Monitor
Serial.begin(9600);
Serial.println("Fan Speed Controlling System");
// Initialize DHT sensor
dht.begin();
}
void loop() {
delay(1000);
// Read temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature(); // Celsius
float f = dht.readTemperature(true); // Fahrenheit
// Check for sensor errors
if (isnan(h) || isnan(t) || isnan(f)) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sensor Error!");
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index
float hi = dht.computeHeatIndex(f, h);
// Print data to Serial Monitor
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" °C ");
Serial.print(f);
Serial.print(" °F\t");
Serial.print("Heat Index: ");
Serial.print(hi);
Serial.println(" °F");
// Display temperature and humidity on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(h);
lcd.print("%");
// Fan speed control logic
if (t < 25) {
analogWrite(pwm, 0);
Serial.println("Fan OFF");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan OFF");
} else if (t >= 25 && t < 30) {
analogWrite(pwm, 32); // Fan at 12.5% speed
Serial.println("Fan Speed: 12.5%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan Speed: 12.5%");
} else if (t >= 30 && t < 35) {
analogWrite(pwm, 64); // Fan at 25% speed
Serial.println("Fan Speed: 25%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan Speed: 25%");
} else if (t >= 35 && t < 40) {
analogWrite(pwm, 128); // Fan at 50% speed
Serial.println("Fan Speed: 50%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan Speed: 50%");
} else if (t >= 40 && t < 45) {
analogWrite(pwm, 191); // Fan at 75% speed
Serial.println("Fan Speed: 75%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan Speed: 75%");
} else if (t >= 45 && t < 50) {
analogWrite(pwm, 239); // Fan at 93.75% speed
Serial.println("Fan Speed: 93.75%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan Speed: 93.75%");
} else {
analogWrite(pwm, 255); // Fan at 100% speed
Serial.println("Fan Speed: 100%");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Fan Speed: 100%");
}
delay(2000); // Delay before the next iteration
}
This code initializes the DHT11 sensor and the 16x2 I2C LCD, reads temperature and humidity data, and controls the fan speed based on the temperature readings. The data is displayed on the LCD and printed to the Serial Monitor.