How to Check CPU Temperature on Raspberry Pi Quickly
To check the CPU temperature on a Raspberry Pi, use the command
vcgencmd measure_temp in the terminal. Alternatively, you can read the temperature in Python by running temp = float(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1000 to get the temperature in Celsius.Syntax
The main ways to check CPU temperature on Raspberry Pi are:
vcgencmd measure_temp: Runs a system command to get the temperature.- Reading from
/sys/class/thermal/thermal_zone0/temp: Reads a system file that contains the temperature in millidegrees Celsius.
In Python, you open the file, read the value, and divide by 1000 to convert to degrees Celsius.
bash/python
vcgencmd measure_temp # Python syntax to read temperature with open('/sys/class/thermal/thermal_zone0/temp') as f: temp = float(f.read()) / 1000 print(f"CPU Temperature: {temp}°C")
Example
This example shows how to check the CPU temperature using Python. It reads the temperature file, converts the value, and prints it.
python
with open('/sys/class/thermal/thermal_zone0/temp') as f: temp = float(f.read()) / 1000 print(f"CPU Temperature: {temp:.2f}°C")
Output
CPU Temperature: 45.32°C
Common Pitfalls
Common mistakes when checking CPU temperature on Raspberry Pi include:
- Trying to run
vcgencmdwithout it installed or on unsupported devices. - Not dividing the raw temperature value by 1000 when reading from the system file, resulting in a very large number.
- Running the commands without proper permissions, which can cause errors.
python
## Wrong way (no division): with open('/sys/class/thermal/thermal_zone0/temp') as f: temp = float(f.read()) print(f"Wrong CPU Temperature: {temp} (too large)") ## Right way: with open('/sys/class/thermal/thermal_zone0/temp') as f: temp = float(f.read()) / 1000 print(f"Correct CPU Temperature: {temp}°C")
Output
Wrong CPU Temperature: 45320.0 (too large)
Correct CPU Temperature: 45.32°C
Quick Reference
Summary tips for checking CPU temperature on Raspberry Pi:
- Use
vcgencmd measure_tempfor a quick terminal check. - Read
/sys/class/thermal/thermal_zone0/tempfor programmatic access. - Always divide the raw value by 1000 to get Celsius degrees.
- Run commands with appropriate permissions.
Key Takeaways
Use the command
vcgencmd measure_temp to quickly check CPU temperature in the terminal.Read the file
/sys/class/thermal/thermal_zone0/temp and divide by 1000 to get temperature in Celsius.In Python, open the temperature file, convert the value, and print it for monitoring.
Avoid forgetting to divide the raw temperature value by 1000 to prevent incorrect readings.
Ensure you have proper permissions to run commands or read system files.