Your  microbit can sense temperature changes but it does not have a dedicated temperature sensor attached to it. Here's what the BBC website has to say:


"The BBC micro:bit does not have a dedicated temperature sensor. Instead, the temperature provided is actually the temperature of the silicon die on the main CPU. As the processor generally runs cold though (it is a high efficiency ARM core), the temperature is a good approximation of the ambient temperature... you might warm up if you give the processor a lot of work to do though, and don't sleep!

The temperature sensor has a high precision, but isn't trimmed for accuracy. In other words, it can sense changes in temperature very well, but there may be (and probably is) base line offset. i.e. it might return 20 degrees when it's actually 17, but it would return 21 when it is 18 etc."


This being the case, I knocked up a bit of code and hung it up against a mercury thermometer. Sure enough it was about six degrees over. So a small modification in the code was called for:

















Let's delve into it and see what these nine blocks are doing.


Firstly the whole thing is enclosed in a forever loop which means that it will just keep doing it round and round with no intervention from people (unless people disconnect the batteries in which case it won't).

This means the code is entirely line-driven.

The first line in the boundaries is setting a variable called temp to the reading from the temperature gauge that isn't there in degrees centigrade minus six, which takes off the six degrees over I measured against my mercury thermometer. This should give me the true temperature.

The next line is showing the number held in that  variable on the LED array. It will appear as a scroll if the number is a double digit and a permanent fix if single. If the number's triple  I don't know what it does and let's face it  if the temperature's greater than 99 degrees we've got bigger problems to solve than checking if this works.

Then it waits half a second to allow sleep so we get no self heating from the processor and begins all over again.


Does it work? Well, I had it hanging against my mercury thermometer for two days during which at a range between sixteen and twenty-three degrees it tracked it almost exactly. As I began to write this at 22:45 I chucked the microbit and mercury outside the back door. I'm leaving them there for an hour whilst I watch some sci-fi, have a smoke and drink more coffee than is good for me, and then I'll see what each one says.


So.......the Microbit went down to five and the thermometer stuck on eight. Bringing them back inside and leaving them, both are showing twenty. One of them is inaccurate and I doubt it's the mercury. The lesson here is we can use the Microbit to sense temperature rises and falls, but not as a real thermometer. At least not with the above hardware.


So let's do it as an approximate temperature gauge that's a bit cheeky. Let's do it step by step and explain what's going in to it as we go.


Firstly then we need to open a new project.



Temperature Sensing.

bbcmicrobit Go Back temperature start

When the workspace loads up we are going to use a user input that isn't really a user input except in the sense that it is. It isn't though. Even though it reacts to something the user's doing. It's an on shake event handler and you do this with it:

input menu shake

So this user input that isn't a user input although it does take an input from the user is going to do the following: when you shake the Microbit about it's going to tell you approximately how hot or cold it is.

It works because the Microbit is equipped with a doodad called an accelerometer. It measures how fast up, down, backwards, forwards and sideways the microbit is moving. The lines underneath give you an idea of how it works.







Measuring on these three straight line dimensions will let you know if the microbit is moving and which way. We aren't bothered about direction here as it's going to be a shake - as soon as the processor sees any lines going one way and then the other it knows a shake's occuring and does the hard work for us. So although it is user input, it's the acceloremeter that's doing the reading and inputting to the processor. So it's a system input. Activated by a user.......er, my head hurts now. Let's put something into the event handler:

renamed dialogue getvariable renamevariable

The first thing we need to do is get a variable in there as we need to adjust the temperature reading (by six degrees on my device) and we can't adjust the output. There's a number of steps to this and there they are on the left. Going clockwise, step 1 is to delve into the variables tab and drag out a set item to block. Then we click it in the event handler as in step 2. Hover over the term item and drop down the menu by clicking on it and when you see the drop-down click on Rename variable. It  brings up a dialogue box and you simply type in what you want to call the variable and click OK.


Once you've done that you'll see that the variable's name has changed to whatever you typed .


Now we need to give it a value. Over the page we look how.

More >