Fan control of Raspberry

Written by pmd - - no comments

Hardware

Python3 program

I have red a lot of pages how to control a fan using PWM signal.
Some of them:

 

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

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

Write a comment

What is the third letter of the word hqtdb?