This circuit is designed to control a 12V fan using an Arduino UNO based on humidity readings from a DHT11 sensor. The fan is powered by a 12V battery and its speed is controlled through a PWM signal from the Arduino, which is interfaced with an NPN transistor acting as a switch. A resistor is used to limit the current to the base of the transistor, and a diode is included to protect against reverse voltage spikes when the fan is turned off.
GND
connected to the ground of the DHT11 sensor and the emitter of the NPN transistor5V
connected to the 5V pin of the DHT11 sensorD9
connected to one end of the 1kΩ resistorD2
connected to the data pin of the DHT11 sensor12V+
connected to the cathode of the rectifier diode and the collector of the NPN transistorGND
connected to the ground of the 12V batteryE
(Emitter) connected to the ground of the Arduino UNOC
(Collector) connected to the anode of the rectifier diode and the 12V+ pin of the fanB
(Base) connected to the other end of the 1kΩ resistor5V
connected to the 5V pin of the Arduino UNOS
(Signal) connected to the D2 pin of the Arduino UNOGND
connected to the ground of the Arduino UNOVCC
connected to the cathode of the rectifier diodeGND
connected to the anode of the rectifier diode, the ground of the fan, and the collector of the NPN transistorCathode
connected to the VCC of the 12V battery and the 12V+ pin of the fanAnode
connected to the collector of the NPN transistor and the ground of the 12V battery#include "DHT.h"
// Pin definitions
#define DHTPIN 2 // Pin connected to the data pin of the DHT sensor
#define FANPIN 9 // PWM pin connected to the base of the transistor
// DHT sensor type
#define DHTTYPE DHT11 // Use DHT11 or DHT22, depending on the sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(FANPIN, OUTPUT); // Set fanPin as an output
dht.begin(); // Initialize the DHT sensor
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
// Read humidity from the DHT sensor
float humidity = dht.readHumidity();
// Check if the reading is valid
if (isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the humidity value for debugging
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Map humidity to a PWM value (0-255)
// Assuming you want the fan to run faster at higher humidity
int fanSpeed = map(humidity, 30, 100, 0, 255); // Adjust 30 (min humidity) and 100 (max humidity) as needed
// Make sure fanSpeed stays within the 0-255 range
fanSpeed = constrain(fanSpeed, 0, 255);
// Set the fan speed using PWM
analogWrite(FANPIN, fanSpeed);
// Wait before the next reading
delay(2000); // Read every 2 seconds
}
This code is responsible for reading the humidity level from the DHT11 sensor and controlling the speed of the fan accordingly. The humidity level is mapped to a PWM value which is then used to set the fan speed. The fan is controlled via a transistor that is driven by the PWM signal from the Arduino's D9 pin.