Build a Temperature-Controlled Fan

A temperature-controlled fan works on a short feedback loop: a sensor measures how hot the surrounding air is, a microcontroller converts that reading into a duty cycle, and a pulse-width-modulated (PWM) signal switches a transistor or MOSFET on and off fast enough to vary the effective voltage the fan sees — and therefore its speed. Below a set temperature the fan idles or stays off; as the temperature climbs, the duty cycle ramps up until the fan runs flat out. None of this needs a microcontroller pin to carry real fan current — the pin only ever switches a driver component that does the heavy lifting.

What parts does a temperature-controlled fan need?

At minimum, the build needs four things: a microcontroller to read the sensor and generate the PWM signal, a temperature sensor, a fan sized for the airflow you want, and a switching component — a transistor, MOSFET, or relay module — between the fan and its power supply. When we asked Compoden's AI build assistant for exactly this, it put together a tray of seven parts around an Arduino Uno: a DS18B20 digital temperature sensor, a 12V 4010 cooling fan, and an IRF540N MOSFET as the switching element, plus a power adapter and wiring. That is a fairly standard shape for this project — a thermistor or DHT11/22 would also have worked as the sensor, and a small MOSFET module or relay would also have worked as the driver, but the assistant chose a 1-wire digital sensor and a discrete N-channel MOSFET for this particular request.

Why can't you drive a fan directly from an Arduino pin?

An Arduino Uno's digital pins are rated to source about 20-40mA each, and the hard absolute maximum for the whole chip is well under an amp shared across all pins. A small DC fan draws far more current than that to start and run, so wiring it straight to a pin will either do nothing or damage the pin trying. There is a second problem beyond current: a spinning DC motor is an inductor, and when the drive current is switched off, the collapsing magnetic field generates a brief high-voltage spike (back-EMF) that can flow back into the switching device. That is why a transistor or MOSFET is used as a middleman — it can be built to handle the fan's real current — and why a flyback (freewheeling) diode across the fan's terminals is standard practice to shunt that kickback safely away from the switch. The Arduino's job shrinks to sending a small, safe signal that tells the MOSFET's gate when to switch.

How does PWM control fan speed?

PWM does not vary voltage directly — it turns the supply to the fan on and off many times per second and varies the fraction of time it is on (the duty cycle). A fan motor's mechanical inertia and the smoothing effect of its winding mean it responds to the average voltage over that cycle rather than to each individual pulse, so a 30% duty cycle behaves roughly like a lower, steady voltage and a 90% duty cycle behaves like most of full voltage. The microcontroller maps the temperature reading onto a duty cycle — off below a low threshold, ramping between the low and high threshold, full speed above the high threshold — and writes that duty cycle to a PWM-capable pin. That pin drives the gate of the MOSFET (or the base of a transistor), which does the actual switching of the fan's higher-current, often higher-voltage supply.

How do you wire it all together?

In the build the assistant generated, the DS18B20 sits on a 1-wire data bus into an Arduino digital pin, with power and ground shared with the board. The IRF540N MOSFET's gate connects to a separate PWM-capable digital pin, its source ties to ground, and its drain switches one side of the fan; the fan's other lead goes to its own 12V supply rather than the Arduino's 5V rail, since a MOSFET on the low side does not add voltage, it only switches what is already there. Two details the assistant flagged on its own, worth repeating rather than skipping: the DS18B20 needs a 4.7kΩ pull-up resistor between its data line and 5V, which was not in the base tray (a resistor variety pack was offered separately to cover it), and the 12V fan needs a 12V source, while the parts list's power adapter only supplies 5V for the Arduino itself — so a separate 12V supply is required and was not bundled into this particular tray. Both are normal, fixable gaps in an auto-generated parts list, not build blockers, but they are exactly the kind of detail worth checking before you order.

Watch it built live

We gave Compoden's AI build assistant this exact prompt: "I want to build a temperature-controlled fan that automatically speeds up as it gets hotter, using an Arduino Uno."

It replied:

"This build creates a fan that automatically adjusts its speed based on temperature. The DS18B20 Temperature Sensor measures how hot it is, and the Arduino Uno R3 CH340G ATmega328P Board uses that information to control the speed of the 12V 4010 Cooling Fan. As the temperature goes up, the fan will spin faster."

