Skip to content

Uno · Build guide

Arduino Temperature-Controlled Fan with DS18B20, L298N and LCD

Make a 12 V fan speed up as things get hotter. An Arduino Uno reads a DS18B20 probe, compares it with a set-point you choose on a knob and drives the fan through an L298N with PWM, with hysteresis, a fail-safe and a live LCD readout.

Arduino Temperature-Controlled Fan with DS18B20, L298N and LCD
Difficulty
Intermediate
Read time
17 min
Board
Uno
Author
Shantanu Kumar Mohanty
Published
Nov 20, 2026
Views
0

What You Will Build

Quick answer: connect a DS18B20 to D2 with a 4.7 kΩ pull-up, a 10 kΩ knob to A0, and an L298N to D9 (ENA), D8 (IN1) and D7 (IN2), with the fan on OUT1/OUT2 and its own 12 V supply. The sketch turns the fan on at the set-point and ramps it from about 35 % to 100 % over the next 5 °C.

A fan that is simply on or off is noisy and wasteful. This controller behaves like a real thermostat:

  • Proportional speed: the hotter it gets above the set-point, the faster the fan
  • Adjustable set-point from 18 °C to 35 °C on a potentiometer, in 0.5 °C steps
  • Hysteresis, so the fan does not chatter on and off around the set-point
  • Minimum duty, because a fan will not start below a certain PWM
  • Fail-safe: if the sensor is unplugged the fan runs at full speed
  • Non-blocking sensor reads, so the knob and display stay responsive

Use it for a 3D-printer enclosure, an electronics cabinet, a reptile tank vent or a greenhouse.

Components

9 items
NameQtyLink
Arduino Uno R3×1—
DS18B20 waterproof probe (or TO-92 sensor)×1—
4.7 kΩ resistor×1—
10 kΩ potentiometer×1—
L298N motor driver module×1—
12 V DC fan (2-wire, up to about 1 A)×1—
12 V power supply×1—
16x2 LCD with I2C backpack×1—
Breadboard and jumper wires×1—

Need these parts? Get this build sheet by email

Parts list with buy links, plus new builds as they're published. By subscribing you agree to our Privacy Policy. Unsubscribe anytime.

⚠️ Warning

Never power the fan from the Arduino. Give the L298N its own 12 V supply, connect that supply's ground to the Uno's GND, and keep the 12 V wire away from the Uno's 5V pin. If the L298N's 5 V regulator jumper is fitted and you feed more than 12 V, remove the jumper and power the module's 5 V logic separately.

How It Works

The DS18B20

The DS18B20 is a digital sensor on a 1-Wire bus: power, ground and a single data line that needs a 4.7 kΩ pull-up to 5 V. It measures −55 °C to +125 °C with ±0.5 °C accuracy between −10 °C and +85 °C. At 12-bit resolution each conversion takes up to 750 ms.

Most examples call requestTemperatures() and wait those 750 ms, which freezes the sketch. Here we call setWaitForConversion(false), start a conversion, carry on with the knob and display, and read the result 750 ms later. While the next conversion runs, the sensor still holds the previous result, so a read never returns garbage.

Speed Control with the L298N

The L298N is an H-bridge. IN1 and IN2 choose the direction and ENA switches the output on and off. Feeding ENA a PWM signal with analogWrite() sets the average voltage the fan sees, which sets its speed.

Temperature vs set-pointFan statePWM duty (0-255)
More than 0.5 °C belowOff0
At the set-pointOn, slow90 (about 35 %)
2.5 °C aboveMediumabout 172 (68 %)
5 °C or more aboveFull255 (100 %)
Sensor missingFail-safe255

The 0.5 °C hysteresis means a fan that switched on at 25.0 °C stays on until the temperature falls below 24.5 °C. Without it the fan would flick on and off every time the reading wobbled across the set-point.

The L298N is an old bipolar design that loses about 2 V across its switches, so a 12 V supply gives the fan roughly 10 V at full duty. That is fine for a fan. For a more efficient build, a logic-level MOSFET such as the IRLZ44N with a flyback diode does the same job with almost no loss.

Try It in the Simulator First

The circuit below runs this exact sketch. Click the DS18B20 and raise its temperature above the set-point, then turn the knob, and watch the LCD and the fan speed follow. Open the Serial Plotter to see temperature, set-point and fan percentage as live graphs.

Try it in the simulator

Wiring

PartPart pinConnects to
DS18B20VDD (red)Uno 5V
DS18B20GND (black)Uno GND
DS18B20DQ (yellow)Uno D2
4.7 kΩ resistorBetween DQ and VDDPull-up
PotentiometerOuter pins5V and GND
PotentiometerWiperUno A0
L298NENA (remove the ENA jumper)Uno D9
L298NIN1 / IN2Uno D8 / D7
L298NOUT1 / OUT2Fan + / Fan −
L298N12V / GND12 V supply + / −
L298NGNDAlso to Uno GND
LCD I2CVCC / GND / SDA / SCL5V / GND / A4 / A5

