User avatar
Imperf3kt
Posts: 3381
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

PMW temperature controlled fan

Tue Jun 20, 2017 1:17 am

I'm not pulling any results via searching that are useful to my situation, so I hope someone can point me in the right direction.

Why do I need a fan?
Most of the results I found asked this, and thus were utterly useless.
Why do I need a fan? Because my Pi 3 reaches temperatures I am not comfortable with. The CPU is throttled and a heatsink (of reasonable proportions) is of little use in lessening this.

What I currently have is a 5v 0.15A brushless DC fan piggybacking pin 2 and grounded at pin 30. (pins 1 - 26 are in use).
While this works great, the fan is not needed to be at 100% RPM all the time.
I found a few guides online that will turn it on and off depending on temperatures, but I'd like to tweak it so my fan starts at say 50% speed instead of 100%

I assume I can use pulse width modulation for this in a similar way to operating a DC motor, but how?

I've ordered a few NPN 2N2222 transistors, but what else would I need and how should I approach this?

http://i.imgur.com/ZkvXXpz.jpg
55:55:44:44:4C
52:4C:52:42:41

Ernst
Posts: 1286
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: PMW temperature controlled fan

Tue Jun 20, 2017 8:32 am

The road to insanity is paved with static ip addresses

User avatar
Imperf3kt
Posts: 3381
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: PMW temperature controlled fan

Tue Jun 20, 2017 8:48 am

Thanks! The links look very useful, especially the first.

I'll give them a try when I can.
55:55:44:44:4C
52:4C:52:42:41

Ernst
Posts: 1286
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: PMW temperature controlled fan

Tue Jun 20, 2017 8:53 am

These go together, the first link describes the circuit, the second link shows the diagram.
The only difference is that I do not use the resistor shown in the diagram.
The road to insanity is paved with static ip addresses

User avatar
Imperf3kt
Posts: 3381
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: PMW temperature controlled fan

Tue Jun 20, 2017 1:57 pm

I'm afraid its been over ten years since I last read a circuit diagram.

Would I be right in assuming GPIO refers to a selected GPIO header, this connects to a 100k resistor that is grounded and then goes to the transistor.
The transistor is grounded on one of the Negative legs (3?) Connected to the GPIO on the middle and the other goes to the fan and subsequently to the 5v rail.
55:55:44:44:4C
52:4C:52:42:41

Ernst
Posts: 1286
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: PMW temperature controlled fan

Tue Jun 20, 2017 2:02 pm

Yes. except that I do not use the [pulldown] resistor.
The road to insanity is paved with static ip addresses

User avatar
Imperf3kt
Posts: 3381
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: PMW temperature controlled fan

Mon Jun 26, 2017 2:04 am

I am having some trouble getting your script to work.
I am using pin 12 instead of 18, but otherwise everything is identical.

I can turn the fan on or off with no problems, but if I use the full script provided in the first post (with only the pin number changed) nothing happens. The fan won't even start much less adjust its speed. I tried lowering the script to trigger at a rediculous temperature like 20 degrees, just to make sure it actually did something, but it still does nothing.

Any further information about the script? I only understand about four lines in it.
55:55:44:44:4C
52:4C:52:42:41

Ernst
Posts: 1286
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: PMW temperature controlled fan

Mon Jun 26, 2017 8:17 am

I have just now done a successful test with pin 12.
what I suggest is that you do the following:
1)check your wiring.

Code: Select all

[email protected]:~ $ gpio readall
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | OUT  | GPIO. 1 | 1   | 18  |  <== BCM 18 is on physical pin 12
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 1 | ALT0 | GPIO.26 | 26  | 12  |  <== BCM 12 is on physical pin 32
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
2) Do the following test to switch the fan on

Code: Select all

gpio -g mode 12 output
gpio -g write 12 1
3) Do the following test to switch the fan off

Code: Select all

gpio -g mode 12 output
gpio -g write 12 0
If the above works then do the following:

Code: Select all

gpio -g mode 12 pwm
gpio -g pwm 12 0
gpio -g pwm 12 1023
The fan should now be on the highest speed, with my fan this takes a bit of time (60 seconds) to reach the highest speed.
If the above is successful do the following test:

Code: Select all

gpio -g mode 12 pwm
gpio -g pwm 12 0
for (( a=0; a<1025; a=a+64 )); do echo $a; gpio -g pwm 12 $a;sleep 15; done
If the fan does work with output mode but not with pwm then the problem is that the fan does not like pwm.
If the fan does not change speed nicely in the last test, then it may need an adjustment to the minimum pwm and step.
this works best with my fan:

Code: Select all

for (( a=832; a<1025; a=a+32 )); do echo $a; gpio -g pwm 12 $a;sleep 5; done
The road to insanity is paved with static ip addresses

User avatar
Imperf3kt
Posts: 3381
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: PMW temperature controlled fan

Mon Jun 26, 2017 9:04 pm

Why "1023". How did you come up with that number?
<rant>
This is one thing that made Linux inaccessible to me for years - random numbers for commands.
Ex: chmod 755 to change file permissions... How am I expected to know that as a new user.
</rant>

Anyway, I appreciate the help. I will try what you suggested and if it doesn't work for me I will just deal with a noisy fan.
55:55:44:44:4C
52:4C:52:42:41

Ernst
Posts: 1286
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: PMW temperature controlled fan

Mon Jun 26, 2017 9:40 pm

My problem is that I have been involved with computers for so many years that I do not notice these "strange" values.
Please read the next as it may help you in the future.
Here http://wiringpi.com/the-gpio-utility/ you will see that the range for the pwm command is 0-1023, which means a total of 1024 values.
Converted from decimal into hexadecimal this will be 0-3FF or in octal 1777, converted to binary using 2 eight bit words this is 0000 0000 0000 0000 to 0000 0011 1111 1111.
The value decimal 1000 is hexadecimal 03e8 which is a strange value for a computer, this is why hexadeciman 03ff is used instead as the maximum because it fits into a 7 bit register. (make a mental note here).
You must be aware that hexadecimal notation (using 8 bits) is something new, in the beginning there were only 7 bits and octal notation was used where 3 bits are combined.
Now it might be clear why I got into this so deeply, a long time ago, when linux was not yet invented, octal was used to note binary information. When you look at chmod you should see that there are three groups of 3 bits, each group can be expressed with an octal digit, thus 755 means 111 101 101, converted to hexadecimal 1ed, somehow octal makes sense here ;)
The road to insanity is paved with static ip addresses

User avatar
Imperf3kt
Posts: 3381
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: PMW temperature controlled fan

Mon Jun 26, 2017 10:16 pm

Awesome, thanks for the lesson. I had no idea computers used to use 7 bits not 8.

I still don't understand the signifigance ofsome numbers, but that definitely helped me understand PWM a bit better and will likely be invaluable later.
55:55:44:44:4C
52:4C:52:42:41

Return to “Beginners”