Fan control of Raspberry
Written by pmd - - no commentsHardware
- Pi Model 3B V1.2
- Raspberry Pi 4 Model B Dual Fans CNC Aluminum Alloy Case Metal 4 Color Armor Shell with Heat Sinks for Raspberry Pi 4B/3B+/3B
- T9 Fan Control Module / TCFan Raspberry Pi 4B 5V PWM Temperature Control Speed Contro
Python3 program
I have red a lot of pages how to control a fan using PWM signal.
Some of them:
- Raspberry Pi Frequency management and thermal control
- PWM Regulated Fan Based on CPU Temperature for Raspberry Pi : 4 Steps (with Pictures) - Instructables
- Smart Raspberry Pi CPU fan - Hackster.io
- Variable Speed Cooling Fan for Raspberry Pi using PWM (video#138) – SensorsIOT
- Fan Control for Raspberry Pi
Finally, it seems my fans don't have much effect on the temperature. About 5°C. So I decided to set an hysteresis:
- Switch ON fans if temperature > 75°C
- Switch OFF fans if temperature < 60°C
- Do nothing if 60°C <= temperature <= 75°C
$ nano gpio_test.py
#!/usr/bin/python3
# -*-coding:Utf-8 -*
import os
from gpiozero import LED
from time import sleep
import RPi.GPIO as GPIO
fanPin = 2
testMode = False
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
temp =(res.replace("temp=","").replace("'C\n",""))
#print("temp is {0}".format(temp)) #Uncomment here for testing
return temp
try:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(fanPin, GPIO.OUT)
myPWM=GPIO.PWM(fanPin,200)
myPWM.start(0)
GPIO.setwarnings(False)
while True:
if testMode:
duty_cycle = input("Nouveau PWM (%) ? ")
myPWM.ChangeDutyCycle(int(duty_cycle))
else:
temp = float(getCPUtemperature())
if temp > 75:
myPWM.ChangeDutyCycle(100)
elif temp < 60:
myPWM.ChangeDutyCycle(0)
else:
pass
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program
#!/usr/bin/python3
# -*-coding:Utf-8 -*
import os
from gpiozero import LED
from time import sleep
import RPi.GPIO as GPIO
fanPin = 2
testMode = False
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
temp =(res.replace("temp=","").replace("'C\n",""))
#print("temp is {0}".format(temp)) #Uncomment here for testing
return temp
try:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(fanPin, GPIO.OUT)
myPWM=GPIO.PWM(fanPin,200)
myPWM.start(0)
GPIO.setwarnings(False)
while True:
if testMode:
duty_cycle = input("Nouveau PWM (%) ? ")
myPWM.ChangeDutyCycle(int(duty_cycle))
else:
temp = float(getCPUtemperature())
if temp > 75:
myPWM.ChangeDutyCycle(100)
elif temp < 60:
myPWM.ChangeDutyCycle(0)
else:
pass
sleep(5) # Read the temperature every 5 sec, increase or decrease this limit if you want
except KeyboardInterrupt: # trap a CTRL+C keyboard interrupt
GPIO.cleanup() # resets all GPIO ports used by this program
Set linux service
$ nano /etc/systemd/system/manageFan.service
[Unit]
Description=start fan management at system startup
After = network-online.target
Wants = network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /home/pi/gpio_test.py
[Install]
WantedBy=multi-user.target
[Unit]
Description=start fan management at system startup
After = network-online.target
Wants = network-online.target
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /home/pi/gpio_test.py
[Install]
WantedBy=multi-user.target