Skip to Content


info@hub360.com.ng 07044715478

SELF REGULATING FAN

February 15, 2025 by
SELF REGULATING FAN
My Company (Chicago), Hub360
| No comments yet

The Self-Regulating Fan is an intelligent cooling solution designed to automatically adjust its speed in response to changes in the surrounding temperature. At the heart of this system is a temperature sensor, such as the DHT11 or LM35, which continuously monitors the ambient environment. The sensor feeds real-time temperature data to a microcontroller, which then processes the information and adjusts the fan speed using a Pulse Width Modulation (PWM) signal. This dynamic control mechanism ensures that the fan operates at the optimal speed—ramping up when temperatures rise and slowing down when conditions cool—resulting in both efficient cooling and energy savings.

This project is particularly well-suited for applications requiring automated temperature regulation, such as maintaining comfort in living spaces, preventing overheating in electronic devices, or even managing thermal conditions in small-scale industrial setups. Beyond its practical utility, the Self-Regulating Fan also serves as an excellent example of how fundamental electronics principles—like sensor integration, PWM control, and microcontroller programming—can be combined to create innovative, real-world solutions. Whether you're a hobbyist, student, or professional, this project demonstrates the power of simple components working together to solve everyday challenges.

Components Required

  • Arduino Uno
  • DHT 11 Temperature Sensor
  • DC Fan 
  • 2N222 NPN Transistor
  • Resistors (10KΩ, 330Ω)
  • 1N4007 Diode
  • Jumper wires
  • Power Supply 
  • Bread Board 

Circuit Diagram

Connect the Temperature Sensor to the Arduino:

  • For DHT11: Connect the VCC pin to 5V, GND to ground, and the data pin to a  digital pin (e.g., D2).
  • For LM35: Connect the VCC pin to 5V, GND to ground, and the output pin to an  analog pin (e.g., A0).

Connect the Fan to the Transistor:

  • ​Connect one terminal of the fan to the 12V power supply.
  • Connect the other terminal of the fan to the collector of the NPN transistor. 
  • Connect the emitter of the transistor to ground.

Add the Base Resistor:

  • Place a 330Ω resistor between the Arduino PWM pin (e.g., D9) and the base of  the transistor.

Add the Diode:

  • Place the diode across the fan terminals, with the cathode (striped end) connected  to the positive terminal.

Upload the Code:

  • Write and upload the program to the Arduino using the Arduino IDE.


CODE

#include <DHT.h> 


#define DHTPIN 2 // Pin connected to the DHT sensor 

#define DHTTYPE DHT11 // DHT sensor type 

#define FAN_PIN 9 // Pin connected to the fan (via transistor)


DHT dht(DHTPIN, DHTTYPE); 


