I built a bathroom fan that knows when to stop
Smartfan is an open-source fan controller on a Raspberry Pi Pico 2 W. It learns what normal looks like in your bathroom, runs the fan for light, humidity and odor until the room is dry, and then stops.
Every bathroom fan I've owned had two settings: off, and running long after anyone needed it. The "smart" ones add a humidity sensor and a number, and the number is always wrong.
This one started with a friend's request: a fan that clears the humidity and shuts itself off when the lights go off, but not until the humidity is gone. I added a gas sensor on top, so it also stays on until odors clear. It decides all of that for itself, and today it's open source: Smartfan.
A number is the wrong question
The obvious design is a threshold: fan on above 60% humidity. It falls apart on the first muggy August day, when the room sits at 65% with nobody in it and the fan never stops. Humidity isn't the signal. The change in humidity is.
So Smartfan learns a slow baseline for the room and reacts to the rise above it. A real shower in my bathroom went from a 53.1% baseline to 91.6% in four minutes, and it's the rise above that baseline that starts the fan, 8 points by default, not the 91.6. It also decides when to stop: the fan runs until the room is back near that baseline, not until a short timer runs out. There's a hard cap as a backstop, 45 minutes by default. Damp that lingers after a shower is what mold needs, so stopping early is the one thing a bathroom fan shouldn't do.
The same idea runs through all three sensors, and every one of them taught me something.
The sun is brighter than my bathroom light
The room light is the occupancy signal: light on, someone's in. I calibrated it, set a threshold, and moved on. Then the fan ran for 175 minutes in an empty, dry room on a sunny afternoon.
The device's own history told the story. Direct sun reached 85% of the light sensor's range. The ceiling lamp reads 81%. There is no level that separates them, so no threshold could ever have worked. What does separate them is speed: the lamp moves the reading 51,511 counts in a single two-second sample, and the fastest the sky has ever managed in that room is 51. Now the light is found by the step away from a learned ambient level, never by a level, and sunlight doesn't start the fan.
Every threshold in this project turned out to be a baseline in disguise.
Odor, honestly
Light and humidity miss the obvious case: someone uses the bathroom, doesn't shower, and turns the light off on the way out. A Sensirion SGP41 gas sensor covers that, and it is the hardest of the three. It reports raw counts with no absolute meaning, the signal falls as the air gets worse, and humidity moves it so much that 0.3 degrees Celsius of overnight cooling shifts it by a whole trigger threshold. It also takes about half an hour to settle after power-up, longer than any timer I'd have guessed.
So the odor channel learns its own baseline, corrects for humidity, and runs the fan when the air changes, even after the light is off, and then it stops. Odor triggering ships switched off. You give it a threshold once you've watched your own room for a few days. Before writing this I went looking for anyone else doing it. A few commercial fans do sense odor: Vent-Axia sells one in the UK, and some budget fans list an "odor sensor". None of them say how theirs decides when to start or stop. Mine is written down, and the code is public.
What actually went wrong
The parts list is short: a Pico 2 W, three sensors, a fan and a handful of passives. Two of the capacitors came off an old power inverter I'd taken apart, and they've worked fine.
The hard parts weren't where I expected. On the hardware side it was loose wires. A breadboard jumper that's slightly out of its socket doesn't fail outright; it fails sometimes. The fan reads zero rpm, then runs again when you go to look at it. On the software side it was the odor algorithm, which took more rounds of testing than light and humidity put together, for all the reasons above.
It runs itself, and it serves its own app
The Pico serves its own app from flash at fan.local, with a week of history stored on the board. No account, no cloud, no server, and the app is never in the control path: the fan runs its own logic and the page only shows what it reports. A button and two lights handle boost, relearning and Wi-Fi setup with no network at all. It watches its own fan, too: if the tachometer says the blades aren't turning, it raises a fault instead of pretending. That's how I found a spider in it.
If you run Home Assistant, there's an integration you can add through HACS as a custom repository, or plain MQTT with discovery. Every reading and threshold becomes an entity, with a dashboard included.
Updates go over the air, signed, so only my build machine can update the fan, and a bad update rolls itself back. The two files that make recovery possible only ever travel over USB. A recovery path you can break remotely isn't one.
What it doesn't do yet
The bench unit uses a 40 mm, quarter-watt fan. It stirs air; it doesn't extract it. It spent its weeks in the bathroom beside the existing fan, so what it proved was the logic: when to start, when to stop, and why. Moving a bathroom's worth of air is V2's job.
The prototype was there to prove the idea works. V2 is the real thing: a ceiling unit on mains power with a proper 120 mm fan, a printed circuit board in place of the breadboard, and a 3D-printed shroud and mounting parts. The board and the assembly are on the project site. It's designed, not built, so the docs say so in large type, along with the obvious warning that mains wiring can kill you.
Try it
- The live demo: the real app against a simulated fan. A shower happens every seven minutes.
- The docs: the build, setup, Home Assistant, updates, and the measurements behind every threshold.
- The repo: firmware, app and integration under GPL v3, hardware under CERN-OHL-S.
If you build one, I'd like to hear what your bathroom does to the baselines. Mine taught me that the sun is brighter than the bulb. Yours will have its own surprise.
Comments