As the Raspberry Pi has become more powerful temperature management has become more important. This article aims to provide some easy techniques to monitor the temperature of your Pi.
The Pi 4 will start to throttle the performance if the temperature exceeds 80 degrees so it is wise to keep an eye on your system temperature.
Read temperature from command line
The easiest way to determine the temperature of your Raspberry Pi is by using the following command in a terminal window :
vcgencmd measure_temp
This will display the temperature in degrees Celsius:
temp=31.0'C
To extract the number you can use:
vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*'
This feeds the output of vcgenmd to egrep. This then outputs only the characters that are numbers surrounding a full stop (period) character.
If you want to constantly monitor the temperature you can use the watch command:
watch -n 2 vcgencmd measure_temp
This will show the temperature and update the value every 2 seconds.
Add Temperature Display to Raspberry Pi OS Desktop
If you are using the Raspberry Pi OS desktop you can add a temperature gauge to the top panel where the clock is displayed. This makes it really easy to keep an eye on the value as you perform different tasks:
In this example the temperature of my Pi is 35 degrees Celsius.
To add the temperature to the top panel:
- Right click on the top panel
- Select “Add / Remove Panel items”
- Click “Add”
- Scroll down the list and select “Temperature Monitor”
- Click “Add”
- Use the “Up” button to position the monitor above the “Digital Clock”
If you want to add some space between the panel items you can:
- Right click on the top panel
- Select “Add / Remove Panel items”
- Click “Add”
- Scroll down the list and select “Spacer”
- Click “Add”
- Use the “Up” or “Down” buttons to position the spacer
- Use the “Preferences” button to set the size of the spacer
I used Spacers with a size of 10 to provide a sensible gap between the temperature reading and the surrounding panel items.
You can change the thresholds and the colours used by:
- Right click on the Temperature in the top panel
- Select “Temperature Monitor Settings”
- Change the settings as required
- Click “OK” to save
Here are my settings:
You can change the values at which the warning colours are triggered.
Reading temperature in Python
It’s fairly easy to read the Pi CPU temperature in Python using the Gpiozero library. Simply import the library at the top of your script:
import gpiozero as gz
and then read the temperature into a variable:
cpu_temp = gz.CPUTemperature().temperature
if you want to round the value to one decimal place you can add the line:
cpu_temp = round(cpu_temp, 1)
Once you have the temperature value in the variable you print it to the screen, log to a file or send to a database.
Reading temperature in Bash
To read the temperature from within a Bash script you can use:
temp=$(('cat /sys/class/thermal/thermal_zone0/temp'/1000)) echo $temp
You’ll get an integer result with no decimal places but that shouldn’t be an issue for most purposes.