Raspberry Pi CPU Fan Automation

All these numbers are arbitrary, start with ‘some’ numbers and adjust as you go along.
Okay so your pwm is (say) 100 hz, so 100% means that the 1st pulse is on for the whole 1/100th second and is immediately followed by the next pulse. And 0% is the same in reverse.
I know you know this.
So let’s assume a target of 38 degrees, and bands at 10, 6, 3 and 1 degrees with each having a percentage of 25, 12, 4 and 1,
Having a target of 38 when if you run the fan at 100% it only gets down to 44 is pointless so you need to set realistic targets and have a fan big enough to achieve it.
So you start the cycle at 50% wait 30 secs, if measured is 49 (11 off) so increase pwm by 25 % wait 30 secs, measure 45 (7 off) so increase by12 %, wait 30 secs, measure 43, (off by 5 so another 12 % (now at 99%} wait 30, measure 41 (3 off) so add 4% (99 +4 must top out at 100) so run at 100, wait 30secs temp is now 36 (so 2 off, reduce by 1 - > 99) wait 30, temp is still 36 reduce by another 1 etc down at less than 1 degree offset make no adjustment
You want an asymptotic approach with minimal overshoot, if possible, but you need to adjust the timings and bands and nudge sizes according to your setup.
The algorithm needs to be simple to implement and slow to respond enabling the thermal inertia to bleed out and your response to allow it to settle, going between 100% and 0% is basically just a binary response and you’ve wasted your time. You will need to have it respond to changes in processor load and ambient temperature, both of which should be a lot slower than your responses.
Keep us updated

OT:
Nope - how many rpis do you see here?
hint one more in the green mini

And for the screws i like it better to acoustically decouple Fans from cases like this
[/OT]

That’s one of the nicest pi cases I’ve ever seen.
Does the fan sound like an A Series 1275 with twin Webbers ?

I would never consider a fan while running to be silent, no matter how slow.

Passive = silent, spinning fan = non-silent

I can recommend the FLIRC case, it works great if you want a silent solution

[/super OT]
its made keeping the WAF high.
my wife told me if i dont want the divorce papers i should remove this as the bootup sound. (1380 straightcut dogbox with a Garrett T3 because i live in a hilly area / not)
[\super OT]

1 Like

Hello Tobi, I like your project and just wanted to take it over because my raspi is always too hot. My question to you do not know whether it is a typing error but you write that you have set the fan as a switch but your description says sensor on port 4. Thanks in advance yoda

@Yoda Which case are you using with your Rpi?
I have this one
https://www.aliexpress.com/item/4000001902776.html
This has two small fans that run of the 5 V rail.
I have used the pwmled addon to control the speed of these fans.
I have used a N Channel Mosfet to drive these to prevent the RPi port overload.
The fans work as an automation. The spin only when temperature is above 55 Deg.C and stops to spin when temp falls below 45 Deg.C Between these temperature values the fan speed is based on PWM.
This method can be used for any fan that you might want to use.
This is my Automation

- id: update cpu temperature
  alias: Time To Start Cpu Cooling
  trigger:
    platform: numeric_state
    entity_id: sensor.cpu_temperature
    above: 55
  action:
    service: light.turn_on
    entity_id: light.pwm_cooling_fan
    data_template:
      brightness_pct: '{{ states(''sensor.cpu_temperature'') }}'
- id: update cpu temperature again
  alias: Time To Stop Cpu Cooling
  trigger:
    platform: numeric_state
    entity_id: sensor.cpu_temperature
    below: 45
  action:
    service: light.turn_off
    entity_id: light.pwm_cooling_fan

I hope my sharing is of help…

2 Likes

Do you think its also possible to do somehow with nearly same double fan, but only with 5V and GND cable? I have only connected this two and would like to also automatize cooling. Thank you.

Great!!! This is what I was searching.
Only a question: I used a IRLZ44N mosfet to drive the fan without any resistance in the gate. I used this solution to act like a switch on/off driving the gate on GPIO17 by HA thermostat linked to the CPU temp… For the good knowledge of the beginners and double checks, could you post your wires configuration, please?
I don’t want to fry my Rpi.
TIA

@Ninuss I actually switched to a flicr case du to the fan noise. And the Flickr case cools the rpi passively, works like a charm

Thanks @Tobi0892 … I’ll think about your suggestion and think I’ll adopt it in the future. I don’t like only the fact that there are no GPIO openings in that chassis though. Before that, I played around with electronics a bit. I successfully got a working GPIO18 PWM pin on my RPi B +, also using an old IRLZ44N mosfet to toggle the separate fan circuit from the GPIOs to avoid damage. I used karansoi’s automation but there was a limit. The fan speed does not work properly without calibrating the fan start at a certain low power% of pwm. Maybe my fan is old, because, even calibrated, the initial current to be given to the fan to start spinning is always unpredictable for the rotation, even if Rpi gives it the correct duty cycle found earlier.

@Ninuss I am sorry was away so could not respond.
BTW is your query resolved. I am using one of the gpio these are 3.3 V so I used the mosfet.
The system is working. I am using gpio 17 to drive the gate of the mosfet. However the fan is ppm controlled as set by pwmled add-on

@colethegamer Yes you can use two fans instead of one as I have on my case.
These fans are rated for 5v. RPi GPIOs are 3.3 V compliant. I have used GPIO17 as a pwm switch. This GPIO outputs a ppm signal that is used to drive the N channel Mosfet. This way the 3.3v is used to control the 5 V fan. Of course use is made of the flyback diode to quench the voltage spikes out of the motor inductance at switch off…

would anyone here know of a way to regulate the official Raspberry fan case ? https://www.raspberrypi.org/products/raspberry-pi-4-case-fan/

Yup, using a simple switch:

# GPIO switch for the case fan
  - platform: rpi_gpio
    ports:
      14: CPU Fan

With the control wire of the fan connected to the GPIO #14

Then a couple of simple automations to turn it on and off:

alias: 'Fan Off 52C '
description: Turns the fan on if the CPU < 52C
trigger:
  - platform: numeric_state
    entity_id: sensor.cpu_temp
    for: '60'
    below: '52'
condition: []
action:
  - service: switch.turn_off
    entity_id: switch.cpu_fan
mode: single
alias: Fan On 70C
description: Turns the fan on if the CPU > 70C
trigger:
  - platform: numeric_state
    entity_id: sensor.cpu_temp
    above: '70'
    for: '60'
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.cpu_fan
mode: single

CPU temp comes from this sensor:

# CPU temperature, for fan
  - platform: command_line
    name: CPU Temp
    command: "/bin/cat /sys/class/thermal/thermal_zone0/temp"
    unit_of_measurement: "°C"
    value_template: '{{ (value | int / 1000) | int }}'

1 Like

that’s really nice! thank you.
so we wouldn’t need anything else, no scripts to run before starting HA, all this can be done from within HA as is?

The instructions for the Fan case state the connections need to be to 4, 6 and 8 though…,. can we simply change that?

Obviously the power and ground need to be connected to one of the 5v and gnd pins, but other than that yes. I just kept them as per the diagram as it’s nice and simple, but I also had it running fine on GPIO 17 at one point too as a test.

wait, I think I misunderstood the diagram. pin 8 (the bottom of the 3 used) is GPIO 14 (TXD)… so that is exactly as you say isn’t it?

Yes, the set-up I currently have is matching the Pi Foundation set-up.

But in our case, if you want you can use a different control pin than #14 if you want. As I said, at one point I had it running on #17 (pin 11) as a test and it worked fine (with a suitable adjustment on the switch config of course).

undrstood, and thanks!

guess my confusion is common, and described at the bottom of this: Raspberry Pi GPIO - Home Assistant :wink:

btw, I guess there’s no such thing as setting a fan speed %?