void setup() { 

 pinMode(FAN_PIN, OUTPUT); 

 dht.begin(); 


void loop() { 

​ float temp = dht.readTemperature(); // Read temperature in Celsius 

​ if (isnan(temp)) { 

​ // If the reading fails, stop the fan 

​ analogWrite(FAN_PIN, 0); 

​ return; 

​ } 

​ // Map temperature to PWM signal (20°C = slow, 40°C = full speed) 

​ int fanSpeed = map(temp, 20, 40, 0, 255); 

​ fanSpeed = constrain(fanSpeed, 0, 255); // Limit fan speed between 0 and  255 

​ analogWrite(FAN_PIN, fanSpeed); 

​ delay(200); // Adjust delay for smoother transitions 

CODE EXPLANATION

1. Include the DHT Library

#include <DHT.h>

This line tells the Arduino to include the DHT library, which is a pre-written code that helps us easily read data from the DHT11 temperature sensor. Think of it as borrowing a tool to make your job easier.

2. Define the Pin Connections

#define DHTPIN 2 // Pin connected to the DHT sensor 
#define DHTTYPE DHT11 // DHT sensor type
#define FAN_PIN 9 // Pin connected to the fan (via transistor)
  • DHTPIN 2: This means the DHT11 sensor is connected to pin 2 on the Arduino.
  • DHTTYPE DHT11: This specifies that we’re using a DHT11 sensor (there are other types like DHT22, so this tells the library which one to use).
  • FAN_PIN 9: This means the fan is connected to pin 9 on the Arduino. The fan is controlled through a transistor, which acts like a switch to turn the fan on/off and adjust its speed.

3. Create a DHT Object

DHT dht(DHTPIN, DHTTYPE);

This line creates an object named dht that represents the DHT11 sensor. It’s like giving the sensor a name so we can talk to it later in the code.

4. Setup Function

void setup() { 
pinMode(FAN_PIN, OUTPUT);
dht.begin();
}

The setup() function runs once when the Arduino starts. Here’s what it does:

  • pinMode(FAN_PIN, OUTPUT): This sets the fan pin (pin 9) as an output pin, meaning the Arduino will send signals to control the fan.
  • dht.begin(): This initializes the DHT11 sensor so it’s ready to start reading temperature.

5. Loop Function

void loop() { 
float temp = dht.readTemperature(); // Read temperature in Celsius
if (isnan(temp)) {
   // If the reading fails, stop the fan
   analogWrite(FAN_PIN, 0);
   return;
}
// Map temperature to PWM signal (20°C = slow, 40°C = full speed) 
int fanSpeed = map(temp, 20, 40, 0, 255);
fanSpeed = constrain(fanSpeed, 0, 255); // Limit fan speed between 0 and 255
analogWrite(FAN_PIN, fanSpeed);
delay(200); // Adjust delay for smoother transitions
}

The loop() function runs over and over again, like a never-ending cycle. Here’s what happens inside it:

  • Read the Temperature:
float temp = dht.readTemperature();

This reads the current temperature in Celsius from the DHT11 sensor and stores it in a variable called temp.

  • Check if the Reading is Valid:
if (isnan(temp)) { 
  analogWrite(FAN_PIN, 0);
  return;
}
    • isnan(temp) checks if the temperature reading failed (e.g., if the sensor is disconnected or malfunctioning).
    • If the reading fails, the fan is turned off (analogWrite(FAN_PIN, 0)), and the code stops here for this cycle (return).
  • Map Temperature to Fan Speed:
int fanSpeed = map(temp, 20, 40, 0, 255);
    • The map() function converts the temperature range (20°C to 40°C) into a fan speed range (0 to 255).
    • For example:
      • At 20°C, the fan speed will be 0 (off or very slow).
      • At 40°C, the fan speed will be 255 (full speed).
      • Temperatures in between will result in proportional fan speeds.
  • Constrain the Fan Speed:
fanSpeed = constrain(fanSpeed, 0, 255);
    • This ensures the fan speed stays within the valid range (0 to 255). Even if the temperature goes below 20°C or above 40°C, the fan speed won’t go below 0 or above 255.
  • Set the Fan Speed​:
analogWrite(FAN_PIN, fanSpeed);
    • This sends a PWM (Pulse Width Modulation) signal to the fan pin to control its speed. The value of fanSpeed determines how fast the fan spins.
  • Add a Small Delay:
delay(200);
    • This pauses the program for 200 milliseconds (0.2 seconds) before the next temperature reading. It makes the fan speed adjustments smoother and prevents the system from reacting too quickly to small temperature changes.

How It All Works Together

  1. The DHT11 sensor reads the temperature.
  2. The Arduino converts the temperature into a fan speed using the map() function.
  3. The fan speed is adjusted using a PWM signal.
  4. The process repeats every 200 milliseconds, ensuring the fan speed is always appropriate for the current temperature.

OBSERVING FUNCTIONALITY 

  • Initial Test: Place the temperature sensor in a normal environment (room temperature).  The fan should run at a low speed or remain off. 
  • Simulate Higher Temperatures: Use a heat source (e.g., hairdryer) near the sensor to  increase the temperature. The fan speed should increase accordingly. 
  • Return to Normal Conditions: Allow the sensor to cool down. The fan speed should  decrease gradually.

COMMON PROBLEMS AND SOLUTIONS

1. Fan Not Responding:

  • Verify connections between the Arduino, transistor, and fan. 
  • Ensure the power supply is adequate for the fan. 

2. Inaccurate Temperature Readings: 

  • Check the sensor wiring. 
  • Replace the sensor if it’s faulty. 

3. Fan Speed Not Adjusting Properly: 

  • Ensure the PWM pin is functioning correctly. 
  • Adjust the mapping values in the code to better suit your fan's speed range.

Conclusion

The Self-Regulating Fan project uses an Arduino and a DHT11 temperature sensor to automatically adjust fan speed based on the surrounding temperature. The system reads temperature data, maps it to a PWM signal, and controls the fan to ensure efficient cooling while saving energy. This project demonstrates how simple electronics and programming can create practical, real-world solutions, making it an excellent introduction to sensor integration, PWM control, and automation for beginners. Happy tinkering!


Comments

Share this post
Tags
Archive
Sign in to leave a comment