Search code examples
pythonraspberry-picpuledtemperature

Raspberry Pi Control LED Base on CPU Temperature with Python


I'd like some help getting this python code to work with my Raspberry Pi. The goal is to turn on 1 of 3 LED's at a time (Green, Yellow, and Red) based on a CPU Temperature Range.

This means:

  • Green LED needs to turn ON when temperature range is less than 32ºC.
  • Red LED ON if temperature is greater than 37ºC.
  • Then Yellow LED ON if temperature is greater than 31ºC or less than 37ºC.

I'm a newbie at coding, so far I can get the temperature to print and only the Red LED turns on and stays on regardless of CPU temperature.

import os
import time
import RPi.GPIO as GPIO


#GREEN=11
#YELLOW=10
#RED=9

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11,GPIO.OUT)
GPIO.setup(10,GPIO.OUT)
GPIO.setup(9,GPIO.OUT)


def measure_temp():
        temp = os.popen("vcgencmd measure_temp").readline()
        return (temp.replace("temp=","").replace("'C",""))

while True:
        measure_temp()
        if measure_temp<32:
            GPIO.output(11,GPIO.HIGH)
            GPIO.output(10,GPIO.LOW)
            GPIO.output(9,GPIO.LOW)
        if measure_temp>37:
            GPIO.output(9,GPIO.HIGH)
            GPIO.output(10,GPIO.LOW)
            GPIO.output(11,GPIO.LOW)
        if measure_temp>32 or <37
            GPIO.output(10,GPIO.HIGH)
            GPIO.output(11,GPIO.LOW)
            GPIO.output(9,GPIO.LOW)
            print(measure_temp())

#cleanup
c.close()
GPIO.cleanup()

Solution

  • Cool project and your code is close.

    I think the main problem is that you get a string from vcgencmd and you are trying to compare that to a number. I would go with something more like this (untested):

    #!/usr/bin/env python3
    
    import os
    import re
    import time
    import RPi.GPIO as GPIO
    
    RED, YELLOW, GREEN = 9, 10, 11
    
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(RED,GPIO.OUT)
    GPIO.setup(YELLOW,GPIO.OUT)
    GPIO.setup(GREEN,GPIO.OUT)
    
    
    def measure_temp():
            output = os.popen("vcgencmd measure_temp").readline()
            # Remove anything not like a digit or a decimal point
            result = re.sub('[^0-9.]','', output)
            return float(result)
    
    while True:
            temp = measure_temp()
            if temp<32:
                GPIO.output(GREEN,GPIO.HIGH)
                GPIO.output(YELLOW,GPIO.LOW)
                GPIO.output(RED,GPIO.LOW)
            elif temp>37:
                GPIO.output(RED,GPIO.HIGH)
                GPIO.output(GREEN,GPIO.LOW)
                GPIO.output(YELLOW,GPIO.LOW)
            else:
                GPIO.output(YELLOW,GPIO.HIGH)
                GPIO.output(GREEN,GPIO.LOW)
                GPIO.output(RED,GPIO.LOW)
            print(temp)
            # Let's not affect our temperature by running flat-out full-speed :-)
            time.sleep(1)
    
    #cleanup
    c.close()
    GPIO.cleanup()
    

    Note also the use of elif which makes it rather easier for testing the final one of your three cases.

    I also went for a regex to extract the number from the vcgencmd string because the text may change with different internationalisation - YMMV here.