Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PWM to control speed? #22

Open
jcoetzee opened this issue Jul 24, 2019 · 11 comments

Comments

@jcoetzee
Copy link

commented Jul 24, 2019

I was looking at the RPi.GPIO package and I noticed that it supports software-based PWM. I was wondering if that would be an idea to add support for that to get more control over fan output than just off and on? Would that be applicable in this case? Not sure what the overhead would be. I don't have a Fan SHIM to play with (not available in my country yet) but could look at bashing around with the idea when I do get one. Unless something else makes it a non-starter.

@jcoetzee

This comment has been minimized.

Copy link
Author

commented Jul 24, 2019

Could even go a step further and delegate control logic to a PID controller (e.g. simple-pid) allowing smooth ramping up and down of fan speed.

@grayerbeard

This comment has been minimized.

Copy link

commented Aug 10, 2019

I intend to try implementing PWM and I wondered what effect would be on the fan. I would think if the right PWM frequency and control algorithm is chosen (any suggestions?) then the Fan might last longer than with the usual "bang bang" on off control.
To start with how about a PWM frequency of (say) 20Hz and a target temperature of 58 and control range below 62. So 100% speed at 62C 0% speed at 54C.
Then of course there is the simple-pid mentioned above which would provide an interesting exercise in its use.
As an exercise in more advanced Control Engineering one could even use a model to improve accuracy by using CPU frequency and load as inputs to a model. (That is what is done nowadays in advanced control systems). You then compare the actual temperature with the predicted model output to update the model.

@druck13

This comment has been minimized.

Copy link

commented Aug 10, 2019

@grayerbeard

This comment has been minimized.

Copy link

commented Aug 17, 2019

I have PWM working OK but I had to play around quite a bit with the pwm frequency and the algorithm. With that arrangement I have not had to put fan full on for a period to get it going (although that is an alternative to what I have done).

Ref:

This is a good idea, as I prefer to see a nice stable temperature on my
CPU temperature graphs, rather than it cycling between high and low
values, so I tend to set the fans to run all the time at low speed.

One could have a very slow control that looked for the right constant speed speed.

I tested with finding the maximum frequency for each range of pwm speeds that always gets the fan going continuously (see below). For lower pwm % you need a lower frequency for the fan to spin at all but not too low so that its running intermittently. Very low speed values are not really worth bothering with (little cooling effect) so I start at 20%.

Its a rather nice result with the Fan spinning nice and calmly most of the time and never intermittently.
The speed is updated at a very slow rate (about every minute and a half) normally but a check is made every 5 sec in case temperature is above a threshold and then for as long as temp is high speed is updated every 5 seconds.

On a full load CPU test it settles at around 60% speed and about 65C.
For long periods the fan does not run at all.

See ([https://www.smalle.uk/r-pi-4-blog])
speed calc is speed = 20 + 80 * (cpu.temp - min_temp)/(max_temp - min_temp)
where min_temp is 62 and max_temp is 68.
The PWM frequency is set ....
2Hz for Speed of 20% to 26%
3Hz for speed of 26% to 32%
4Hz for speed of 32% to 45%
6Hz for speed of 45% to 100%

I have not progressed to PID but I have my doubts its worth it from your comments above and observing what I have so far.

I have used a similar algorithm to control a Sauna Heater and its much easier to get a good result in situations when the load and conditions are constantly changing and super accuracy is not needed.

Whether its worth the trouble or not is debatable but its a nice exercise in Python and control methods.

@AndreaCCIE

This comment has been minimized.

Copy link

commented Sep 2, 2019

@grayerbeard are you going to publish the code somewhere?

@adicarlo

This comment has been minimized.

Copy link

commented Sep 19, 2019

I really don't understand the approach you guys are suggesting.

Linux includes a facility for fan control by way of device tree overlays and GPIO. Why are you pursuing software control of fan speed when it should be the kernel dealing with it, that is, if you want maximum robustness?

@jcoetzee

This comment has been minimized.

Copy link
Author

commented Sep 20, 2019

That's because RPi.GPIO (the package used by the library) only supports software PWM (hardware based is in the pipeline apparently). Obviously hardware-based would be preferable and there are python libraries that do claim support, but they come with their own complications and would involve rewriting the project to use them. I opened this mostly to gauge interest and prove the concept. If viable it may be beneficial to look into different python package although I have no visibility into why this package was chosen in the first place.

@polojoe

This comment has been minimized.

Copy link

commented Sep 20, 2019

@grayerbeard I would be also interested in the working pwm code.

@joaomoreno

This comment has been minimized.

Copy link

commented Sep 24, 2019

@grayerbeard Yeah show us the goods.

@grayerbeard

This comment has been minimized.

Copy link

commented Oct 14, 2019

First of all apologies for not answering earlier as I have been traveling.
I do have a set of PWM code that works and its fairly Modularized so not hard to extract from it a simpler version. The problem I found was that the FanShim does not like short on times so there is only a range of frequencies/PWM rates that work
Once I have got over jet lag etc and will put the latest version on my website at My Pi Blog Where you can see the result.

I am happy with the result as it can be set up to hardly use the fan at all if the Pi is not busy and yet can manage the temperature well when there is a burst of activity.

@Raphael713 Raphael713 referenced this issue Oct 15, 2019
@grayerbeard

This comment has been minimized.

Copy link

commented Oct 17, 2019

Scripts now uploaded to https://www.smalle.uk/r-pi-4-blog where you can see the results of the code running live as of now. (1400hrs Oct 17th) and you can download the python etc.
It will be some time before I get to a tidy version but you should be able to extract the essential parts to make a PWM control of your own.
Much of the time the fan runs nice and peacefully at 70% speed or less, much more peaceful than when it runs at full speed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.