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.
- Getting Started With Micro:Bit
- Display Your Name on Micro:Bit
- Make Your Micro:Bit Talk
- How To Control Mini-Servo
The Micro:Bit doens'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.
- Micro:Bit make code
- MicroPython programming
- JavaScript
First, we will see how we can code using block editor.
Step 1So, go to micro:bit makecode website.
Step 2Start a new project.You will see something like this.
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, 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 4So now, go to Variable and choose set to block and place it inside the forever block.
Now go to Input and choose the temperature and place it inside the set to block.
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:
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.
Now let's move to micropython code.
Open your mu Code edior and write the following code.
from microbit import *
while True:
temp = temperature()
display.scroll(str(temp) + 'C')
sleep(500)
Now let us analyze the above code:
- 1st lineThis line explains that we're importing micro:bit library which contains all the functions afor micro:bit .
- 2nd lineThis line includes the while loop and it is used to display the temperature continuously.
- 3rd lineIn this line, we have two parts; temp, which is simply a variable, and temperature (), which is a built-in function which 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 lineIf 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 lineThis 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 :)
Comments