How To Read Temperature Using BBC Micro:Bit

Introduction

 
Today, I am going to write about how we can use our BBC Micro:Bit as a thermometer; meaning how we can read the temperature using Micro:Bit.
 
If you don't know about BBC Micro:Bit, then please go through these articles below. I hope you will get the basic idea of Micro:Bit.
Tools You Need
  1. Micro:Bit (1 pcs)
  2. USB Cable (1 pcs)
  3. Battery AAA 1.5 v(2 pcs)
  4. Battery Box (1 pcs)
  5. mu code editor
The Micro:Bit doesn't have any particular temperature sensor for reading the temperature. The temperature that we are going to read today is actually the temperature of the silicon die on the main CPU.
 
So now let us see how can we read temperature using micro:Bit.I am going to use three methods to read the temperature from Micro:Bit.
  1. Micro:Bit make code 
  2. MicroPython programming
  3. JavaScript
First, we will see how we can code using block editor.
 
Step 1
 
So, go to micro:bit makecode website.
 
Step 2
 
Start a new project. You will see something like this.
 
 
Step 3
 
Now, if you want to display anything on startup then  choose ok, otherwise delete the on start block and leave the forever block. I am going to use forever block.
 
Actually, the temperature variable is used to read the temperature from the micro:bit. So, we're going to read the temperature and assign it to a variable and display that variable.
 
Step 4
 
So now, go to Variable and choose set to block and place it inside the forever block.
 
 
Step 5
 
Now go to Input and choose the temperature and place it inside the set to block.
Step 6
 
Now go to the basic block and choose show number and place it right after the set item variable. Now go to the variable block and choose the item from there and attach that to show the number block something like this:
 
 
Step 7
 
Now click on Download and copy it to MICROBIT drive. And it is done.
Now if you will click on a javascript tab on make code site you will get the javascript code there.
 
 
Here is the Javascript code:
  1. let item = 0  
  2. basic.forever(() => {  
  3.     item = input.temperature()  
  4.     basic.showNumber(item)  
  5. })  
Now let's move to micropython code.
 
Open your mu Code edior and write the following code.
  1. from microbit import *  
  2. while True:  
  3.    temp = temperature()  
  4.    display.scroll(str(temp) + 'C')  
  5.    sleep(500)  
Now let us analyze the above code,
  • 1st  line
     
    This line explains that we're importing micro:bit library which contains all the functions for micro:bit .
     
  • 2nd line
     
    This line includes the while loop and it is used to display the temperature continuously.
     
  • 3rd line
     
    In this line, we have two parts;  temp, which is simply a variable, and temperature (), which is a built-in function that is used to read the temperature. So we're calling this function to read the temperature and storing the value in the temp variable.
     
  • 4th  line
     
    If you have gone through the previous article then you should see display and scroll so I am skipping this but just know that this is used to display or scroll the string or number on micro:bit.
     
  • 5th line
     
    This line is used to provide the delay which means we're telling our micro:bit to wait sometimes (i.e 500 ms) and then display the temperature.
We're done -- we just need to upload the code to micro:bit and see the output.
 
Click on the flash button and wait for a second after writing the code. 
 
 
You can see a DEMO HERE
 
I hope it helps. If there is any problem, please post in comment section.
 
Thanks :)