From dd45d017cf6d256acafccf17953b26d2ffaa0cb6 Mon Sep 17 00:00:00 2001 From: Matt Kohls Date: Sun, 17 Jun 2018 16:03:00 -0400 Subject: Adding in comments and reworking how temp is read from CPU --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ rpi-temp-control.py | 22 ++++++++++------------ 2 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d88c3ff --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# rpi-temp-control + +A temperature monitor and control program for the Raspberry Pi's CPU. This is done by monitoring the reported temperature +of the combined CPU and GPU package, which is fed into a PID loop that controls the speed of a fan mounted above it. + +I wrote this so my Pi would be quite when it is doing simple tasks, but would be able to enable a fan to cool the CPU +when I need to compile things or surf the web. + +# Using + +To run this program, you are going to need root privileges but can be run by simply entering + +``` +# python rpi-temp-control.py +``` + +This will leave the program running forever, and you should notice the fan changing speed as the Pi gets warmer and +cooler. At some point I will turn this into a proper daemon service that can be setup and forgotten about. + +There are only a couple of things that should be noted when using this, first that you need to connect a fan to the Pi +in a safe way. You should NOT drive the fan from the gpio pins on the Pi directly, but instead the pin that this +program controls should be some sort of enable line on either a power transistor or motor control board. The second +thing is that this program is writing a pwm signal to that pin, which means the fan is really turning on and off quickly +and may make not allow this program to work with motor controllers that use other methods to control motor speed. Also +be aware that there is probably a minimum speed (or pulse rate) that the fan will work at, so the minimum fan speed +may need to be tweaked because of that. + +Please don't expect this to solve all overheating issues your Pi may experience, this just a dumb PID loop. Really any +Raspberry Pi 3 (at least in my experience) should have at least a heatsink on their CPU to help with limiting when +thermal throttling will happen. A case or mounting for a fan should have enough airflow for the fan so it won't burn out +the motor, but if the air the fan is moving across the CPU will limit how cool it can make the CPU. +# Copyright and License + +2018 Matt Kohls +GPL v3 + +Based on the work by [rudybrian](https://github.com/rudybrian/PID_Fan_Control) + +This program is distributed in the hope it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty +of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. \ No newline at end of file diff --git a/rpi-temp-control.py b/rpi-temp-control.py index d99824a..fb7f0aa 100644 --- a/rpi-temp-control.py +++ b/rpi-temp-control.py @@ -3,11 +3,14 @@ ## # rpi-temp-control # -# +# Changes speed of system fan to try and control the CPU/GPU temperature. This is done through the use of a PID loop +# that is targeting a specific temperature. # # Matt Kohls +# 2018 +# +# Licensed under GPL v3 -import subprocess from gpiozero import Motor from pid import PID from time import sleep @@ -22,14 +25,10 @@ MIN_FAN_SPEED = 80 / 100 # # @return temperature of GPU/CPU def grab_temp(): - try: - file = open("/sys/class/thermal/thermal_zone0/temp", "r") + val = 0 + with open("/sys/class/thermal/thermal_zone0/temp", "r") as file: val = int(file.readline()) / 1000 - file.close() - return val - except: - print("Error reading from thermal zone") - return 0 + return val # Initial loop setup ploop = PID(1, 1, .02, Integrator_max=100, Integrator_min=0, Set_Point=TEMP_TARGET) @@ -42,9 +41,8 @@ last_temp = 0 while True: sleep(1) temp = grab_temp() - print(temp) - cycle_change = (ploop.update(temp)) * -1 - cycle = (100 + int(cycle_change)) / 100 # Since fan.forward() wants a number between 0 and 1 + cycle_change = ploop.update(temp) + cycle = (100 - int(cycle_change)) / 100 # Since fan.forward() wants a number between 0 and 1 if cycle <= .2: cycle = 0 -- cgit v1.2.3