Getting the temperature

One of the new packages included in 2.0.0 is microcontroller.cpu.temperature: this gives you the temperature (in Celsius) of the board’s CPU. It isn’t an accurate room temperature sensor like the TMP36 or DHT22, since it’s the temperature of the CPU, not the ambient air, but it’s still a data point and it is very accurate!

Getting the temperature is as simple as can be. In the REPL:

Download: file
Adafruit CircuitPython 2.0.0 on 2017-09-12; Adafruit Gemma M0 with samd21e18
>>> import microcontroller
>>> microcontroller.cpu.temperature
29.6556

If you’d like it in Fahrenheit, you can create a function for that.

Download: file
>>> def fahrenheit(celsius):
...     return (celsius * 9 / 5) + 32
...
>>> fahrenheit(microcontroller.cpu.temperature)
90.1077

Once you've got all that working, we can go on to the next step, storage!

This guide was first published on Sep 27, 2017. It was last updated on Sep 27, 2017. This page (Getting the temperature) was last updated on Apr 23, 2019.