Stuff Babies Need

Using the Raspberry PI CPU Temperature sensor to get an approximate room temperature

Yesterday I wrote a post showing how to create a Raspberry Pi Temperature Logger using a cheap and simple USB Thermometer. If you don’t have and don’t want to buy a USB Thermometer, here’s a quick and dirty alternative – using the Raspberry Pi CPU temperature sensor to get an approximate room temperature.

Reading the Raspberry Pi CPU temperature

Whilst you can get dedicated USB Thermometer sensors, for a quick and simple approximate temperature, you can use a built in sensor on the Raspberry Pi‘s CPU.

/opt/vc/bin/vcgencmd measure_temp

Running the command above will give you an output such as:

temp=45.5'C

As you can see, the temperature is a little high – the thermometer in this case is a sensor right inside the Raspberry Pi’s CPU. With a little code, we can extract just the temperature and apply an offset to get it back to an approximate room temperature.

Given that I do have a USB Thermometer, I can compare the two temperatures as per the below:

pi@raspberrypi:~ $ sudo temper-poll
Found 1 devices
Device #0: 21.6°C 70.9°F
pi@raspberrypi:~ $ /opt/vc/bin/vcgencmd measure_temp
temp=37.9'C

This was taken once the Pi had been on and active for a while, but without anything too stressful going on. So in my case, we can approximate the room temperature by offsetting the Raspberry Pi CPU temperature by 16.3 degrees Celsius.

A quick update to yesterdays python script to read the Raspberry Pi CPU temperature sensor rather than the USB thermometer:

 import os.path
import datetime
import time
import subprocess

while True:
    try:
        #If the file is new, we'll write a header row
        header_row = None
        if os.path.isfile('./temperature_log.csv') == False:
            header_row = 'datetime,temperature_c\n'
        
        #The temperatures will be logged to this file    
        f = open('temperature_log.csv', 'a')

        #Write the header row if needed
        if header_row:
            f.write(header_row)
        
        
        cpu_temp = float(subprocess.check_output(["/opt/vc/bin/vcgencmd","measure_temp"]).replace("temp=","").replace("'C","")) - 16.3
        
        #Write the datetime and temperature
        f.write(str(datetime.datetime.now()) + ',' + str(cpu_temp) + '\n')
        
        #Close the file until next time...
        f.close()
        print str(datetime.datetime.now()) + " - " + str(cpu_temp)
        
    #Badly handle any exceptions...    
    except Exception as e: 
        print "An error occured..." + str(e)
        pass
    #Sleep for 10 mins before taking the next reading
    time.sleep(600)

Same as before, it will print the temperature and log to a CSV file every 10 mins. I’ll leave this running and provide some output shortly!

Note – don’t rely on this, if you have a baby and are worried about the temperature in their room, get a decent room thermometer!

5 comments for “Using the Raspberry PI CPU Temperature sensor to get an approximate room temperature

  1. dahlo
    February 21, 2016 at 10:49 am

    It would be cool to make some temp curves of the cpu under different loads and fit a curve to the values, and then repeat it at a different ambient temperatures, giving you a f(avg_load, cpu_temp) = ambient_temp

    • February 23, 2016 at 5:54 am

      That’s a cool idea. If I keep it in the same room, maybe time of day could be used as well to predict the temperature.

  2. Bish
    May 8, 2016 at 11:21 am

    I know this is an oldish article, and I hate to be a buzzkill, but as a parent myself, I’m not sure you should open this post with ‘If you don’t have and don’t want to buy a USB Thermometer, here’s a quick and dirty alternative – using the Raspberry Pi CPU temperature sensor to get an approximate room temperature.’ The disclaimer at the end really ought to be front and centre.

    Different people’s Pis will have different specs and may be running additional background processes (mine doubles as an Airplay station and drives a Pimoroni Rainbow Hat, which I use as a gently glowing nightlight) – using your 16.3C offset would probably give me unsettlingly high readings, since my Pi is probably running harder. Dahlo’s suggestion of mapping it against avg_load would be a better way of extracting a more accurate and transferrable reading, but even so, if a hack-curious parent is looking to monitor temperature in a baby’s bedroom, they’re still much better buying a USB thermometer or a component like the DS18B20 – or, as you say, a dedicated thermometer.

    • May 8, 2016 at 3:01 pm

      Ah, you’re right, I wouldn’t want someone to rely on this, I’ll update it when I get a chance.

  3. Pingback: dancome

Leave a Reply

%d bloggers like this: