orpattager
Posts: 5
Joined: Fri Aug 15, 2014 9:26 pm

PC fan control with hardware PWM: what circuit?

Fri Aug 15, 2014 10:22 pm

Hello,

I would like to control the fan speed of a 4 pin pc fan using PWM. In the last couple of days, I wrote a small python script which generates a hardware PWM signal on GPIO 18. I tested the script with a connected LED and everything seems to work well (brightness changes with a changing duty cycle).

So, after the testing I would now like to control the fan speed of the PC fan. And being a total electronic noob, I have no idea how to do this correctly.
VCC and ground from the pc fan are connected to an external 12v power supply. I don't want to measure RPM, so I do nothing with the tacho signal.
What exactly should I do with the PWM cable from the pc fan?
Connect it directly to GPIO18 on the raspberry?
Do I have to connect anything to the ground on the raspberry?
Do I need resistors or transistors for the circuit?

I already spent quite a few hours on google searching for instructions on how to do this, but I din't find anything. All the sources I found only explained the software side, but none explained the exact wiring.
So, I'd be really thankful for any answers! :)

drgeoff
Posts: 8605
Joined: Wed Jan 25, 2012 6:39 pm

Re: PC fan control with hardware PWM: what circuit?

Sat Aug 16, 2014 9:20 am

The fan will probably need more current than a GPIO can (safely) supply. You should use a transistor or FET between the RPi and fan. http://elinux.org/RPi_GPIO_Interface_Circuits

orpattager
Posts: 5
Joined: Fri Aug 15, 2014 9:26 pm

Re: PC fan control with hardware PWM: what circuit?

Sat Aug 16, 2014 7:29 pm

Thanks for your suggestion! :)

I did some googling and discovered that the current of the PWM signal should be between 5 and 8mA (http://formfactors.org/developer/specs/ ... M_Spec.pdf). Google also told me that a single GPIO on the raspberry can output up to 16mA.
So, in my understanding the current should be enough, shouldn't it? :?

User avatar
rpdom
Posts: 12856
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: PC fan control with hardware PWM: what circuit?

Sat Aug 16, 2014 7:40 pm

The current is only part of the issue. What voltage does the fan's PWM input need?
[edit] From the spec it looks like it will work at 3.3V

boyoh
Posts: 1175
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: PC fan control with hardware PWM: what circuit?

Sat Aug 16, 2014 8:19 pm

orpattager wrote:Hello,

I would like to control the fan speed of a 4 pin pc fan using PWM. In the last couple of days, I wrote a small python script which generates a hardware PWM signal on GPIO 18. I tested the script with a connected LED and everything seems to work well (brightness changes with a changing duty cycle).

So, after the testing I would now like to control the fan speed of the PC fan. And being a total electronic noob, I have no idea how to do this correctly.
VCC and ground from the pc fan are connected to an external 12v power supply. I don't want to measure RPM, so I do nothing with the tacho signal.
What exactly should I do with the PWM cable from the pc fan?
Connect it directly to GPIO18 on the raspberry?


Us
Do I have to connect anything to the ground on the raspberry?
Do I need resistors or transistors for the circuit?

I already spent quite a few hours on google searching for instructions on how to do this, but I din't find anything. All the sources I found only explained the software side, but none explained the exact wiring.
So, I'd be really thankful for any answers! :)

Using a Opto Isolator might do the control of the
PWM to the Fan, I might be wrong I think the fan PWM
I/P voltage is 3.3v, ?
Using the Opto Isolator , you will have no need to
Common the ov- between the Pi and the fan power
Supply


12vdc----------------------Fan
0v---------------------------Fan

GPIO o/p PWM-----( 130R )-----( Opto a LED k)----0v-Pi
v v v v
3.3v-----------------------------( Opto e Transistor c )---- v----- Fan PWM I /P
---------------------------------------------------------------------47K--------------------
----------------------------------------------------------------------0V---
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

drgeoff
Posts: 8605
Joined: Wed Jan 25, 2012 6:39 pm

Re: PC fan control with hardware PWM: what circuit?

Sat Aug 16, 2014 8:36 pm

orpattager wrote:Thanks for your suggestion! :)

I did some googling and discovered that the current of the PWM signal should be between 5 and 8mA (http://formfactors.org/developer/specs/ ... M_Spec.pdf). Google also told me that a single GPIO on the raspberry can output up to 16mA.
So, in my understanding the current should be enough, shouldn't it? :?
Yes, you should be able to connect the control input of the fan directly to the RPi GPIO.

I hadn't understood earlier that the fan already contains a "drive" circuit.

boyoh
Posts: 1175
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: PC fan control with hardware PWM: what circuit?

Sat Aug 16, 2014 10:14 pm

drgeoff wrote:
orpattager wrote:Thanks for your suggestion! :)

