BBC micro:bit Cool Colours Temperature Sensor featured image
microbit BBC micro:bit - Kitronik University This Kitronik University resource is part of the BBC micro:bit partnership and is an in depth tutorial into making a temperature sensor with the BBC micro:bit.

microbit kitronik_microbit_logo_870

This is a simple tutorial demonstrating how to use an analogue input with the BBC micro:bit. The BBC micro:bit is able to read analogue voltages from sensors like Light Dependent Resistors (LDRs) and Thermistors. This tutorial uses the analogue read function to change the colour of a tri-colour LED to correspond to the temperature of the thermistor. A thermistor is a special type of resistor that changes its resistance as it gets warmer.

BBC microbit Cool Colours Temperature Sensor:

microbit bbc_microbit_cool_colours_temperature_sensor_01_560

Learn how to:

  • Program your BBC microbit to read an external sensor.
  • Control a tri-colour LED.
  • Measure temperature increases!

Level of difficulty:

  • Beginner.

Parts List

In order to make your BBC microbit Cool Colours Temperature Sensor you will need:

You will also require the following equipment:

  • A computer with a USB port and internet access.

Step by Step Guide to making your "BBC micro:bit Cool Colours Temperature Sensor"

Step 1

microbit bbc_microbit_editor_editors_870

Go to the BBC micro:bit website, click "Create Code" then look for the Microsoft Research blocks banner and click "New Project". Pick a name for your project and click create! Let's practice using the microbit block system to build some code. The blocks are organised into categories and the categories are listed in a column down the left hand side of the screen.

Step 2

microbit bbc_microbit_dont_steal_my_microbit_alarm_step2_870

First we need to create a "forever" loop. This is the block that the rest of our code will sit inside. The "forever" loop runs all the blocks inside it starting from the top and working its way to the bottom. Once it gets to the bottom block it starts again at the top and this process will continue forever. Click on the "Basic" category to open it and then drag the "Forever" block into the workspace. (This is the big white area in the middle of the screen). The forever block is useful as it runs the program again from the beginning once all of the steps have been completed.

Step 3

microbit bbc_microbit_cool_colours_temperature_sensor_step3_870

Our code will read from a sensor and use that reading to control the LED. This will require us to store and use data from a pin on the BBC micro:bit. To do this first we will create a variable, this is a location where we can store data in the BBC micro:bit's memory. We will then pull data from the pin and store it in that variable. Grab the “Set item to” block from the variables category. Rename the variable by clicking the dropdown and call it "TemperatureRaw". We will use this variable to store data from the thermistor. We call it "Raw" because the data stored will be a number between 0 and 1024 as this is how the BBC micro:bit sees analogue data, not a temperature in degrees. Drop this block into the forever block you just placed and you should find it snaps into position.

Step 4

microbit bbc_microbit_cool_colours_temperature_sensor_step4_870

Now that we have a variable ready to store temperature information we need to get the information from the sensor and put it into that variable. The way to do this is to perform an "analog read". Attach an "analog read pin" block to the variable you just made. The "analog read pin" block is used to read data from a pin that you can select by clicking the drop down menu to the right hand side of the block. These blocks, when combined, will read the value from the selected pin and set the variable you created to that value.

Step 5

microbit bbc_microbit_cool_colours_temperature_sensor_step5_870

Let's put that variable to use with an "If" statement. An "If" statement will perform a process but only if certain conditions are met. They are great as they allow us to create programs that react to changes. In this case we will use the "if,do,else" block. This lets us set a condition (the if part) and tell the BBC micro:bit what to do if the condition is met (the do part) or what to do if the condition is not met (the else part). We can set a condition using blocks in the Logic and Maths categories and a process that will happen if the condition is met. Create a condition that checks if the variable "TemperatureRaw" goes over 520. The "Do" part should perform a digital write (1) to pin P1 and a digital write (0) to pin P2. It should also display the number stored in "TemperatureRaw" on the LED display. The "Else" part should perform a digital write (1) to pin P2 and a digital write (0) to P1. It should also display the number stored in "TemperatureRaw" on the LED display.

Step 6

microbit bbc_microbit_cool_colours_temperature_sensor_01_560

Now would be a good time to put what we’ve just done to the test. First however, we need to set up the hardware. Put the batteries into the battery holder and push the JST connector into the socket on the BBC micro:bit. Connect your thermistor to your resistor with one crocodile lead. It doesn’t matter which side of the resistor or thermistor you connect. Using another crocodile lead, connect the unattached leg of the resistor to the pin on your BBC micro:bit labelled "GND" and the unattached leg of the thermistor with a third crocodile lead to the pin labelled "3V". To finish connecting the sensor, connect the leg of the thermistor that is connected to the resistor with another clip to the pin on the BBC micro:bit labelled "0". This means that the thermistor has two crocodile clips on one of its legs. Finally we will connect the LED. The tri-colour LED will have 3 legs. If using a common cathode style tri-colour LED you need to connect the long centre leg to the pin on the BBC micro:bit labelled GND. Then connect one of the short legs to pin P1 and the other short leg to pin P2. It can be hard to tell which leg is the red LED and which is the green LED so if you notice the wrong colour LEDs coming on you may want to swap the legs that pins P1 and P2 are connected to.

Step 7

microbit bbc_microbit_emoji_bag_step10_870

Now, let’s try that out! Press compile* and after a few moments the code should appear as a download in your browser. If you plug your BBC micro:bit into a USB port it will show up as a storage device. Simply drag and drop the .hex file you just downloaded onto the BBC micro:bit. The file might not show up on the BBC micro:bit in the file explorer but it is there! Once the file has been transferred (the light on the BBC micro:bit will stop blinking rapidly) remove the BBC micro:bit from your computer. Now power up your BBC micro:bit with the batteries, then press the "reset" button next to the USB connector you should see the green LED light up. You should be able to warm the thermistor by holding it in your fingers or breathing on it. This will trigger the red LED. *If this is the first time you have used the compiler you will be asked to make an account. Ask your teacher for the code required to make an account.

Further Tasks

At the moment we just have a red or green light for hot or cold. The cool thing about tri-colour LEDs is that we can use the two colours simultaneously to mix and give an amber light. This also lets us take advantage of the analog read. Instead of having just "hot" and "cold" conditions we can add a "warm" condition too. Using the larger "If, else, do, else if, do, else" block we can set two "If" conditions and have them "do" two different things as well as giving a command what to do if neither condition is met. All with one decision block! Use the blocks in the Maths and Logic categories to create two "If" conditions. One that checks if the "TemperatureRaw" variable is between 520 and 550, this will be our "warm" condition. If this condition is met we need to do a digital write (1) to both pins P1 and P2, this will put both lights on giving us amber. The "else if" condition should check if "TemperatureRaw" goes above 550, if this condition is met we want to digital write (1) to P1 and digital write (0) to P2, this will put just the red light on. The last part of the block, the "else" statement, should digital write (1) to P2 and digital write (0) to P1, giving us just the green light.   Download a pdf version of this page here. pdf_icon.gif See the code on the BBC micro:bit website here.


Rob Haywood

Rob Haywood

Hi Lillian, The BBC micro:bit website is now live at Rob

Lillian Brevik

Lillian Brevik

Where do I find Microsoft Research blocks? "Go to the BBC micro:bit website, click “Create Code” then look for the Microsoft Research blocks banner and click “New Project”." I don´t find that page on BBC.

Leave a comment

All comments are moderated before being published