micro:bit Educator Lab Pack Experiment Guide

Contributors: Ell C
Favorited Favorite 1

Experiment 1: Reading the Temperature Sensor


A temperature sensor is exactly what it sounds like --- a sensor used to measure ambient temperature. The micro:bit has a temperature sensor sneakily built into it's processor chip. In this experiment we'll read that sensor and set up the micro:bit to display the ambient air temperature in both Celsius and Fahrenheit!

Parts Needed

You will need the following parts:

  • 1x micro:bit
  • 1x Micro-b USB Cable

Run Your Script

Either copy and paste, or re-create the following code into your own MakeCode editor by clicking the open icon in the upper right-hand corner of the editor window. You can also just download this example by clicking the download button in the lower right-hand corner of the code window.

Note: You may need to disable your ad/pop-up blocker to interact with the MakeCode programming environment and simulated circuit!

Code to Note

Let's take a look at the code blocks in this experiment.

[[micro:bit code block in MakeCode](https://cdn.sparkfun.com/assets/learn_tutorials/4/9/9/MicroBitLabPackTemperatureCode.png)

If you are having a hard time viewing this code, click on the image above to get a better look!

Set degreesC

Since the microcontroller also doubles as a temperature sensor, temperature is a built in value. In the Variables section of the makecode IDE, you can choose to create a new variable based on that temperature value.

Show Number

The Show Number block accepts a number value (or variable) and then displays it on the LED array.

Show String

The Show String block scrolls the string across the LED array after your temperature value.

Setting and Calculating degreesF

The Fahrenheit temperature can be calculated using blocks from the Math section. To convert C to F, the equation is F = C x 9/5 + 32. You can then display the Fahrenheit temperature using the same blocks you used to display the Celsius temperature.

What You Should See

When your micro:bit turns on, the temperature reading in Celsius will be displayed and scrolled across the LED array followed by the temperature reading in Fahrenheit.


Temperature Value is Unchanging

Try pinching the sensor with your fingers to heat it up or pressing a bag of ice against it to cool it down.

Temperature Value is Unchanging

Try holding the sensor near a heater (NOT TOO CLOSE!!) to heat it up or press a bag of ice against it to cool it down.