I did some googling and discovered that the current of the PWM signal should be between 5 and 8mA (http://formfactors.org/developer/specs/ ... M_Spec.pdf). Google also told me that a single GPIO on the raspberry can output up to 16mA.
So, in my understanding the current should be enough, shouldn't it? :?
Yes, you should be able to connect the control input of the fan directly to the RPi GPIO.

I hadn't understood earlier that the fan already contains a "drive" circuit.


I would do a bit of research on noise feeding
Back to the Pi from the fan motor inductive
Windings when using a direct connection
To the PWM IN/PUT
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

drgeoff
Posts: 8605
Joined: Wed Jan 25, 2012 6:39 pm

Re: PC fan control with hardware PWM: what circuit?

Sun Aug 17, 2014 9:28 am

boyoh wrote:
I would do a bit of research on noise feeding
Back to the Pi from the fan motor inductive
Windings when using a direct connection
To the PWM IN/PUT
Au contraire, mon ami. It is you who needs to do some research. Please explain how you think noise from a motor running from a 12 volt supply is going to feedback to a control signal input which has a pullup to 3.3 or 5 volts within the fan and sources a maximum of 5 mA.

Three points for the OP:

1. The fan spec calls for the control input of the fan to have a pull resistor to a maximum of 5.25 volts within the fan, but new designs are strongly encouraged to implement a 3.3 volt pullup. 3.3 is ideal for the RPi. Anything above that has the potential to damage a RPi. You need to be sure that the fan is "3.3".

2. The GPIO must be set to "open-drain" mode.

3. Note that the PWM frequency is to be in the range 21 kHz to 25 kHz.

orpattager
Posts: 5
Joined: Fri Aug 15, 2014 9:26 pm

Re: PC fan control with hardware PWM: what circuit?

Sun Aug 17, 2014 1:49 pm

It worked! :D Thanks for all the suggestions, everybody!

I connected the PWM cable from the fan to GPIO 18 and the ground from the fan to the ground on the raspberry.

Here's the python script I used in case anybody would like to do the same thing in the future:

Code: Select all

import wiringpi2 as wiringpi  
import time

wiringpi.pwmSetMode(0) # PWM_MODE_MS = 0

wiringpi.wiringPiSetupGpio()  

wiringpi.pinMode(18, 2)      # pwm only works on GPIO port 18  

wiringpi.pwmSetClock(6)  # this parameters correspond to 25kHz
wiringpi.pwmSetRange(128)

wiringpi.pwmWrite(18, 0)   # minimum RPM
time.sleep(1)
wiringpi.pwmWrite(18, 128)  # maximum RPM
time.sleep(1)

wiringpi.pwmWrite(18, 0)

boyoh
Posts: 1175
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: PC fan control with hardware PWM: what circuit?

Sun Aug 17, 2014 4:29 pm

drgeoff wrote:
boyoh wrote:
I would do a bit of research on noise feeding
Back to the Pi from the fan motor inductive
Windings when using a direct connection
To the PWM IN/PUT
Au contraire, mon ami. It is you who needs to do some research. Please explain how you think noise from a motor running from a 12 volt supply is going to feedback to a control signal input which has a pullup to 3.3 or 5 volts within the fan and sources a maximum of 5 mA.

Three points for the OP:

My Dear drgeolf




,



1. The fan spec calls for the control input of the fan to have a pull resistor to a maximum of 5.25 volts within the fan, but new designs are strongly encouraged to implement a 3.3 volt pullup. 3.3 is ideal for the RPi. Anything above that has the potential to damage a RPi. You need to be sure that the fan is "3.3".

2. The GPIO must be set to "open-drain" mode.

3. Note that the PWM frequency is to be in the range 21 kHz to 25 kHz.
My Dear drgeolf

I admire your copying skills off the Internet on this project
All correct WORD FOR WORD.
I notice you live of other posters Ideas, and try to modefie them
Remember WE all get things wrong some times,
But some of us don't shout them down
Just don't be so brash and ignorant

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

brueni
Posts: 4
Joined: Sun Nov 16, 2014 5:08 pm

Re: PC fan control with hardware PWM: what circuit?

Sun Nov 16, 2014 5:15 pm

Hello orpattager

I'm currently struggling with the same thing as you did, trying to control a 12V fan through PWM. Your script works like a charm when i connect a LED instead of the FAN. But maybe i do something wrong while connecting the fan. You wrote, that you've connected the PWM cable from the fan to pin 18, and the ground to the ground of the raspi. I did that too, and connected the 12V cable from the fan to an external 12V source, but nothing happens. Do i have to connect the ground from the external powersupply too?
Hope you can give me a hint here, it would be much appreciated.

Mike

orpattager
Posts: 5
Joined: Fri Aug 15, 2014 9:26 pm

Re: PC fan control with hardware PWM: what circuit?

Sun Nov 16, 2014 7:12 pm

Do i have to connect the ground from the external powersupply too?
I don't really feel capable of giving advice about electronic things, but yes, my setup only worked once I used a common ground between the RPi and the 12V power supply.
I did that too, and connected the 12V cable from the fan to an external 12V source, but nothing happens.
"Nothing" as in "the fan didn't slow down", or nothing as in "the fan didn't turn at all"?

And just to be sure: you do have a fan with 4 pins, right? ;)

