Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import RPi.GPIO as GPIO # Import the GPIO library.
- import time # Import time library
- from gpiozero import CPUTemperature
- GPIO.setmode(GPIO.BCM) # Set Pi to use pin number when referencing GPIO pins.
- # Can use GPIO.setmode(GPIO.BCM) instead to use
- # Broadcom SOC channel names.
- GPIO.setup(2, GPIO.OUT) # Set GPIO pin 2 to output mode.
- pwm = GPIO.PWM(2, 50) # Initialize PWM on pwmPin 50Hz frequency
- # main loop of program
- cpu = CPUTemperature()
- print(cpu.temperature)
- print("\nPress Ctl C to quit program \n") # Print blank line before and after message.
- dc=0 # set dc variable to 0 for 0%
- pwm.start(dc) # Start PWM with 0% duty cycle
- try:
- while True: # Loop until Ctl C is pressed to stop.
- if (cpu.temperature > 75):
- dc = (((cpu.temperature - 75)*14)+30)
- if dc > 100:
- dc = 100
- if (cpu.temperature < 70):
- dc = 0
- pwm.ChangeDutyCycle(dc)
- time.sleep(2.0) # wait 2 seconds
- # print(dc," PWM")
- # print(cpu.temperature," C")
- except KeyboardInterrupt:
- print("Ctl C pressed - ending program")
- pwm.stop() # stop PWM
- GPIO.cleanup() # resets GPIO ports used back to input mode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement