© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_12

12. Using Sensors

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

This chapter presents how to use sensors with micro: bit to sense the physical environment. It has some built-in sensors such as accelerometers, compasses, temperatures, lights, and touch. You can use them without attaching any external components to your micro:bit.

12-1. Using Built-In Accelerometer

Problem

You want to get the acceleration values in the left and right direction (x-axis).

Solution

  • In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the forever block.

  • In the Toolbox, click on the Input category. Then click and drag the acceleration (mg) block over, and place it inside the placeholder of the show number block. By default, the acceleration (mg) block outputs the acceleration values in the x-axis.

  • In the Toolbox, click on the Basic category. Then click and drag the pause (ms) block over, and place it underneath the show number block .

  • Once completed, your code should look like this (Figure 12-1 ).
    ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig1_HTML.jpg
    Figure 12-1.

    Full code listing

How It Works

The micro:bit has an on-board three-axis accelerometer chip that can be used to measure the acceleration . The accelerometer is internally connected to the micro:bit’s I2C bus. It measures the acceleration or movement along the three axes: x and y axes (the horizontal panes) and the z axes (the vertical pane), which it experiences relative to free fall. This is most commonly called the G-force. With the micro:bit’s accelerometer, you will get acceleration values in mG (milliG).

When you place the micro:bit board on the surface of the earth, it will measure acceleration due to the earth’s gravity, straight upward of g~9.81 m/s2. The micro:bit accelerometer can measure accelerations between +2g and −2g. This range is suitable to use with a wide range of applications.

The acceleration (mg) block outputs the acceleration values in one of three directions (x, y, and z) or as the strength of acceleration from all three directions (dimensions). Following are the options that you can choose to get the output values:
  • x - Outputs the acceleration values in the x-axis. Put your micro:bit on a level table with the screen pointing up. Initially, x=0, y=0, and z=-1023. Now, tilt your micro:bit board from the left to right or the right to left. Your micro:bit will display values ranging from −1023 to +1023.

  • y - Outputs the acceleration values in the y-axis. Put your micro:bit on a level table with the screen pointing up. Initially, x=0, y=0, and z=-1023. Now, tilt your micro:bit board forward and backward. Your micro:bit will display values ranging from −1023 to +1023.

  • z - Outputs the acceleration values in the z-axis. Put your micro:bit on a level table with the screen pointing up. Initially, x=0, y=0, and z=-1023. Now, move your micro:bit up and down. Your micro:bit will display values ranging from −1023 to +1023.

  • strength - Outputs combined force in all directions (x, y, and z) also known as the overall acceleration. The overall acceleration can be calculated by the Pythagorean theorem . The formula uses the acceleration along the x, y, and z axes as shown below.

$$ acceleration=sqrt{x^2+{y}^2+{z}^2} $$
The same formula can be implemented with MakeCode as shown in Figure 12-2 .
../images/474604_1_En_12_Chapter/474604_1_En_12_Fig2_HTML.jpg
Figure 12-2.

Displaying overall acceleration

Watch this great video located at https://youtu.be/byngcwjO51U to learn how the accelerometer on micro:bit works .

12-2. Using Gestures

Problem

You want to display a random number from 1 to 6, when you shake your micro:bit .

Solution

  • In the Toolbox, click on the Input category and then click on the on shake block.

  • In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on shake block.

  • In the Toolbox, click on the Math category. Then click and drag the pick random block over, and place it inside the placeholder of the show number block.

  • In the pick random block, type 1 for the minimum and 6 for the maximum value.

  • Once completed, your code should look like that in Figure 12-3 .
    ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig3_HTML.jpg
    Figure 12-3.

    Full code listing

How It Works

The micro:bit’s built-in accelerometer can also be used to create interactive applications based on gestures . In MakeCode, on shake is the default block for gesture detection. If you want to test different gestures, click on the drop-down list and choose one of the following.
  • Shake

  • Logo up

  • Logo down

  • Screen up

  • Screen down

  • Tilt left

  • Tilt right

  • Free fall

  • 3g

  • 6g

  • 8g

Figure 12-4 shows the graphical representation of each gesture so that you can get an idea about how to make gestures with micro:bit by holding the micro:bit in your hand .
../images/474604_1_En_12_Chapter/474604_1_En_12_Fig4_HTML.jpg
Figure 12-4.

Accelerometer gestures

12-3. Using Compass

Problem

You want to find which direction on a compass the micro:bit is facing.

Solution

  • In the Toolbox, click on the Variables category. Next, click on the Make a Variable button. In the New variable name window, type degrees. Then click on the Ok button.

  • In the Toolbox, click on the Input category. Then click and drag the compass heading block over, and place it inside the placeholder of the of the set degrees to block.

  • In the Toolbox, click on the Logic category. Next, click and drag the if-then-else block over, and place it underneath the set degrees to block. Then add more else if branches as shown in Figure 12-5 . Use the show arrow block to display different directions.

  • The conditional statements for if, else if, and else are as follows:
    • If degrees < 45, then show arrow north

    • If degrees < 135, then show arrow east

    • If degrees < 255, then show arrow south

    • If degrees < 315, then show arrow west

    • Else, show arrow north
      ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig5_HTML.jpg
      Figure 12-5.

      Full code listing

