Projects / Bedroom fan that follows the night

Bedroom fan that follows the night

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Test the sensor

    Print the temperature.

  2. NANODS323113:27

    Set the clock

    Use the RTClib example.

  3. NANOIRLZ44

    Wire the MOSFET

    Gate resistor and flyback diode.

  4. NANO

    Run the fan at fixed speeds

    Check it starts at 30 percent.

  5. NANO

    Add the night curve

    Time-based speed.

  6. NANO

    Add the quiet button

    Cap the speed.

  7. NANO

    Log a night

    Review the serial output.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
DHT22 dataNano D2, with 10 kΩ to 5VRoom temperature
RTC SDA and SCLNano A4 and A5Clock
Nano D9IRLZ44N gate, with 10 kΩ gate to groundFan PWM
12 V adapter plusFuse (2 A), then fan plusFan supply
Fan minusIRLZ44N drainSwitched ground
IRLZ44N sourceAdapter minus and Nano GNDCommon ground
1N4007Across the fan, stripe to plusFlyback protection
Quiet-mode buttonNano D3 to groundCaps 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.

CheckExpected
Warm room at 10 pmHigher speed
Cooler at 4 amLower speed or stopped
Quiet buttonSpeed never above 40 percent
Fan noiseNo 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 →

Ask Lark AI

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.