Learn & build · Electronics · Part II · Chapter 23

Design your own: a thermostat fan

Six builds ago you were told where every leg goes. This chapter asks you to do the two things a designer does without being told. First, read someone else's schematic and find what's wrong with it, which is also how you'll catch your own mistakes. Then design a whole project from a one-paragraph spec, choosing every part yourself, with the answers folded away so you can check each decision after you've made it.

Before you start. Everything here was taught in Part II: the steps and schematic reading (17), choosing regulators (18), levels and floating inputs (19), sensor to switch (20), signals and datasheet circuits (21), and the microcontroller board (22), plus motors and flyback (chapter 14) and KiCad (chapter 15).

1. The design review

Each of these six schematics has one mistake, of the kind every chapter of Part II warned about. Some of them still simulate happily: a simulator only knows what its models tell it, which is part of the lesson. Click the part that's wrong, or its button. Hover the live ones for voltages: the evidence is often right there.

🎯 Challenge: find all six. 0 of 6 found.

A design review is the same checks you'd run on your own design: every part's job, every value against its datasheet, every input with a defined voltage, every coil with a path for its current, every address and every limit.

2. The spec

A thermostat fan. A 12 V computer fan (about 0.2 A) runs when the room is warmer than a temperature you set, and stops when it's cooler, without chattering around the threshold. The temperature can be read out (on a display or over Wi-Fi). Everything runs from one 12 V adapter.

From here on, every step is yours. Each question below is one decision; think it through (better: write it down), then open the answer. The answers are one good design, not the only one.

Step 1: the goal, as something you could test

Rewrite the spec as one sentence you could check with a thermometer and a stopwatch.

For example: "With the setpoint at 28 °C, the fan starts within ten seconds of the sensor reading above 28.5 °C and stops within ten seconds of it reading below 27.5 °C, powered only by the 12 V adapter." The two numbers either side of the setpoint are the hysteresis (chapter 20), now a requirement instead of a fix.

Step 2: the blocks

Chapter 20's dusk light did "sensor, threshold, switch" with a comparator and no code. Why would you use a microcontroller here instead?