On the MOSFET, it explained: "The IRF540N MOSFET is a type of transistor that acts like an electronic switch... This MOSFET is essential because the Arduino cannot directly power the 12V fan, so the MOSFET allows the Arduino's low-power signal to control the fan's higher power." And on the gaps in the tray, it was upfront: "the fan needs 12V, so a separate 12V power source would be needed for the fan, which is not included in this list... the DS18B20 sensor needs a pull-up resistor, which is not included in this parts list and would need to be sourced separately."

Compoden AI build assistant parts tray for a temperature-controlled fan project with Arduino Uno, DS18B20 sensor, 12V fan and IRF540N MOSFET

Watch the firmware get generated

We then took the same build to Compoden's Soldr firmware assistant with a follow-up prompt asking for the full sketch. It produced working Arduino code that reads the DS18B20 over its 1-wire bus on digital pin 2 and drives the IRF540N MOSFET's gate on digital pin 3, a PWM-capable pin. The generated comments spell out the wiring directly in the source: the DS18B20 data pin needs that 4.7kΩ pull-up to 5V, and the MOSFET's source ties to ground while its drain switches the fan. The assistant's own summary of the sketch: "It reads temperature from the DS18B20 sensor and controls the fan speed using PWM via the IRF540N MOSFET. The fan will turn on at 25°C and reach full speed at 35°C, with linear control in between." It also flagged the two libraries the sketch depends on — OneWire and DallasTemperature — installable from the Arduino IDE's Library Manager before flashing.

Soldr firmware assistant generated Arduino code for temperature-controlled fan with DS18B20 sensor and IRF540N MOSFET PWM control

Get everything in this build

These are the exact parts Compoden's AI put in the tray for this build, at current listed prices.

Part Role Price Add to cart
Arduino Uno R3 CH340G ATmega328P Board Microcontroller Rs.230 Add to cart
DS18B20 Temperature Sensor Temperature sensor Rs.40 Add to cart
12V 4010 Cooling Fan Cooling fan Rs.68 Add to cart
IRF540N MOSFET Fan speed controller Rs.60 Add to cart
5V 2A Micro-USB Power Adapter Power supply (Arduino) Rs.140 Add to cart
23 AWG Multi-Strand Breadboard Wire Prototyping wire Rs.10 Add to cart
23 AWG Solid Core Hookup Wire Hookup wire Rs.10 Add to cart
Total Rs.558 Add all 7 to cart

As noted above, this tray does not include a 12V supply for the fan itself or the 4.7kΩ pull-up resistor the DS18B20 needs — the assistant offered a resistor variety pack as an optional add-on for the latter. Check both before you order if you are building this exactly as shown.

Built and Backed by Compoden

Every part in that table is a real, in-stock listing on Compoden, matched by the same AI build assistant that answers on the storefront and in Soldr. The parts tray, the wiring notes, and the firmware shown here all came from a single live prompt each — nothing in this article was assembled by hand after the fact. If you want a different sensor, a quieter fan, or a relay instead of a MOSFET, describing that change to the assistant will rebuild the tray and the firmware around it.

FAQ

Can I use a different temperature sensor, like a thermistor or DHT22, instead of the DS18B20?
Yes. Any of them can report temperature to an Arduino; the DS18B20 was simply what the assistant matched for this prompt because it is a simple 1-wire digital sensor. A thermistor needs an analog pin and some calibration math, while a DHT11/22 gives you humidity as well, at a slower read rate.

Do I need the flyback diode if I'm using a MOSFET module instead of a bare MOSFET?
Many pre-built MOSFET driver modules already include a flyback diode on the board. With a bare MOSFET like the IRF540N used here, adding a diode across the fan's terminals is still good practice to protect the switching device from motor kickback.

Why does the fan need its own 12V supply instead of running off the Arduino?
The Arduino's 5V rail cannot supply the fan's rated 12V or its running current. The MOSFET only switches power on and off — it does not step voltage up — so the fan must be wired to a supply that matches its own voltage rating, with grounds shared between that supply and the Arduino.

What happens if I skip the pull-up resistor on the DS18B20?
The 1-wire data line can float and produce unreliable or missing readings without a pull-up resistor (typically 4.7kΩ) between the data pin and 5V. It is a small, inexpensive addition but not optional for reliable operation.

Back to blog