micro:bit MicroPython Tutorial: Getting temperature
DFRobot
Mar 11 20191192
In this microbit tutorial we will learn how to obtain temperature measurements using the Micro:Bit board.
Introduction
In this microbit tutorial we will learn how to obtain temperature measurements using the Micro:Bit board.
It’s important to mention that we are not going to need any additional sensor. Instead, we are going to use Micro:bit’s built in temperature measurement capabilities.
Nonetheless, the Micro:bit also doesn’t contain any dedicated temperature sensor. Thus, we will use the temperature sensing feature integrated in the nRF51822 chip that the Micro:bit contains.
This feature allows to obtain the chip die temperature [1]. Note however that the values obtained from these measurements are typically higher than the room temperature because they are also affected by the temperature of the electronics [2].
The code
The first thing we are going to do is importing the microbit module. Upon importing this module, we should have access to the function that allows to measure the temperature.
import microbit
Now that we have imported the module, we simply need to call the mentioned function that returns the temperature measurement. This function is called temperature and takes no arguments [3].
As output, the temperature function returns the measured temperature, in degrees Celsius [3]. Thus, we can directly print the output of this function call.
print(microbit.temperature())
The final code can be seen below.
import microbit
print(microbit.temperature())
Testing the code
To test the code, simply run the previous script on your Micro:bit board, using a tool of your choice. I’ll be using uPyCraft, a MicroPython IDE. Upon running the code, you should get an output similar to figure 1, which shows the measured temperature.
As already mentioned in the introductory section, we should be careful interpreting this value since it corresponds to the nRF51822 chip die temperature, which is expected to be higher than the room temperature, due to the heating generated by the electronics.
Starting Nov 21st (EST), grab a Mystery Box for just $1 and stand a chance to get Raspberry Pi 5B / LattePanda 3 Delta Single Board Computers, with guaranteed prizes worth over $15!
Explore the performance of Small Language Models (SLMs) on the Raspberry Pi 5 SBC, including models like mathstral, phi 3, llama 3.1, gemma2 2b, and Qwen.
Explore our top picks for interactive sensors, DIY lighting, essential maker tools, and exciting project ideas to make this Halloween truly unforgettable!