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 python.microbit.org and press Enter . This will load the Python coding editor.
  2. Delete the text on lines 6, 7 and 8 .
    Setting up the code area

Temperature Sensing

  1. From line 6 type the following code:
    Sensing Temperature code

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

Light Sensing

  1. From line 8 type the following code. Make sure your cursor is positioned under the if on line 6 .
    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. From line 14 type the following code making sure your cursor is under the if of line 8 .
    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 on Connect . Click on your micro:bit and click Connect again.
  3. Click Download .

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.