How to Check Your Embedded Device CPU Temperature

Click on the top “Embedded Miscellaneous” and “Star the Official Account” to view embedded notes at the first time!

Everyone should have seen some videos of big shots grilling meat with computer CPUs. Is the temperature of the computer CPU really that high? Let’s take a look at what the temperature of our computer CPU is!

There are many ways to check the CPU temperature on PC, one of the simpler methods is to install some benchmarking software on Windows, through which you can see the CPU temperature, such as Master Lu:

How to Check Your Embedded Device CPU Temperature

In addition, there are many methods to check in Linux, which will not be introduced here.

Next, we will introduce some ways to check the CPU temperature of our Linux embedded devices. To obtain the CPU temperature, we definitely need sensors (not sure if the CPU temperature is collected this way), related drivers, etc. We won’t study these lower-level aspects, let’s just use them for now.

Generally, the Linux kernel comes with drivers for CPU temperature detection, and the device files related to the CPU are in /sys and /proc.

Here, I am using the development board from Baiwen Network, and the virtual file for the CPU temperature object that comes with the factory system is located in /sys/devices/virtual/thermal/thermal_zone0. The temp file in this directory can give us the CPU temperature. For example:

cat /sys/devices/virtual/thermal/thermal_zone0/temp

How to Check Your Embedded Device CPU Temperature

Here, 54242 represents 54.242℃.

We can view the content of a file directly in the shell command line or through programming. Before coding, let’s look at a diagram:

How to Check Your Embedded Device CPU Temperature

This is a basic diagram that must be familiar to learn embedded Linux. We can write applications in two ways: one is to directly call system call level interfaces (i.e., open, read, write, etc.), and the other is to call glibc interfaces (i.e., fopen, fread, fwrite, etc.) to indirectly call system call level interfaces. Related notes are recommended:

Basics of Application Development under Linux

What are Linux Kernel Space and User Space?

Here we will use the glibc calling method to write our application program to read the CPU temperature, the code:

Swipe left and right to view all code>>>

#include <stdio.h>  
#include <stdlib.h>
#include <unistd.h>

int main(int arc, char *argv[])
{
 FILE *fp = NULL;
 unsigned int temperature = 0;
 
 fp = fopen ("/sys/devices/virtual/thermal/thermal_zone0/temp", "r");
 if (fp < 0)
 {
  printf("fopen error!\n");
  exit(1);
 }
 while(1)
 {
  fscanf(fp, "%d", &temperature);
  printf("cpu temperature: %d.%d\n", temperature/1000, temperature%1000/100);
  sleep(1);
 }
 fclose(fp);
 return 0;
}

Cross-compile, transfer to the board, and run:

How to Check Your Embedded Device CPU Temperature

This is the sharing about obtaining CPU temperature. If there are any errors, please feel free to point them out. Go check if your CPU temperature can grill meat!How to Check Your Embedded Device CPU TemperatureHow to Check Your Embedded Device CPU Temperature

You May Also Like

My Journey from Microcontroller to Embedded Linux

[Linux Notes] PC Machine_Development Board_Ubuntu Ping Experiment

[Linux Notes] Mounting Network File Systems

1024G Embedded Resources Giveaway! Including but not limited to C/C++, Microcontrollers, Linux, etc. Reply “1024” in the public account chat interface to get it for free!

How to Check Your Embedded Device CPU Temperature

Leave a Comment

×