I am working on the kind of RPi platform to collect some external sensors data or control relays (using the remote tools like web or sms).

Next problem I am tring to solve now is to get the direct access to one-wire or internal temp sensor.

Most examples I have found seems to access data by reading the system/drivers files.

What I am interested in is the direct access just from the cpp code.

Any ideas ?


If by "internal CPU temp" you mean the sensor built into the pi's SoC, I guess you could get direct access by mmap()ing the right part of the kernel, but since it already exposes an inteface to userspace, that seems a bit silly.

Where it's exposed depends on how your kernel is configured (see "Thermal Sensor" at the bottom here), but it is somewhere in /sys and the value is in thousandths of a degree Celcius.

Files in /sys are not real files. They are just nodes in userspace. This is a language agnostic kernel interface. When you read one, you are requesting information from the kernel; when you write to one, you are sending it information. So there is no instrinsic I/O overhead.

Occasionally I run across people who do not like this; they want a fancier API with system calls or ioctl() options or something generally more complicated looking (because of course, more complicated == better and faster...or maybe not). But open(), read(), and write(), (or higher level equivalents) necessary to using the language agnostic interface are system calls.

That's how it works. If that is distasteful to you, you'll have to write your own driver, or use the pi-specific stuff joan pointed to1 -- but that seems a complete waste of time. You just want a single scalar, the CPU temperature. This wheel has been invented already.

direct access just from the cpp code

Note that (excepting use of mmap()) you cannot do that with hardware, period. That is part of the purpose of an operating system kernel. If you are not familiar with the distinction between kernel space and user space, now is the time to start learning.

1. Since vcgencmd can also provide this information, you might look at the stuff in /opt/vc (on Raspbian) to see if there is any relevant source there -- but again, why bother?

| improve this answer | |
  • You did it. I agree - "that seems a bit silly" :) Thanks – Paul Paku Jun 6 '15 at 13:00
  • Note I'm not sure if that driver is loaded automatically (just noticed it isn't on one of mine here, but I don't use the stock kernel). Try modprobe bcm2835-hwmon and if that's "not found", modprobe bcm2835-thermal. You should then be able to find the relevant nodes somewhere in /sys, lol, although the names might not be exactly as they are in that other post. You can check them easily, e.g., cat temp1_input, = 40622. – goldilocks Jun 6 '15 at 13:32

You can use the mailbox interface from C/C++ to get the SOC's temperature.

I'm not sure what you mean re the 1-wire bus. You could bit bang the protocol to read a DS18B20 directly if you want. There is C code to do so available for the Raspberry Pi (some uses the UART for the timing, some doesn't).

However I can see no advantage over using the standard driver.

| improve this answer | |
  • Thank you, will read more on it but from my point of view and based on @goldilock post, it seems, I will stay with drivers finally.. – Paul Paku Jun 6 '15 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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