The ENA pin on most L298N boards has a jumper that ties it to 5 V for full speed. Pull that jumper off and connect ENA to D9, or PWM will have no effect.

The Code

Install OneWire by Paul Stoffregen, DallasTemperature by Miles Burton and LiquidCrystal I2C from the Library Manager.

C++

Key Parts of the Code Explained

CodeWhat it does
sensors.setWaitForConversion(false)Starts conversions without blocking loop() for 750 ms
now - conversionStarted >= CONVERSION_MSReads the result once the conversion time has passed, then starts the next
t != DEVICE_DISCONNECTED_CDetects a missing or faulty sensor (the library returns −127)
readSetPoint()Maps the 0-1023 knob reading to 18-35 °C, rounded to 0.5 °C
fanDutyFor()Applies the hysteresis and the linear ramp from MIN_DUTY to 255
driveFan()Sets IN1 HIGH, IN2 LOW and writes the PWM to ENA
Serial.print(...,',')Prints CSV that the Serial Plotter graphs directly

Tune MIN_DUTY for your fan: lower it until the fan just fails to start from rest, then add about 20.

Upload and Test

  1. Upload, open the Serial Monitor at 9600 baud and check the first line reads temp_C,setpoint_C,fan_pct.
  2. Turn the knob fully anticlockwise (18 °C). The fan should run, because room temperature is above 18 °C.
  3. Turn it fully clockwise (35 °C). The fan stops.
  4. Set it just above room temperature, then hold the probe in your hand and watch the fan start and speed up.
CheckExpected result
LCD line 1Temp 23.5°C updating about once a second
LCD line 2Set25.0C Fan 0% below the set-point
Probe warmed 3 °C above setFan around 75 %
Probe unpluggedSENSOR ERROR, fan at 100 %
Probe back inNormal readings within a second

To check calibration, put the probe in a glass of iced water stirred for a minute. It should read close to 0 °C; a constant offset can be subtracted in code.

Troubleshooting

ProblemLikely causeFix
Always −127 °C / SENSOR ERRORMissing pull-up or wrong pinFit 4.7 kΩ between DQ and 5 V; check DQ is on D2
Reads 85 °CRead before the first conversion finishedLeave at least 750 ms after requestTemperatures()
Fan never changes speedENA jumper still fittedRemove the jumper, wire ENA to D9
Fan hums but does not startDuty too lowRaise MIN_DUTY
Fan whinesPWM at 490 Hz is audible in some fansNormal; a MOSFET driver with 25 kHz PWM is quieter
Uno resets when the fan startsNo common ground or shared supply noiseSeparate 12 V supply, grounds joined at one point
Reading jumps by several degreesLong unshielded probe cableUse the waterproof probe or a 2.2 kΩ pull-up on long runs

Make It Your Own

  1. Two fans: use the L298N's second channel (ENB, IN3, IN4) for an intake fan.
  2. Remember the set-point: store it in EEPROM and use buttons instead of a knob.
  3. Quieter PWM: drive a MOSFET from Timer1 at 25 kHz, the standard for 4-pin PC fans.
  4. Several probes: put more DS18B20s on the same pin and cool by the hottest one.
  5. IoT: move to an ESP32 and publish temperature and fan speed over MQTT.

FAQ

Why does the DS18B20 need a 4.7 kΩ resistor?

The 1-Wire data line is open-drain: devices can only pull it LOW. The resistor pulls it back HIGH between bits. Without it every read fails and the library returns −127 °C.

Can I power a 12 V fan from the Arduino?

No. The Uno cannot supply 12 V or the current a fan needs. Use a separate 12 V supply through the L298N or a MOSFET and connect the grounds.

Why does the fan not turn on at low PWM?

A DC fan needs a minimum voltage to overcome friction when starting. The sketch never drives it below MIN_DUTY while it is on.

What is hysteresis and why use it?

It is a small gap between the switch-on and switch-off temperatures. It stops the fan chattering on and off when the temperature sits right at the set-point.

Can I use a 4-pin PC fan?

Yes. Power it from 12 V permanently and feed its PWM wire a 25 kHz signal from the Arduino instead of using the L298N.

What You Learned

You read a 1-Wire sensor without blocking, controlled motor speed with PWM through an H-bridge and built proper thermostat behaviour with a ramp, hysteresis and a fail-safe. For more on PWM-driven outputs see the gesture controlled robotic arm, and for current limits on each pin see the Arduino Uno pinout guide.

Topicsarduino-unods18b20l298npwmfanthermostattemperaturelcd1602

Questions & Answers