Without the read-out, chapter 20's circuit with an NTC thermistor in place of the LDR would do, with no code at all; that's worth knowing, and it's chapter 20's "make it yours". With it, the blocks are: 12 V in; a 5 V supply for the board; a microcontroller (chapter 22's ESP32); a temperature sensor; a switch for the fan.

Step 3: the supply

The ESP32 board takes 5 V on its 5V pin and bursts to about a quarter of an amp on Wi-Fi. From 12 V, which regulator?

A 7805 would burn \((12 - 5) \times 0.25 \approx 1.75\) W in the bursts, about 95 °C above the room in a bare TO-220 (chapter 18's chooser). A buck module (chapter 10) wastes a fraction of that. Set its output with a meter before connecting the board: most modules have a trim pot and leave the factory at some other voltage. And add chapter 22's bulk capacitor (470 µF) on the 5 V side against brown-outs.

Step 4: the sensor

BME280 on I²C, or an NTC thermistor in a divider on an analog pin? What decides it?

Both work. The BME280 (chapter 22) gives calibrated degrees over two shared wires and humidity for free; the thermistor (chapter 11) costs cents, needs one ADC1 pin and chapter 11's arithmetic. This design uses the BME280, at 0x76, powered from the board's 3V3 pin, exactly as chapter 22 wired it. Either way, mount it where it measures the room, not the fan's exhaust or the warm regulator (chapter 20's "a sensor that sees what it controls").

Step 5: the fan switch

The ESP32 pin gives 3.3 V. Chapter 20 switched an amp with an IRLZ44N. Will it do here?

"Logic level" meant 5 V logic when the IRLZ44N was designed: its table stops at 4 V (0.035 Ω). Chapter 4 §4's lesson, and chapter 5's "design with the min and max columns": below the last promised row you're on your own. The AO3400A promises under 48 mΩ at 2.5 V, so at 3.3 V it's fully on: \(0.2^2 \times 0.048 \approx 2\) mW. It's a tiny SOT-23 part; on a breadboard, solder it to a SOT-23 adapter board first.

Three small parts go around the MOSFET. Name each one's job before you open this: a 100 Ω resistor in series with the gate, a 100 kΩ from gate to ground, and a diode across the fan.

100 Ω in series: the gate is a small capacitor; each switch, the pin charges or empties it, and the resistor caps that current at 3.3 V / 100 Ω = 33 mA, under the 40 mA the ESP32's datasheet gives a pin. 100 kΩ to ground: while the ESP32 resets and boots, its pins float (chapter 13), and a floating gate can turn the fan on at random; the pull-down holds it off until the program takes over. The diode: a fan is a motor, a coil (chapter 14); every time the MOSFET turns off, many thousands of times a second with PWM, the coil's current needs a path, or the drain spikes far above 12 V (chapter 4 §3). A 1N5819 Schottky is fast enough for that.

Step 6: pins and grounds

Which ESP32 pin drives the fan, and what has to be true of the grounds?

Any output-capable pin that isn't a strapping, flash or UART pin (chapter 22 §2): GPIO25 here, with PWM at 25 kHz so the fan's speed can follow the temperature without an audible whine (chapter 14 §3). I²C stays on GPIO21 and 22. And the grounds must be one: the 12 V adapter's −, the buck module's output −, the ESP32's GND and the MOSFET's source. The gate voltage only means something relative to the source; with separate grounds, the fan would do whatever the stray voltage between them says.

3. The design, part by part

Here's the result of those decisions. The boards (the buck module, the ESP32, the sensor) are drawn as boxes with labels, as in chapter 22, and the drawing isn't simulated: the lab has no microcontroller. Pick any part for the reasoning behind it.

The one analog part, the fan driver, can be simulated on its own, with a 3.3 V square wave standing in for GPIO25 (slowed to 1 kHz so you can watch). Hover the drain while it switches, then take out D1.

🎯 Challenge: take out D1 and find how high the drain goes when the MOSFET turns off.

Where it breaks: the simulator's 611 V

Without D1, the simulator shows the drain flying to about 611 V at each turn-off, because its MOSFET model has no breakdown voltage: it shows how hard the coil pushes, with nothing to stop it. A real AO3400A is rated for 30 V; past that its drain breaks down and absorbs the energy as heat (chapter 4 §3's avalanche), which it survives a limited number of times. Either way the number is a warning, not a prediction: the real part fails somewhere between.

4. The fan driver on a breadboard

Your layout, your choice; here's one. The drain lives in two strips joined across the gap by the green jumper: the MOSFET's adapter board is in the bottom half, the fan's wire and D1 in the top. This build's own breadboard check caught exactly that jumper missing while this chapter was being written: it's the easiest wire to forget.

5. The shopping list

RefPartWhy this one
U1Buck module (adjustable or 5 V)12 → 5 V at a fraction of a 7805's heat. Set 5.0 V before connecting.
C1470 µF electrolytic, 10 V+Bulk capacitance against Wi-Fi brown-outs.
U2ESP32 dev boardThe brains (chapter 22).
U3BME280 breakoutThe temperature, I²C at 0x76.
Q1AO3400A on a SOT-23 adapterUnder 48 mΩ at a 2.5 V gate; 30 V; ±12 V gate.
R1, R2100 Ω, 100 kΩ ¼ WGate current limit; gate pull-down for boot.
D11N5819Flyback, fast enough for 25 kHz PWM.
M112 V fan, ~0.2 A (2-wire)A 4-wire PWM fan takes PWM on its own wire instead: read its spec.
BAT112 V adapter, 1 AFan + buck input with a margin.

6. Your checklist

Write the power-up order for this build, chapter 17 style: what do you connect and measure first, second, third, and what reading must you see before going on?

  1. Buck module alone on 12 V, nothing on its output: adjust to 5.0 V.
  2. Add C1 and the ESP32 board (no sensor, no fan): its 3V3 pin reads 3.3 V; upload the I²C scanner.
  3. Add the BME280: the scanner finds 0x76; the lines idle at 3.3 V.
  4. Add the fan driver without the fan: the gate reads 0 V while the board resets (that's R2), and 3.3 V when the program turns GPIO25 on.
  5. Add the fan: it runs when the program says so; the MOSFET stays cool; the drain, on a scope, stays near 12 V at turn-off (D1 working).
  6. Last, the thermostat program, with a test: warm the sensor with your hand and time the fan against your step 1 sentence.

7. From breadboard to board

A breadboard proves the design; a printed board makes it last. Chapter 15's KiCad steps apply as they are: draw the schematic you just designed, choose footprints (the AO3400A is SOT-23, so on a PCB it needs no adapter), place the parts (decoupling and bulk capacitors by the chips they serve, the fan's connector and the 12 V jack at the edge), and size the traces: the fan's 0.2 A needs only a thin trace by chapter 15's rule, but the 12 V input feeds everything, so give it more. Pour a ground plane, run the design rules check, order five boards, and solder your first one with chapter 15's "heat the joint, not the solder."

8. Where to go from here

That's Part II. You can read a schematic, pick parts from datasheets, lay them out, check them, and find what's wrong. The next projects are yours: the build logs on this site (a USB-C power bank, an ESP32 remote, a solar weather node) use exactly these steps, and the circuit lab has every part from Part II to try a design before you buy anything.

Previous: A microcontroller board · All chapters: Learn & build.

Last updated