Temperature control fan using Arduino uno ||Arduino control fan
Arduino code
#include <DHT.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Motor driver connections
#define MOTOR_PIN_1 3 // Motor driver input 1
#define MOTOR_PIN_2 4 // Motor driver input 2
void setup() {
Serial.begin(9600);
dht.begin();
// Motor driver pins setup
pinMode(MOTOR_PIN_1, OUTPUT);
pinMode(MOTOR_PIN_2, OUTPUT);
}
void loop() {
delay(2000); // Delay between sensor readings
// Read temperature
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read temperature from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Set fan speed based on temperature
int speed = temperature * 4; // Speed of motor = temperature in Celsius * 4
Serial.print("Fan speed");
Serial.print(speed);
if (speed > 255) {
speed = 255; // Ensure speed does not exceed maximum PWM value
}
analogWrite(MOTOR_PIN_1, speed); // Set motor speed using PWM
if (temperature > 25) {
// Fan on
digitalWrite(MOTOR_PIN_2, LOW);
} else {
// Fan off
digitalWrite(MOTOR_PIN_2, HIGH);
}
}
Connection:
DHT11 Sensor:
VCC -> 5V
GND -> GND
Data -> Pin 2
Motor Driver:
Motor Input 1 (PWM) -> Pin 3
Motor Input 2 -> Pin 4
Comments
Post a Comment