How It Works

The dedicated magnetometer chip located on the back of your micro:bit measures the compass heading from 0 to 359 degrees. If the compass is not ready, it returns −1003. The micro:bit compass is based on the NXP/Freescale MAG3110, which is a three-axis magnetometer sensor that can be accessed through the I2C bus. The compass can also act as a metal detector.

In the above solution under Recipe 12-3, the following ranges of values are used to find the direction the micro:bit is facing:
  • North: 315–44 degree

  • East: 45–134 degrees

  • South: 135–224 degrees

  • West: 225–314 degrees

12-4. Calibrating the Compass

Problem

You want to calibrate the built-in compass .

Solution

  • In the Toolbox, click on the Input category. Then click and drag the calibrate compass block over, and place it inside the on start block (Figure 12-6 ).
    ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig6_HTML.jpg
    Figure 12-6.

    Full code listing

How It Works

Before using the compass, you should calibrate it to ensure correct readings. It is also wise to calibrate the compass each time you use it in a new location.

In some situations, when the compass needs to be calibrated, the micro:bit will automatically prompt the user to calibrate it. However, the calibration sequence can also be manually started with the calibrate compass block .

You can place the calibrate compass block at any point in your code, when you need to calibrate the compass. Sometimes the compass may not work even after calibration. It can give spurious results, so it shouldn’t be relied on fully for navigation.

12-5. Using Built-In Temperature Sensor

Problem

You want to read the air temperature surrounding your micro:bit in Celsius .

Solution

  • In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the forever block.

  • In the Toolbox, click on the Input category. Then click and drag the temperature block over, and place it inside the placeholder of the show number block.

  • In the Toolbox, click on the Basic category. Then click and drag the pause (ms) block over, and place it underneath the show number block.

  • Once completed, your code should look like the following (Figure 12-7 ).
    ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig7_HTML.jpg
    Figure 12-7.

    Full code listing

How It Works

The micro:bit doesn’t have a dedicated temperature sensor. Instead, the temperature block outputs the temperature of the micro:bit’s main CPU. The temperature is a good approximation of the air temperature where your micro:bit is kept and known as ambient temperature .

In the above solution under Recipe 12-5, the temperature block outputs the CPU temperature in Celsius. The forever and the show number blocks are used to continually update and display the temperature values on the micro:bit’s LED screen.

If you want to display the temperature in Fahrenheit , implement the following formula to convert Celsius into Fahrenheit using blocks.
  • Fahrenheit = ((Celsius x 9) / 5) + 32

Figure 12-8 shows how to arrange blocks to convert Celsius to Fahrenheit .
../images/474604_1_En_12_Chapter/474604_1_En_12_Fig8_HTML.jpg
Figure 12-8.

Converting Celsius to Fahrenheit

12-6. Using Built-In Light Sensor

Problem

You want to find the light level around your micro:bit .

Solution

  • In the Toolbox, click on the Variables category. Next, click on the Make a Variable button. In the New variable name window, type reading. Then click on the Ok button.

  • In the Toolbox, click on the Input category. Then click and drag the light level block over, and place it inside the placeholder of the set reading to block.

  • In the Toolbox, click on the Led category. Then click and drag the plot bar graph of block over, and place it underneath the set reading to block.

  • In the Toolbox, click on the Variables category. Next, click and drag the variable named reading over and place it inside the first placeholder of the plot bar graph of block . Then type 255 in the second value box.

  • Once completed, your code should look like the following (Figure 12-9 ).
    ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig9_HTML.jpg
    Figure 12-9.

    Full code listing

How It Works

The micro:bit doesn’t have a dedicated light sensor. Instead, when you shine light on the front of your micro:bit, it measures the capacitance across a number of LEDs on the front of the board . Then these values are averaged together and give you a number between 0 and 255. The 0 indicates darkness and the 255 indicates bright light. The plot bar graph block is used to display a vertical bar graph based on the light level.

12-7. Using Touch Pins

Problem

You want to display the happy icon when you touch the pin 0 .

Solution

  • In the Toolbox, click on the Input category and then click on the on pin P0 pressed block.

  • In the Toolbox, click on the Basic category. Now, click and drag the show icon block over, and place it inside the on pin P0 pressed block. Then choose the happy icon from the drop-down list.

  • Once completed, your code should look like that shown in Figure 12-10 .
    ../images/474604_1_En_12_Chapter/474604_1_En_12_Fig10_HTML.jpg
    Figure 12-10.

    Full code listing

How It Works

Micro:bit board has three specialized pins in the edge connector with large pads, known as touch pins. They are pins 0, 1, and 2. These pins can be used to build touch-sensitive applications based on the analog input. The large connector pads allow you to touch them with your fingertips to change the capacitance of the internal circuit .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.197