In this lesson, we will carry out an interesting experiment to make a pedometer with the Micro:bit.
11.1 Components to be prepared
11.2 LED display
The front LED display of Micro:bit consists of 25 5x5 lattice LEDs.
25 LEDs arranged in a 5x5 grid make up the display for showing pictures, words and numbers.
11.3 Accelerometer
(1) An accelerometer sensor is integrated on the Micro:bit. This sensor can measure the data on x,y and z axes, respectively. According to the data, the direction of the resultant acceleration can be obtained. When only gravity is applied to the Micro:bit, the acceleration of gravity is downward. According to the data on the x, y, and z axes, the direction of the acceleration of the Micro:bit can be determined. The use of the accelerometer can detect the shakes to realize the step counting. Every time a shake is detected, the pedometer step will be increased by 1.
(2)An accelerometer is a motion sensor that measures movement. The accelerometer in your BBC micro:bit detects when you tilt it left to right, backwards and forwards and up and down. There are lots of ways you can use the accelerometer in your projects. Find out more about how it works by watching the video, then choose a project to get started.
https://microbit.org/get-started/user-guide/features-in-depth/#accelerometer
11.4 Circuit
Connect micro:bit and PC with a Micro USB cable.
11.5 MakeCode programming
We will use an online MakeCode Editor to complete the experiment in this lesson, as shown below.
11.5.1 Start programming
(1) Log in to the website
1. You need to enter the URL in the address bar of Google Browser:
https://makecode.microbit.org/
2. After the website is successfully opened, the interface as shown below will appear:
(2) Import a project
1. In the HOME interface, click the "Import" button to import the external ".hex" file:
In the pop-up dialog box, select the "Import File", as shown in the following figure:
Click the "Choose File":
Find the code file for this lesson:
BBC _Microbit_Kit\Code\Lesson_11\BlockCode
Select the file in ".hex" format and click the Open:
2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:
3. Open the file successfully, as shown in the following figure:
11.5.2 Run the program
1. After the program is written, connect micro:bit and PC with a Micro USB cable.
2. After micro:bit is connected to the computer, you need to first "Pair device". Click the button on the right of in the lower left corner, and then click the option, as shown in the following figure:
Then click in the lower right corner:
Then the following dialog box will pop up, select , and then click
After the device is successfully paired, the button changes to
3. Start to download the program to Micro:bit, and click the button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded.And the LED screen of the Micro:bit will display the number 0 by default, as shown in the figure below:
4.You need to shake the Micro:bit (walk with a Micro:bit, and shake it once at every step) and observe the change of the number on the LED screen.
[Note]:
If Micro:bit doesn't respond after clicking the , you need to click the button on the right of the , and then click the , and observe the situation of the Micro:bit again, as shown in the following figure:
If you have problems, please send us an email: support@adeept.com
11.5.3 Learn the code program
In the program, we use the following instruction blocks, which are explained as follows:
Block | Function |
| This is an instruction block to detect that the Micro:bit is shaken. Besides, it can be modified to the state such as detecting tilt. |
| This is generated by the variable step created by ourselves, and it’s an instruction block that can initialize the variable step. |
| This is an instruction block that can change the variable step. |
“shake”uses the accelerometer on the Micro:bit. More information about the use of the accelerometer is available in the following link:
https://microbit-micropython.readthedocs.io/en/v1.0.1/accelerometer.html
11.6 Python programming
11.6.1 Run the program
1.Connect micro:bit and PC with a Micro USB cable.
2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:
Find the code file for this lesson:
BBC _Microbit_Kit\Code\Lesson_11\PythonCode
Select the file in ".py" format and click the Open:
3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:
4.After the program is downloaded, you need to shake the Micro:bit and observe the change of the number on the LED screen.
【Note】:
After you click the [Flash] button, if there is no change on the LED screen of Micro:bit, you need to restart the Micro:bit, and then click the [Flash] button again.
If you have problems, please send us an email: support@adeept.com
11.6.2 Learn the code program
The source codes are as follows:
1 2 3 4 5 6 7 8 9 10 | from microbit import * step = 0 display.show(step) while True: if accelerometer.was_gesture('shake'): step += 1 display.scroll(step) if button_a.is_pressed(): step = 0 display.show(step) |
(1) The method of the was_gesture('shake') can judge whether the Micro:bit is shaken. Each time the Micro:bit shakes, the pedometer step will plus 1 and the accumulated data will be output onto the LED screen through the display.scroll(step).
5 6 7 | if accelerometer.was_gesture('shake'): step += 1 display.scroll(step) |
(2) The method of the button_a.is_pressed() can judge whether the button A on the Micro:bit is pressed. If the button A is pressed, the pedometer step will be reset to 0 and the data will be output onto the LED screen through the display.show(step).
8 9 10 | if button_a.is_pressed(): step = 0 display.show(step) |
(3)In the code program, we use the“accelerometer”library. More information about the use of this“accelerometer”library is available in the following link:
https://microbit-micropython.readthedocs.io/en/v1.0.1/accelerometer.html
(4)Learn how to use the accelerometer to program increasingly sophisticated step counters.