-
Notifications
You must be signed in to change notification settings - Fork 0
/
fan_demo1.py
38 lines (34 loc) · 963 Bytes
/
fan_demo1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BCM)
##Set to false, other processes occupying the pin will be ignored
GPIO.setwarnings(False)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12,100)
print("\nPress Ctrl+C to quit \n")
dc = 0
pwm.start(dc)
def ReadTemp():
# view CPU temperature
file = open("/sys/class/thermal/thermal_zone0/temp")
cpu = float(file.read()) / 1000
file.close()
print('CPU temperature is: %2.2f' % cpu)
time.sleep(5)
try:
while True:
temp = subprocess.getoutput("vcgencmd measure_temp|sed 's/[^0-9.]//g'")
ReadTemp()
if round(float(temp)) >= 40:
dc = 10
pwm.ChangeDutyCycle(dc)
time.sleep(0.05)
else:
dc = 0
pwm.ChangeDutyCycle(dc)
time.sleep(0.05)
except KeyboardInterrupt:
pwm.stop()
GPIO.cleanup()
print("Ctrl + C pressed -- Ending program")