CPU temperature of Raspberry Pi in C#
Asked Answered
S

4

14

I have read a lot of articles and forum posts about this topic, but almost everything is quite complicated and all from over 2+ years ago.

So I was wondering, What is the best way tot get the CPU temperature of the Raspberry Pi in C#?

I have the newest version of Windows 10 IoT Core.

Update

I dont want to reduce the CPU temperature. I am using the Sense HAT. The temperature sensor of that thing is located near the CPU so the reading is inaccurate. I have found a python script what makes the reading accurate, but it needs the CPU temperature.

NOTE:

I am using Windows 10 IoT Core! I DO NOT have this file: /sys/class/thermal/thermal_zone0/temp

Update 2:

If the only way is to write your own driver, please guide me how to do that. I don't know anything about writing drivers or anything so low-level...

Serapis answered 29/8, 2018 at 0:8 Comment(2)
Okay, but I am using Windows 10 IoT Core. Not Raspbian or any order Linux distro.Serapis
I dont want to reduce the CPU temperature. I am using the Sense HAT. The temperature sensor of that thing is located near the CPU so the reading is inaccurate. I have found a python script what makes the reading accurate, but it needs the CPU temperature.Serapis
L
3

As far as i know, there is no IoT SDK or powershell client could access to low-level BCM2836 peripherals.You need to write your own drivers. You can follow the code samples from https://github.com/ms-iot/bsp/tree/master/drivers, it's written under WDK framework so you'll need to get yourself familiar with that in order to get started.

Another option is, measure temperature using DS18B20 thermal sensors. This thermal sensor connects the raspberry pi via I2C. And you need to install Rinsen.OneWire package via nuget in your solution, this library is written in C# and it has built-in support for DS18B20 and DS18S20 sensors.

I'm not 100 percent sure this is the right way to go, but if someone could correct me or point out a better solution, I would be more than happy to learn from it.

Lippe answered 30/8, 2018 at 3:27 Comment(7)
Yeah, I have read such an answer on a question before. Only problem for me is that I never ever wrote my own driver before. I don't know C, or C++ so that's a bit hard for me. Isn't there any exciting driver what I can read the temperature from? Or guide I can use to make my own? And why can't I just access the low-level BCM2836 peripherals?Serapis
Unfortunately, there is no such driver on windows iot core to read the CPU temperature currently. You can post this issue with Feedback Hub app.Lippe
So you are telling me there is no simple way to get the CPU temperature???? How and why could this not be possible?Serapis
@LuukWuijster, yes, there is no simple way to get the CPU temperature on windows iot core. It is possible but you need to develop the driver.Lippe
Is there any guide available on how to do that?Serapis
Sorry, i have not found any document about that. You can post this issue via Feedback Hub app.Lippe
Okay, I awarded the bounty to you because this seems like the best "answer".Serapis
M
1

You can achieve this using MCP9808 chip.

There is an article by Jeremy Lindsay which walks you through the process of how to make it work using C# on Window 10 IoT Core.

https://jeremylindsayni.wordpress.com/2016/06/09/how-to-measure-temperature-using-c-raspberry-pi-windows-10-iot-core-and-the-adafruit-mcp9808-sensor/

Some excerpts from the article:

How does the MCP9808 work?

This sensor uses the I2C protocol, which is supported by Windows 10 IoT Core on the Pi 3. As there’s already extensive integration of this protocol in the framework, this makes coding significantly easier. The MCP9808 can be initialised using the slave address of 0x18, and the ambient temperature can be read back from two bytes of data after writing 0x05 to the device.

Connecting the MCP9808 to the Raspberry Pi

There are 4 pins on the MCP9808 temperature sensor that we need to use to get a basic temperature reading. I connected mine to the Pi using the pins specified below.

• Supply Voltage – Pin 2
• Ground – Pin 6
• Serial Clock – Pin 5 • Serial Data – Pin 3

Jeremy has also shared the code on GitHub: https://github.com/jeremylindsayni/Magellanic.Sensors.MCP9808

Hope this help!

Milord answered 1/9, 2018 at 9:24 Comment(1)
Thank you for answering, but this is not what I am looking for. First of all, all my pins being used up by the Sense Hat so I can't add some chip. Second, this is possible on Raspbian, so it's a software issue, not hardware, so it has to be possible on Win 10 IoT Core as well!Serapis
S
0

I think you could try to calculate the current CPU temperature by reading the current consumption and then using this formula to calculate the temperature.

I don't know how current cellphone manufacturers measure the device's temperature today. But this is not much different than how Nokia used to measure temperature based on IBATT (battery current) and VBATT (battery voltage) readings back in the day (when I worked there).

Smashandgrab answered 7/9, 2018 at 13:59 Comment(1)
I don't think it's possible to read the CPU current consumption in Windows 10 IoT Core.Serapis
S
0

Use the vcgencmd measure_temp command line tool, which is built into the Raspberry Pi. Capture the standard output into a string and parse it into a float/double.

You can use the System.Diagnostics.Process and ProcessStartInfo to run the tool and collect its output by subscribing to the Process.OutputDataReceived event.

Silvestro answered 6/11, 2023 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.