brueni
Posts: 4
Joined: Sun Nov 16, 2014 5:08 pm

Re: PC fan control with hardware PWM: what circuit?

Sun Nov 16, 2014 7:44 pm

Thanks for your quick reply :)
OK, i tried it now with common ground, but still the same.
My 'nothing' :) was meant as 'the fan didn't slow down'. As soon as 12v and GND are connected, it goes to full speed, but nothing changes as i run your script. Also modified your script with different pwm values, and different times, so it should slow down for longer timespans, but does not change anything.
Exactly, i have a 4pin fan: 12v, GND, RPM-Signal (i'm not using that) and PWM.

orpattager
Posts: 5
Joined: Fri Aug 15, 2014 9:26 pm

Re: PC fan control with hardware PWM: what circuit?

Tue Nov 18, 2014 6:12 pm

My setup has been running fine for the last 2 or 3 months, so what you want to do is definitely doable... but I've honestly no idea why it doesn't work for you. :?

As far as I can tell, your wiring seems to be correct... maybe anybody else has any tips?

brueni
Posts: 4
Joined: Sun Nov 16, 2014 5:08 pm

Re: PC fan control with hardware PWM: what circuit?

Tue Nov 18, 2014 6:19 pm

Yeah it's strange, i'm sure this should work... In the meantime, i've found another 4pin PWM fan from another brand, just to see if it's something specific with my fan, but still no luck.
Anyway, thanks for your help, appreciate that. :) At least i'm able to control the fan speed with PWM through a transistor directly on the 12v line of the fan. (But your version would be more sleek because it does not need an external circuit ;) )

chsbrgr
Posts: 1
Joined: Thu Mar 12, 2015 7:56 pm

Re: PC fan control with hardware PWM: what circuit?

Thu Mar 12, 2015 8:00 pm

Hello orpattager

I'm currently struggling with the same thing as you did, trying to control a 12V fan through PWM. Your script works like a charm when i connect a LED instead of the FAN. But maybe i do something wrong while connecting the fan. You wrote, that you've connected the PWM cable from the fan to pin 18, and the ground to the ground of the raspi. I did that too, and connected the 12V cable from the fan to an external 12V source, but nothing happens. Do i have to connect the ground from the external powersupply too?
Hope you can give me a hint here, it would be much appreciated.

Mike
Mike,

I'm currently researching controlling a fan through GPIO and PWM wire, and saw this thread, hopefully i can help you with this. GPIO18 is different than Pin #18. Please refer to this image showing the pin # and their GPIO #, GPIO18 is actually physical pin #12.

brueni
Posts: 4
Joined: Sun Nov 16, 2014 5:08 pm

Re: PC fan control with hardware PWM: what circuit?

Sun May 10, 2015 12:29 pm

Sorry for my (very) late reply, somehow forgot about this topic. I'm actually aware about the difference between the GPIO-# and Pin-#... but its possible that i forgot about this when i tried it :D
I'll give it a try the next time i'm tinkering, thanks for the hint!

Mike

sabareeshchambayil
Posts: 26
Joined: Wed Aug 05, 2015 1:59 pm
Location: Kerala
Contact: Yahoo Messenger

Re: PC fan control with hardware PWM: what circuit?

Thu Aug 06, 2015 3:36 pm

Can i use this with 5 volt fan and how the fan is working, i mean is it working all time or work according to temperature

CodeOhms
Posts: 5
Joined: Sat Jul 07, 2018 8:27 am

Re: PC fan control with hardware PWM: what circuit?

Thu Jul 12, 2018 6:53 am

Just thought I'd add a shameless self promo :roll:.
Lightweight program in C++, which automatically installs a systemd file for starting at boot. Can be customised for different speeds at different temps.
It has a script for easy install, and soon a deb installer. Also includes a description on how to wire a fan to be compatible.
viewtopic.php?f=41&t=217607&p=1339897#p1339897.

User avatar
DougieLawson
Posts: 34098
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: PC fan control with hardware PWM: what circuit?

Thu Jul 12, 2018 7:02 am

Did you have to spam this same stuff all over the forum? One post would have been enough. It's been reported to the moderators.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Beginners”