Best way to control a 12v PC fan with raspberry pi?


I am doing a project at the moment to air my cupboard with a raspberry pi, battery and 12 PC fan. What is the best way to control the fan from the raspberry pi (considering it takes 12v) ?


Funny you should ask this. I’m currently trying to acquire some 12V PC fans to hack with, and will have to figure out how to control them.

I’m not sure exactly how they’re controlled- be it variable voltage or current, or a constant supply and a control signal to an internal driver chip- but it could be that something as simple as a Darlington transistor pair would suffice (as seen on, for example, Explorer pHAT) with a PWM signal for varying the voltage.

The fan may run adequately at 5v, too. I might see if I can dig one up for testing.


I’ve got stacks of them in my box of misc salvage and wanted to use one for a similar purpose to the OP, I’ll watch this space :)


After a little research, it looks like they’re voltage-controlled. The 24v tolerant sinking outputs on Automation pHAT should do the trick, although for speed control you’d want to set up a PWM signal and I can’t recall if they’re connected to any good PWM pins. Software might be okay though, depending on the use case :D

I’ve found a 12v fan- thanks for the offer @oatmaster :D we have a similar stash of salvage! - I’m going to eat something (not the fan) and then hook it up to Automation pHAT.


Rigged it up with Automation HAT and used my bench power supply to source 12v. Works pretty well! I could even read the Hall effect sensor and get some idea what speed the fan is running at- albeit I think the bounetime I’d set in RPi.GPIO might have been too high.

It’s chugging away happily on my desk.


Can I power the automation phat at 12v from the pi 0?


I think you’d only be able to do it via a step-up converter and then you might not be able to get enough current to drive the fan properly. What you may be able to do, however, is use a single 12V power supply and step that down to provide power to the Pi Zero so you’d still only need a single power source.

(though let’s await the experts :) )

EDIT: or possibly a 9V battery?


I’d say if you don’t fully understand the implications of mixing a 12v supply with the Pi’s 3.3v and 5v supplies you’re probably best just running the fan at 5v- although in my setup it really takes the edge off it- if the speed readings from my fan are correct, 5v seems to be about 2100RPM and 12v 2700RPM (I think they’re a little wrong, though).

While - as @oatmaster suggests - you could step up the 5v from the Pi to provide a 12v power supply for the fan, he’s right that you’d probably be better off splicing a 12v power supply and stepping that down to 5v.

Using my flaky readings, 9v hits about 2300RPM.


Having never used the automation phat before what will I need to do to use the fan whole powering the phat via battery?


Are these the type used in PCs?
If so, they won’t play nicely with PWM…
PC fans tend to use brushless motors, and have a little circuit board of cleverness within the hub. These switch the coils on and off appropriately to make a circular magnet (attached to the fan) rotate. If you apply PWM to this, you’re effectively switching and electronic switching device on and off really quickly!
You should take one apart to see regardless :-)

There are some fancy fans that have four wires; power, rotational speed feedback, and a PWM input. One of these types would be absolutely perfect for this type of purpose.



Woo, I’ve got a fancy one then. I’d assume that the rotational speed feedback wire is an analogue voltage of 5v rather than 12v.


Mine only has a black wire and a brown wire


I have no idea about the feedback signal as I never used it (or measured it). The set-up I had with a fan like yours was just a microcontroller outputting a 5V PWM to the 12V fan’s PWM input, based on temperature readings.
I had around 4 staged speeds though as the fan was noisy at some (a bit like when a car’s tyre needs balancing!), so be prepared for that.
I look forward to seeing how you get on :-)


Hi Benhackerman,

That means in all likelihood that you wouldn’t be able to easily control the speed of that fan.

It does work a bit; but the results are unpredictable as you’re essentially turning an electronic device on and off repeatedly at a rapid rate! If the fan was cheap or free, and reliability isn’t an issue then it might be worth just giving it a try!



The setup only needs to be a slow airflow that can be put on and off easily


Ah, that makes things much easier then. (Depending on what conditions you want to monitor as a case for turning the fan on or off, this should be relatively straightforward).


The conditions are a timer and maybe a web sever


My wiring solution was just stuffing male-to-male jump wire into the holes on the fan connector, and then into the screw terminals of the Automation HAT. I think if you were going to do things properly, you’d strip and tin the wires to connect directly, or fine a little connector with bare wire tails.

@RacingtoMars your point about the built-in motor controller certainly sheds some light on why I was getting my PWM signal superimposed on the hall-effect sensor output. In retrospect, I don’t know where I thought the sensor was going to be getting power :D

If full tilt isn’t essential, then I guess it becomes a case of picking a constant voltage that drives the fan at the appropriate speed.

For switching on/off you could use either the sinking output driver (ULN2003A) or the relay on Automation HAT. Or just any similar board with a relay.

Wire your fan up as normal to the power supply, but route the positive side through the relay’s COM and Normally-Open terminals.

How to wire fan (with ground and 12v cables) and power to automation phat?

Well, I’m a right 'nana @gadgetoid — I looked at your post and didn’t even make the connection with what I said about the fans!

You should undo one to have a look inside though :-)


Would you be able to do a picture to demonstrate what I need to do?