Created
October 24, 2020 17:03
-
-
Save isoteemu/319d5a8550b037268250561ad0517ab5 to your computer and use it in GitHub Desktop.
nespi npn fan
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 IO | |
from gpiozero import CPUTemperature | |
import time | |
import logging | |
minspin = 25 | |
logger = logging.getLogger(__name__) | |
def main(): | |
IO.setwarnings(False) | |
IO.setmode(IO.BCM) | |
IO.setup(21,IO.OUT) | |
fan = IO.PWM(21, 100) | |
cpu = CPUTemperature() | |
fan.start(0) | |
oldtemp = 0 | |
while True: | |
temp = cpu.temperature | |
if abs(temp - oldtemp) < 1.5: | |
time.sleep(2) | |
continue | |
oldtemp = temp | |
if temp > 50: | |
speed = max(5, min(100, ((temp - 45)*4)+minspin)) | |
fan.ChangeDutyCycle(speed) | |
logger.info(f"Temperature changed to: {temp}. Adjusting fan speed to {speed}") | |
else: | |
fan.ChangeDutyCycle(0) | |
logger.info("Turning fan off") | |
time.sleep(1) | |
fan.stop() | |
IO.cleanup() | |
logger.info("Exiting mainloop...") | |
if __name__ == "__main__": | |
logging.basicConfig(level=logging.INFO) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment