Sensing the Environment

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

  1. Within your favourite browser (we recommend Google Chrome) go to app.edublocks.org and press Enter . This will load the EduBlocks coding environment.
  2. Click on micro:bit and click OK to open the micro:bit coding editor.
  3. 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.
  4. Click on Basic . Click and drag a while True: block to the code area and attach it under from microbit import * block.
    Setting up the code area

Temperature Sensing

  1. Click on Basic . Click and drag an if True: block to the code area and attach it within the while True: block.
  2. 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.
  3. 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.
  4. Click on the 0 of the display.scroll block and type temperature(°) .
    Sensing Temperature code

We can now sense the temperature of the room you are in by shaking the micro:bit.

Light Sensing

  1. 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.
  2. 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.
  3. 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.
  4. 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 < .
  5. Within the first 0 of the if 0 > 0: block type display.read_light_level() and within the 2nd 0 type 100 .
  6. 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.
  7. Click on Basic . Click and drag an else: block to the code area and attach it under if display.read_light_level() < 100: block.
  8. Click on Display . Click and drag a display.clear() block to the code area and attach it within the else: block.
  9. 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 .
    Sensing Light code

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

  1. Click on Basic . Click and drag an if True: block to the code area and attach it under if button_a.is_pressed(): block.
  2. 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 .
  3. 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() .
    Compass sensing code

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

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

  1. Take the micro USB cable and connect the micro:bit to the computer.
  2. 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.