Bedroom fan that follows the night
Build it step by step
0 of 7 doneTest the sensor
Print the temperature.
Set the clock
Use the RTClib example.
Wire the MOSFET
Gate resistor and flyback diode.
Run the fan at fixed speeds
Check it starts at 30 percent.
Add the night curve
Time-based speed.
Add the quiet button
Cap the speed.
Log a night
Review the serial output.
How do you build a bedroom fan that follows the night?
A fan changes speed through the night as the room cools, so you sleep without being too cold. You need Arduino Nano, DHT22 Sensor, DS3231 RTC module, MOSFET IRLZ44N, Rectifier Diode 1N4007, Resistor 10 kΩ, 12 V DC fan 80 mm, 12 V 2 A DC adapter and Tactile push button 6 mm. It takes 7 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 9
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuyDHT22 Sensor × 1
- SensorsBuy
- SemiconductorsBuyMOSFET IRLZ44N × 1
- SemiconductorsBuy
- PassivesBuyResistor 10 kΩ × 1
- Motors & ActuatorsBuy
- PowerBuy
- SwitchesBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| DHT22 data | Nano D2, with 10 kΩ to 5V | Room temperature |
| RTC SDA and SCL | Nano A4 and A5 | Clock |
| Nano D9 | IRLZ44N gate, with 10 kΩ gate to ground | Fan PWM |
| 12 V adapter plus | Fuse (2 A), then fan plus | Fan supply |
| Fan minus | IRLZ44N drain | Switched ground |
| IRLZ44N source | Adapter minus and Nano GND | Common ground |
| 1N4007 | Across the fan, stripe to plus | Flyback protection |
| Quiet-mode button | Nano D3 to ground | Caps the speed |
Code
The speed is a base value that changes through the night plus 10 percent for every degree above 26, and below 24 degrees the fan stops, so the room does not get cold after midnight. The DS3231 clock keeps time without Wi-Fi. Set it once with the example sketch in the RTClib library. A 25 kHz PWM from timer 1 puts the switching above hearing, which avoids the hum a default PWM makes. The fan needs a 12 V adapter and a 2 A fuse; the Nano runs from USB with its ground joined to the fan supply. Keep the fan guarded and away from curtains and bedding, place it so it never blows directly on a sleeper's face all night, and speak to a doctor rather than rely on a fan if heat is a health issue.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Night fan: speed follows the clock and room temperature. Libraries: DHT sensor library, RTClib. Board: Arduino Nano.
#include <DHT.h>
#include <Wire.h>
#include <RTClib.h>
DHT dht(2, DHT22);
RTC_DS3231 rtc;
const int QUIET = 3;
bool quiet = false;
void pwmBegin() { // 25 kHz PWM on D9 so the fan does not whine
pinMode(9, OUTPUT);
TCCR1A = _BV(COM1A1) | _BV(WGM11);
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
ICR1 = 639;
}
void setFan(int pct) { pct = constrain(pct, 0, 100); OCR1A = (long)pct * 639 / 100; }
int nightBase(int hour) { // percentage by hour: strong early, soft later
if (hour >= 22 || hour < 1) return 70;
if (hour < 3) return 55;
if (hour < 6) return 35;
return 50;
}
void setup() {
Serial.begin(9600);
pinMode(QUIET, INPUT_PULLUP);
dht.begin(); rtc.begin(); pwmBegin(); setFan(0);
}
void loop() {
DateTime now = rtc.now();
float t = dht.readTemperature();
if (digitalRead(QUIET) == LOW) { quiet = !quiet; delay(400); }
if (!isnan(t)) {
int speed = nightBase(now.hour()) + (int)((t - 26.0) * 10.0); // +10 percent per degree above 26
if (t < 24.0) speed = 0; // cool enough: stop
else if (speed < 30) speed = 30; // below about 30 percent most fans stall
if (quiet && speed > 40) speed = 40;
setFan(speed);
Serial.print(now.hour()); Serial.print("h "); Serial.print(t, 1); Serial.print(" C fan "); Serial.print(speed); Serial.println("%");
}
delay(5000);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Warm room at 10 pm | Higher speed |
| Cooler at 4 am | Lower speed or stopped |
| Quiet button | Speed never above 40 percent |
| Fan noise | No whine at low speed |
If it does not work
Fan will not start
Raise the minimum percentage.
Whine
Check the 25 kHz setup on pin D9.
Wrong time
Reset the RTC with the example sketch.
Reads nan
Check the DHT pull-up and wiring.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the bedroom fan that follows the night?
Arduino Nano, DHT22 Sensor, DS3231 RTC module, MOSFET IRLZ44N, Rectifier Diode 1N4007, Resistor 10 kΩ, 12 V DC fan 80 mm, 12 V 2 A DC adapter and Tactile push button 6 mm.
How long does the bedroom fan that follows the night take to build?
About 90 minutes for a beginner maker, in 7 steps.
How do I know the bedroom fan that follows the night works?
Warm room at 10 pm: Higher speed. Cooler at 4 am: Lower speed or stopped. Quiet button: Speed never above 40 percent.
Fan will not start: what should I check?
Raise the minimum percentage.
Whine: what should I check?
Check the 25 kHz setup on pin D9.
Wrong time: what should I check?
Reset the RTC with the example sketch.
Reads nan: what should I check?
Check the DHT pull-up and wiring.
