Categories
Projects

Measuring Raspberry Pi temperature one-off and self-updating guide

There are mutiple ways to get the measure the temperature on a Raspberry Pi. In the examples below I’ll show you the commands that you need to:

Things you’ll need

The details below assume that you have:

  • A Raspberry Pi 3 or 4 Running 32bit Raspian (Debian)
  • Terminal access

Generally speaking a monitor, keyboard and mouse will be needed to access Terminal however I connect via SSH for devices running Raspberry Pi OS (Raspian) Lite and RDP for those running Raspberry Pi OS (Raspian) Full GUI.

Getting a one-off temperature reading on Raspberry Pi

Raspberry Pi’s have a range of sensors built in which will can be interogated via Terminal commands. The commands are a bit long to type in every time but I’ll include details below to create an ‘alias’ so that you can type in a single word to get a reading.

In Terminal type the following

sudo /opt/vc/bin/vcgencmd measure_temp

Terminal should then show, on the next line, the temperature of your Raspberry Pi which in this case is 53.0 C

temp=53.0'C
Output from running measure_temp command on Raspberry Pi OS

Creating an alias to retrieve the Pi’s temperature

If you want to check the temperature of the Raspberry Pi often, typing in the above is not particularly effecient. You can however create an alias for this command so that you can shortcut all of that nonsense.

Start by creating the bash aliases file – this needs to be within the Raspberry Pi user’s directory and can be done with the command below.

sudo nano /home/pi/.bash_aliases

Now add in the following line if you want the shortcut to be “temp”. If you want to use a different word, change the bolded section to whatever you want it to be.

alias temp='/opt/vc/bin/vcgencmd measure_temp'
Example of bash_aliases file when adding temp alias on Raspberry Pi OS
Example of bash_aliases file when adding temp alias on Raspberry Pi OS

NOTE: to actually use this shortcut you need to close the existing Terminal Window and start a new ones. Open up a new Terminal Window and you can now simply enter the temp command to get your Raspberry Pi’s temperature.

Output after using the temp alias on Raspberry Pi OS
Output after using the temp alias on Raspberry Pi OS

Get regularly updating Raspberry Pi temperature reading

To get a reading that will update every second you can use the same measure_temp command but uses a slightly different setup.

Open a new Terminal Window seperate to any existing Terminal’s that are open as this is where you’ll see the terminal updating.

sudo watch -n1 vcgencmd measure_temp
Raspberry Pi Terminal showing output from using watch -n1 vcgencmd measure_temp
Raspberry Pi Terminal showing output from using watch -n1 vcgencmd measure_temp

Terminal will then provide a output which will update every second. This can be handy if you’re looking to identify cooling impact from fans or other external factors.