BakedPi2 - See how hot your Raspberry Pi is, now with added filtering

This is an old driver based LedBorg script
The new driver free example which replaces this one can be found here.

BakedPi was good, but did you notice just how much the LedBorg jumps around in colour?
What we need is a filtered version that smoothes those updates down!

We decided a simple box or flat filter would be well suited for this task.
Put simply the filter takes the average of the last so many readings and uses that as the result for the colour.
The larger the averaging window (number of samples) the more out of date the reading is, but the smoother the display is.

The improved version is, a Python script which reads the current processor temperature, filters the reading, and then controls an LedBorg appropriately.

There are a few variables in the scripts you may wish to set to change options:
  • pathSensor - Line 18, sets the file to read to get temperature, change this if you want to use a different temperature reading
  • readingPrintMultiplier - Line 19, if you change pathSensor you may need to change this to print values in a sensible unit (e.g. degrees)
  • tempHigh - Line 20, sets the temperature representing 100% scale, you will probably want to change these to show a better temperature range, since our example uses a value which works well when watching the Raspberry Pi warm up from boot
  • tempLow - Line 21, sets the temperature representing 0% scale, you will probably want to change these to show a better temperature range, since our example uses a value which works well when watching the Raspberry Pi warm up from boot
  • interval - Line 22, time in seconds between readings, smaller will update quicker put use more processor time
  • filterSize - Line 23, the size of the filter window, larger numbers make the reading smoother, but also cause it to update slower

Here's the code, you can download the BakedPi2 script file as text here
Save the text file on your pi as
Make the script executable using
chmod +x
and run using

#!/usr/bin/env python
# coding: latin-1

# Import libary functions we need
import time

# Make a function to set the LedBorg colour
def SetColour(colour):

# Set up our temperature chart, from cool to hot
colours = ['002', '012', '022', '021', '020', '120', '220', '210', '200']

# Setup for processor monitor
pathSensor = '/sys/class/thermal/thermal_zone0/temp'    # File path used to read the temperature
readingPrintMultiplier = 0.001                          # Value to multiply the reading by for user display
tempHigh = 40000                                        # Highest scale reading
tempLow = 30000                                         # Lowest scale reading
interval = 1                                            # Time between readings in seconds
filterSize = 10                                         # Number of samples for the filter to cover (larger is smoother but slower)

# Box filter class
class BoxFilter:
    # Filter storage
    oldValues = []
    lastIndex = 0
    seeded = False
    window = 0
    average = 0.0

    # Filter initialisation
    def __init__(self, window):
        # Make sure we have at least 1 sample
        if window < 1:
            self.window = 1
            self.window = int(window)
        # Make our storage the right size
        for i in range(self.window):

    # Run a new value through the filter
    def run(self, newValue):
        # Ensure newValue is floating point
        newValue = float(newValue)
        # Check if we need to 'seed' the filter
        if self.seeded:
            # Find the next value to replace (the list wraps at the end)
            self.lastIndex += 1
            if self.lastIndex >= self.window:
                self.lastIndex = 0
            # Fill in the next value and re-calculate the average
            self.average -= self.oldValues[self.lastIndex] / float(self.window)
            self.oldValues[self.lastIndex] = newValue
            self.average += self.oldValues[self.lastIndex] / float(self.window)
            # We have not, fill in all fields
            for i in range(self.window):
                self.oldValues[i] = newValue
            self.seeded = True
            # Use the new value as the average
            self.average = newValue
        # Return the average
        return self.average

    # Make sure we are using floats
    tempHigh = float(tempHigh)
    tempLow = float(tempLow)
    # Make a new filter
    tempFilter = BoxFilter(filterSize)
    while True:
        # Read the temperature in from the file system
        fSensor = open(pathSensor, 'r')
        rawReading = float(
        # Pass the reading through the filter
        reading =
        # Pick the relevant colour
        position = (reading - tempLow) / (tempHigh - tempLow)
        position = int(position * len(colours))
        if position < 0:
            position = 0
        elif position >= len(colours):
            position = len(colours) - 1
        # Set the relevant colour
        # Print the latest reading, both filtered and raw
        print '%02.3f (raw %02.3f)' % (reading * readingPrintMultiplier, rawReading * readingPrintMultiplier)
        # Wait a while
except KeyboardInterrupt:
    # CTRL+C exit, turn off the LedBorg
    print 'Terminated'
Subscribe to Comments for &quot;BakedPi2 - See how hot your Raspberry Pi is, now with added filtering&quot;