I know you can get the temperature using the tool vcgencmd but I was wondering if there is any API I can use in C to get the temperature programatically without reading the stdout of vcgencmd ?

up vote 13 down vote accepted

You can read the file /sys/class/thermal/thermal_zone0/temp, as specified in this answer. There it's about measuring from the command line, and the file is read with cat. But you should be able to just open the file in C. The temperature is returned in milli-degrees Centigrade and as ASCII numbers. Perhaps like this, not tested :)

FILE *temperatureFile;
double T;
temperatureFile = fopen ("/sys/class/thermal/thermal_zone0/temp", "r");
if (temperatureFile == NULL)
  ; //print some message
fscanf (temperatureFile, "%lf", &T);
T /= 1000;
printf ("The temperature is %6.3f C.\n", T);
fclose (temperatureFile);
  • Experience has lead me to believe the high level stream interface is not reliable on proc/sys files, and you should use open() and read() instead -- but YMMV. There's also a "libsysfs", but it's been abandoned and the kernel devs have explicitly told people to stay away from it. – goldilocks Apr 13 '13 at 11:58
  • @goldilocks Interesting! I did not know this, thought it just behaves like any file. You may very well be right. – Frepa Apr 13 '13 at 15:21

As @Frepa said, there is kernel thermal driver for RaspberryPi now so the easiest way is to use it. As far as I know, that's not the way vgencmd works however. It was able to read the temperature even before thermal kernel driver existed. It is using special communication mechanism available on RaspberryPi called mailbox in order to communicate with VideoCore OS running on GPU. It is partly documented here.

  • The Baking Pi course has some more mailbox material, however there it is used only for graphics. – Frepa Apr 12 '13 at 15:49

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.