We are going to explore our environment by using the temperature sensor, light sensor and compass on the micro:bit.
What You Will Learn
How to:
Use the shake function.
Display a number on the LED matrix.
Use the temperature sensor.
Use the button function.
Use the if/else conditions.
Use comparison operators.
Display patterns on the LED matrix.
Clear the LED matrix.
Use the compass.
Use the light sensor.
Coding
Within your favourite browser (we recommend Google Chrome) go to app.edublocks.org and press Enter . This will load the EduBlocks coding environment.
Click on micro:bit and click OK to open the micro:bit coding editor.
Click on Basic . Click and drag a from microbit import * block to the coding area and drop it or if you are using the new editor connect it under the # start code here block.
Click on Basic . Click and drag a while True: block to the code area and attach it under from microbit import * block.
Temperature Sensing
Click on Basic . Click and drag an if True: block to the code area and attach it within the while True: block.
Click on Accelerometer . Click and drag an accelerometer.is_gesture(‘shake’) block to the code area and attach it within the True of the if block.
Click on Display . Click and drag a display.scroll(0) block to the code area and attach it within the if accelerometer.is_gesture(‘shake’): block.
Click on the 0 of the display.scroll block and type temperature(°) .
We can now sense the temperature of the room you are in by shaking the micro:bit.
Light Sensing
Click on Basic . Click and drag an if True: block to the code area and attach it under the if accelerometer.is_gesture(‘shake’): block.
Click on Buttons . Click and drag a button_a.is_pressed() block to the code area and attach it within the True of the if block.
Click on Basic . Click and drag an if True(): block to the code area and attach it within the if button_a.is_pressed(): block.
Click on Basic . Click and drag a 0 = 0 block to the code area and attach it within the True of the if block. Click on the = and click on < .
Within the first 0 of the if 0 > 0: block type display.read_light_level() and within the 2nd 0 type 100 .
Click on Display . Click and drag a display.show(Image.HAPPY) block to the code area and attach it within the if display.read_light_level() < 100: block.
Click on Basic . Click and drag an else: block to the code area and attach it under if display.read_light_level() < 100: block.
Click on Display . Click and drag a display.clear() block to the code area and attach it within the else: block.
Click on Basic . Click and drag a sleep(1000) block to the code area and attach it under the else: block. Click on 1000 and type 2000 and press Enter .
We can now use the micro:bit to detect if it’s light or dark within a room by pressing the A button on the micro:bit.
Compass Sensing
Click on Basic . Click and drag an if True: block to the code area and attach it under if button_a.is_pressed(): block.
Right-click on button_a.is_pressed() block and click duplicate . Attach the duplicated block within the True of the if block. Click on a and click b .
Right-click on display.show(Image.HAPPY) and click on duplicate . Attach the duplicated block within the if button_b.is_pressed(): block. Click on Image.HAPPY and type compass.heading() .
We can now see what the compass baring of the direction the micro:bit is facing by pressing the B button on the micro:bit.
Completed Code
We are now ready to download the code to our micro:bit so we can go out and sense our environment.
Downloading the Code
Take the micro USB cable and connect the micro:bit to the computer.
Click Download Hex . Within the file explorer click on MICROBIT to open the drive and click save to download your code.
Now that you have downloaded the code to your micro:bit attach a battery pack to your micro:bit and go and sense your environment.