Temperature-controlled PWM fan with a DS18B20 and MOSFET: design worksheet and code

A ₹1,195 design worksheet for a fan that speeds up as the temperature rises, using a DS18B20 sensor and a logic-level MOSFET, with the duty-cycle curve and hysteresis worked out.

About the author · Sources, testing and AI policy

  • Parts₹1,195
  • Bench time3–5 h
  • LevelIntermediate
  • ReviewedNov 2026

A ₹1,195 fan controller that does not run flat out all the time. A DS18B20 sensor reads the temperature, a logic-level MOSFET switches a 12 V fan with PWM, and the sketch raises the speed in a straight line from 30 percent at 28 °C to full speed at 40 °C. It is useful for a cabinet, a 3D-printer enclosure or a small server shelf.

Scope: intermediate. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The duty table below was computed by script. Fan current varies, so check your fan label before choosing the MOSFET and supply. A fan stall or a loose sensor wire should never be able to leave a heat source unprotected, so treat this as a comfort controller, not a safety cut-off.

How it works

Below 27 °C the fan is off. At 28 °C it starts at 30 percent duty, because many fans will not start from a very low duty. From there the duty rises linearly with temperature to 100 percent at 40 °C, which is 30 + (T − 28) × 70 / 12 percent. The gap between 27 °C off and 28 °C on is a one degree hysteresis, so the fan does not chatter around a single threshold.

The Uno’s PWM runs at roughly 490 Hz on most pins and 980 Hz on pins 5 and 6. That can make some fans hum. It is usually acceptable for a simple 2 or 3-wire fan; four-wire PC fans have their own PWM input and expect about 25 kHz.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
DS18B20 waterproof probe1₹120₹120
4.7 kilohm resistor1₹2₹2
Logic-level N-channel MOSFET1₹40₹40
1N4007 diode1₹3₹3
12 V DC fan (two or three wire)1₹120₹120
12 V power adapter1₹250₹250
Breadboard1₹60₹60
Jumper wires1₹50₹50
Parts total₹1,195

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

TemperatureFan dutyPWM value (0 to 255)
27 °C or belowOff0
28 °C30%77
31 °C47.5%121
34 °C65%166
37 °C82.5%210
40 °C or above100%255
Duty = 30 + (T − 28) × 70 / 12 percent. PWM value = duty × 255 / 100, rounded.

The MOSFET needs to be a logic-level type that switches fully on at 5 V on its gate; check its datasheet for the drain-source resistance at a gate voltage of 5 V. At a fan current of 0.2 A and a resistance of 0.05 ohm the power in the MOSFET is only 2 milliwatts, so it will stay cool. The fan label gives your real current.

Power and wiring

PartConnects toNote
DS18B20 red, black, yellow5V, GND, D24.7k resistor between data and 5V
MOSFET gateD5 through a 100 ohm resistorPin 5 gives about 980 Hz PWM
MOSFET sourceGND, shared with the 12 V supply
Fan negativeMOSFET drainFan positive to +12 V
1N4007 diodeAcross the fan, stripe to +12 VAbsorbs the spike when the fan switches off
Join the Arduino ground and the 12 V supply ground. Add a 10 kilohm resistor from gate to ground so the fan stays off while the Arduino starts.

The sketch

// Temperature-controlled fan: Uno + DS18B20 + logic-level MOSFET
// Design worksheet: check the fan current and MOSFET rating first.

#include <OneWire.h>
#include <DallasTemperature.h>

const int ONE_WIRE = 2;
const int FAN = 5;
const float T_ON = 28.0, T_OFF = 27.0, T_FULL = 40.0;
const float DUTY_MIN = 30.0;

OneWire bus(ONE_WIRE);
DallasTemperature sensors(&bus);
bool running = false;

void setup() {
  pinMode(FAN, OUTPUT);
  analogWrite(FAN, 0);
  sensors.begin();
  Serial.begin(9600);
}

void loop() {
  sensors.requestTemperatures();
  float t = sensors.getTempCByIndex(0);

  if (t == DEVICE_DISCONNECTED_C) {          // sensor fault: run the fan
    analogWrite(FAN, 255);
    Serial.println("sensor fault");
    delay(1000);
    return;
  }

  if (!running && t >= T_ON) running = true;
  if (running && t <= T_OFF) running = false;

  int pwm = 0;
  if (running) {
    float duty = DUTY_MIN + (t - T_ON) * (100.0 - DUTY_MIN) / (T_FULL - T_ON);
    duty = constrain(duty, DUTY_MIN, 100.0);
    pwm = (int)(duty * 255.0 / 100.0 + 0.5);
  }
  analogWrite(FAN, pwm);
  Serial.print(t); Serial.print(" C, pwm "); Serial.println(pwm);
  delay(1000);
}

A disconnected sensor reports a special error value; the sketch then runs the fan flat out instead of stopping it, which is the safer failure. The one second delay matches the sensor’s conversion time. The sketch has been proofread but not compiled on this site.

Test plan

  1. Sensor check: compare the reading with a second thermometer in the same spot.
  2. Curve check: warm the probe with your hand or warm water and note the PWM values against the table.
  3. Hysteresis: hold the probe near 27.5 °C and confirm the fan does not chatter.
  4. Fault test: unplug the sensor data wire and confirm the fan goes to full speed.
  5. Start-up: check the fan reliably starts at 30 percent; if it stalls, raise DUTY_MIN.
  6. Heat test: run it in the enclosure with the real heat source and see where the temperature settles.

What usually goes wrong

SymptomLikely causeFix
Fan hums at low duty490 or 980 Hz PWM audibleRaise DUTY_MIN or use a fan with a PWM input
Fan does not start at 30%Fan needs more starting voltageRaise DUTY_MIN
Reads −127 °CSensor not detectedCheck the 4.7k resistor and wiring
MOSFET hotNot a logic-level part, or too much currentCheck the part number and the fan current
Fan runs at power-upFloating gateAdd the 10k pull-down resistor

Upgrades

  • Tachometer: read a three-wire fan’s tach pin to detect a stall.
  • Display: show temperature and duty on a small screen.
  • 25 kHz PWM: reconfigure a timer to drive a four-wire PC fan.
  • Two sensors: control from the hotter of two probes.

Cost and parts notes

The ₹1,195 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

Why start at 30 percent duty?

Many fans will not start turning from a very low duty, so the curve begins at a level most fans can start from. Check yours.

Can I use a regular transistor?

A logic-level MOSFET is a better fit because it switches fully on at 5 V. A plain transistor needs a base resistor and wastes more power.

Can this protect a heater from overheating?

No. It is a comfort controller. Use a proper thermal cut-off for safety.

Why is the diode needed?

A fan is an inductive load, and the diode gives its current a path when the MOSFET turns off.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.