Advertisement
Guest User

Untitled

a guest
Feb 5th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. import RPi.GPIO as GPIO   # Import the GPIO library.
  2. import time               # Import time library
  3. from gpiozero import CPUTemperature
  4.  
  5.  
  6. GPIO.setmode(GPIO.BCM)  # Set Pi to use pin number when referencing GPIO pins.
  7.                           # Can use GPIO.setmode(GPIO.BCM) instead to use
  8.                           # Broadcom SOC channel names.
  9.  
  10. GPIO.setup(2, GPIO.OUT)  # Set GPIO pin 2 to output mode.
  11. pwm = GPIO.PWM(2, 50)   # Initialize PWM on pwmPin 50Hz frequency
  12.  
  13. # main loop of program
  14.  
  15. cpu = CPUTemperature()
  16. print(cpu.temperature)
  17.  
  18.  
  19.  
  20. print("\nPress Ctl C to quit program \n")  # Print blank line before and after message.
  21. dc=0                               # set dc variable to 0 for 0%
  22. pwm.start(dc)                      # Start PWM with 0% duty cycle
  23.  
  24. try:
  25.   while True:                      # Loop until Ctl C is pressed to stop.
  26.     if (cpu.temperature > 75):
  27.       dc = (((cpu.temperature - 75)*14)+30)
  28.     if dc > 100:
  29.         dc = 100
  30.     if (cpu.temperature < 70):
  31.       dc = 0
  32.     pwm.ChangeDutyCycle(dc)
  33.     time.sleep(2.0)             # wait 2 seconds
  34. #    print(dc," PWM")
  35. #    print(cpu.temperature," C")
  36. except KeyboardInterrupt:
  37.   print("Ctl C pressed - ending program")
  38.  
  39. pwm.stop()                         # stop PWM
  40. GPIO.cleanup()                     # resets GPIO ports used back to input mode
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement