Make a thermometer with the BBC micro:bit

Wednesday, 10 August 2016

Have you ever wondered how hot or cold your room gets? well now with the BBC micro:bit and an inexpensive sensor you can!

In this tutorial I'll show you how to turn your BBC micro:bit into a thermometer using a TMP36 and programming it using the Microsoft Block editor. For this lesson you will need the following:

If you want to buy all of these parts then you can click on this wish list where you can click on the tick box next to the parts you want to buy.

First up lets hook everything up:

With the flat side of the sensor facing you the pins from left to right need hooked up to 3V - Pin0 - Ground on the BBC micro:bit.

hooking up a tmp36 to a bbc micro bit with croc clips

Now you've connected the sensor you can plug the USB cable into the micro:bit and your computer, done? lets get coding!

In this example I'm going to use the Microsoft Block editor.

First up we need the following blocks:

Basic

  • BBC micro bit Forever block

Variables

  • BBC micro bit set var to block

Pins

  • BBC Micro bit analog read pin block

We are using a forever block as we want everything within it to run then start running again looping round forever, first thing we need to do is change the variable name to tmp36 do this by clicking on the drop down after set and name it tmp36, next clip the analog read pin to the set tmp36 to. This will take the reading from Pin 0 and place that number into the variable tmp36. If you now clip this inside forever it will now get a reading from pin 0, update the value in tmp36 and then loop back to get another reading from pin 0 and continue to do so until the micro:bit runs out of power. Once it's all clipped together it should look like this:

Setting BBC Micro bit variable with sensor reading

next we want to convert tmp36 to a new variable called temp. Hold on now this bit requires some…

…Maths! Yay!!

So why all the maths? Well the TMP36 outputs an analogue voltage from 0-3300 mV because of this we have to read the voltage then convert this number into a temperature we can understand, the following formula is what we need to use:

Millivolts = (the reading from pin0) * (3300/1024)

We then convert the millivolt reading in to Centigrade using the following

Centigrade = [(millivolt) – 500] / 10

The square brackets mean this sum needs done before you divide it by 10.

So how the heck do we do this? Well we could do each part separately creating lots of variables to store all of these values, but, this could get a little messy and why have more lines of code than we need?

For this bit we need the following blocks:

Variables

  • BBC Micro bit set var temp to block

Maths

  • BBC Micro bit devide 3300 by 1024 block
  • BBC Micro bit devide block by 10
  • BBC Micro bit minus by 500 block
  • BBC Micro bit variable multiplied by a value block

Putting these together can be a little daunting but here we go:

BBC Micro bit formula to get temperature from a TMP36

Okay so we've done the maths but how do we actually know what the temperature is? we should probably display the current temperature on the LED display, you'll need the following blocks for this part:

Basic

  • BBC Micro bit show number block

Variables

  • BBC Micro bit temp var block

If we take the variable temp now and clip it into the show number block like this:

  BBC Micro bit show number with temp variable block

this will take the current temperature in centigrade and display it on the micro:bit.

If you hit compile now and then copy the file to your micro:bit it should be displaying the current temperature, if not then best check your code and your connections.

wouldn't it be cool (or hot) if we could press a button to show us the hottest or coolest temperature detected as well as seeing the current temperature? in order to do this we need to store the highest and lowest temperatures recorded and update them if the current temperature is greater or lower than that already recorded how do we store and update them? what do we need to achieve this? Variables, it's only logical.

You will need the following blocks:

Logic

  • BBC Micro bit if do block  BBC Micro bit if do block
  • BBC Micro bit less than logic block
  • BBC Micro bit greater than logic block

Variables

  • BBC Micro max variable blockBBC Micro max variable blockBBC Micro max variable blockBBC Micro max variable block
  • BBC Micro min variable blockBBC Micro min variable blockBBC Micro min variable blockBBC Micro min variable block
  • BBC Micro temp variable blockBBC Micro temp variable blockBBC Micro temp variable blockBBC Micro temp variable blockBBC Micro temp variable block
  • BBC Micro set item to blockBBC Micro set item to block

What we now need to do is to check that the variable "temp" is higher than the "max" variable or lower than the "min" variable and update either "max" or "min" if required. For this we need to use logic that's where the if-do block comes in we want if temp is higher than max - do change max to the same as temp

Once its all put together it should look like this:

BBC Micro bit reading a tmp36 displaying and storing the min max temperatures code

Now we have the bulk of the program done we need to add a bit of interactivity and make the buttons display the "min" and "max" values stored.

You will need the following blocks:

Input

  • BBC Micro bit on button press block

Maths

  • BBC Micro bit maths number block BBC Micro bit maths number block

Basic

  • BBC Micro bit show number block BBC Micro bit show number block

Variables

  • BBC Micro max variable block
  • BBC Micro min variable block

What we need to do is make 2 on button pressed blocks and we want them to do the following, when button A is pressed display the "min" temperature on the display so we will make them look like this:

BBC Micro bit show variable min when button A is pressed BBC Micro bit show variable max when button B is pressed

When we now compile and run this the temperature will display and when you press B it will display the highest temperature recorded and if you press the A button it will most likely show 0, why? it's not that cold in here! well the variable min and max both get set to zero when they are created and because your room is probably over 0 centigrade then the micro bit wont update the min value. To get round this and to add a little more functionality we will add a min max reset option using an A+B button press.

You will need the following blocks:

Input

  • BBC Micro bit on button A+B pressed block

Variables

  • BBC Micro bit set item to block BBC Micro bit set item to block

Maths

So what we are going to do is say when A+B are pressed together set min to 100 and max to -100, the reason for this is it's likely the temperature in your room will fall between -100 and +100 centigrade and we make the min +100 and the max -100 so the reading it takes will update both. It should look like this:

BBC Micro bit when button A+B are pressed set variable min to 100 and variable max to -100

So that's us all done, we are taking an analog signal from a sensor, doing maths to convert that signal into a usable temperature, displaying it on a screen and updating a min and max variable while having the ability to reset and display stored variables.

Links and download

Cant see the video? Click Here to view on our YouTube Information channel

Using a TMP36 with the BBC micro:bit code

BBC micro:bit site

 

 

 

Free Technical Support
Fast Delivery Options
Customer Loyalty Scheme

Proto-PIC - UK Distributor of Sparkfun, Arduino, Adafruit, BBC Micro Bit electrical componentsProto-PIC ; UK Distributor of Sparkfun, Arduino, Adafruit, BBC Micro Bit electrical components