Skip to content

Instantly share code, notes, and snippets.

@ugurelveren
Last active March 22, 2023 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugurelveren/435df9b77b94731698aa42c5a85775f0 to your computer and use it in GitHub Desktop.
Save ugurelveren/435df9b77b94731698aa42c5a85775f0 to your computer and use it in GitHub Desktop.
Python Script for Raspberry Pi fan speed control
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