-
-
Save ugurelveren/435df9b77b94731698aa42c5a85775f0 to your computer and use it in GitHub Desktop.
Python Script for Raspberry Pi fan speed control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode(GPIO.BOARD) | |
GPIO.setup(12, GPIO.OUT) | |
fan = GPIO.PWM(12, 50) # Initialize PWM on pin 12 with 50Hz frequency | |
fan.start(0) # Start the PWM with 0% duty cycle | |
while True: | |
temp = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1000 | |
if temp < 38: | |
fan.ChangeDutyCycle(0) # Turn off the fan if temperature is below 38C | |
elif temp >= 38 and temp < 40: | |
fan.ChangeDutyCycle(30) # Set fan speed to 30% if temperature is between 38C and 40C | |
elif temp >= 40 and temp < 43: | |
fan.ChangeDutyCycle(50) | |
elif temp >= 43 and temp < 45: | |
fan.ChangeDutyCycle(80) | |
else: | |
fan.ChangeDutyCycle(100) # Set fan speed to 100% if temperature is above 45C | |
time.sleep(5) # Wait for 5 seconds before checking temperature again |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment