Microbit Python
Microbit Python
(Python)
Contents
(Python)....................................................................................................... 1
1.Introduction: .......................................................................................... 9
2.Description: .......................................................................................... 10
3.Preparations: .......................................................................................... 11
( 2 )Layout .................................................................................... 13
( 3 ) Pinout ................................................................................... 14
4.Python .................................................................................................... 17
4.1.Python ........................................................................................... 18
4.2.Mu ................................................................................................. 19
5. Projects: .............................................................................................. 29
Project 1: Heartbeat............................................................................ 29
(2)Preparations: ............................................................................ 29
1
(3)Test Code: .............................................................................. 30
(2)Preparations: ............................................................................ 41
(6)Reference ................................................................................. 47
(2)Preparations: ............................................................................ 48
(2)Preparations: ............................................................................ 54
2
(5)Test Code2: ............................................................................ 57
(2)Preparations: ............................................................................ 64
(2)Preparations: ............................................................................ 75
Project 7: Accelerometer..................................................................... 87
(2)Preparations: ............................................................................ 88
3
(4)Test Results1: ......................................................................... 91
4
(4)Test Results: ........................................................................... 120
5
(4)Test Results: ........................................................................... 142
6
(4)Test Results: ........................................................................... 176
7
(3)Schematic Diagram: ............................................................ 201
8
(1) Project Description: .............................................................. 232
8.Resources:............................................................................................. 256
1.Introduction:
become more common. For the same reason, they have increasingly
9
2.Description:
hardware of Micro:bit and designed for those who dream of living a more
This smart home system, with Micro:bit as its control board, is equipped
with a 1602 LCD, a DHT11 temperature and humidity sensor, an analog gas
With the help of these sensors, this kit can be applied to detect
home and open and close doors. Furthermore, all the information detected
can display on 1602 LCD in real time available for you to check and monitor
via smart phones or iPad. By the way, it supports powering by solar energy
MicroPython language for Micro:bit main board to control the smart home
system. In this process, not only can you enhance your ability to make
10
Python is one of the most popular programming language especially in
machine learning for its availability and accessibility have brought huge
This tutorial is a Python tutorial for micro:bit smart home. If you haven't
3.Preparations:
( 1 )What is Micro:bit?
with ARM, Barclays, element14, Microsoft and other institutions. The core
Though it is just the size of a credit card, the Micro:bit main board is
11
programmable buttons, an accelerometer, a compass, a thermometer, a
energy, and a buzzer and others. Thus, it also boasts multiple functions.
The buzzer built in the other side of the board makes playing all kinds of
sound possible without any external equipment. The golden fingers and
gears added provide a better fixing of crocodile clips. Moreover, this board
be entered if users long press the Reset & Power button on the back of it. It
is capable of reading the data of sensors, controlling servos and RGB lights
compatible with almost all PCs and mobile devices. It has no need to install
make some cool inventions like robots and musical instruments, basically
everything imaginable.
12
( 2 )Layout
For the Micro: Bit main board, pressing the Reset & Power button , it will
https://tech.microbit.org/hardware/
https://microbit.org/new-microbit/
https://www.microbit.org/get-started/user-guide/overview/
https://microbit.org/get-started/user-guide/features-in-depth/
13
( 3 ) Pinout
P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,
GPIO
P13,P14,P15,P16,P19,P20
ADC/DAC P0,P1,P2,P3,P4,P10
IIC P19(SCL),P20(SDA)
SPI P13(SCK),P14(MISO),P15(MOSI)
PWM(used
P0,P1,P2,P3,P4,P10
frequently)
14
PWM(not
P5、P6、P7、P8、P9、P11、P12、P13、P14、P15、P16、P19、
frequently
P20
used)
https://tech.microbit.org/hardware/edgeconnector/
https://microbit.org/guide/hardware/pins/
b. Its IO port is very weak in driving since it can merely handle current less
Furthermore, you must figure out the current requirements of the devices
before you use them and it is generally recommended to use the board
15
c. It is recommended to power the main board via the USB interface or via
the battery of 3V. The IO port of this board is 3V, so it does not support
board is required.
blocking them from the matrix or the LEDs may display randomly and the
e. Pin 19 and 20 can not be used as IO ports though the Makecode shows
f. The battery port of 3V cannot be connected with battery more than 3.3V
for use.
16
3.2.Install Micro:bit driver
to recognize the main board, you can install the diver too.
file folder .
4.Python
The following instructions are applied for Windows system but can also
This tutorial is written for Python language. If you want to use graphical
the root directory of the resource you downloaded, there is a folder named
"Python tutorial", which stores all the Python code of Microbit smart home.
17
4.1.Python
the micro:bit unable to fully support Python. Thus here comes the
https://microbit-micropython.readthedocs.io/en/latest/tutorials/introducti
on.html
18
Python has two types of editors(web version and offline version).
(Download Mu:https://codewith.mu/en/download)
4.2.Mu
https://codewith.mu/.
Download Mu
your computer.
19
Below is shown system type of your computer.
20
Enter link: https://codewith.mu/en/download to download the
Find out the folder where Mu is downloaded and double-click file to install
21
Mu.
https://codewith.mu/en/howto/1.1/install_macos .
Windows 10
You will view the page pop up, then click More info;
22
Then click“Run anyway”;
23
Check the license agreement and tick to agree and tap”Install”;
24
Just wait for a few seconds until the installation is finished;
25
Finally,click“Finish”.
26
Start Mu:
Click Mu icon to get started. It may take a while for the first time;
27
Mu’s main interface is shown below:
28
5. Projects:
Project 1: Heartbeat
(1)Project Introduction
This project is easy to conduct with a micro:bit main board, a Micro USB
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
29
(3)Test Code:
Type
Code/Project Code/Project
1:Heartbeat
30
There is another way to import code. Open Mu software and drag
file”Project1:Heartbeat.py”into it.
31
You can also input code in the edit window yourself.
32
The following is a list of built-in images.
• Image.HEART
• Image.HEART_SMALL
• Image.HAPPY
• Image.SMILE
• Image.SAD
• Image.CONFUSED
• Image.ANGRY
• Image.ASLEEP
• Image.SURPRISED
• Image.SILLY
33
• Image.FABULOUS
• Image.MEH
• Image.YES
• Image.NO
Image.ARROW_W, Image.ARROW_NW
• Image.TRIANGLE
• Image.TRIANGLE_LEFT
• Image.CHESSBOARD
• Image.DIAMOND
• Image.DIAMOND_SMALL
• Image.SQUARE
• Image.SQUARE_SMALL
• Image.RABBIT
• Image.COW
• Image.MUSIC_CROTCHET
• Image.MUSIC_QUAVER
• Image.MUSIC_QUAVERS
34
• Image.PITCHFORK
• Image.PACMAN
• Image.TARGET
• Image.TSHIRT
• Image.ROLLERSKATE
• Image.DUCK
• Image.HOUSE
• Image.TORTOISE
• Image.BUTTERFLY
• Image.STICKFIGURE
• Image.GHOST
• Image.SWORD
• Image.GIRAFFE
• Image.SKULL
• Image.UMBRELLA
• Image.SNAKE,Image.ALL_CLOCKS,Image.ALL_ARROWS
35
The code, even it is wrong, can be downloaded to micro:bit board
36
Click“REPL”and press the reset button on micro:bit, the error information
37
Click“REPL”again to turn off REPL mode, then you could refresh new code.
To make sure code correct, you only need to tap“Check”. The errors will be
38
Modify the code according to the prompt and click“Check”.
39
More tutorials, log in website please:https://codewith.mu/en/tutorials/.
(4)Test Results:
and keeping the connection with the computer to power the main board,
(5)Code Explanation:
40
Project 2: Light A Single LED
to locate these LEDs quickly, as the figure shown below, we can regarded
this matrix as a coordinate system and create two aces by marking those in
rows from 0 to 4 from top to bottom, and the ones in columns from 0 to 4
from the left to the right. Therefore, the LED sat in the second of the first
line is (1,0)and the LED positioned in the fifth of the fourth column is (3,4)
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
41
B.Open the offline version of Mu.
(3)Test Code:
import code:
Code/Project Code/Project
42
Click“Check”to examine error in the code. The program proves wrong if
43
If the code is correct, connect micro:bit to computer and click“Flash”to
44
(4)Test Results:
After uploading test code to micro:bit main board and powering the main
board via the USB cable, the LED in (1,0) lights up for 0.5s and the one in
45
(5)Code Explanation:
of micro:bit
in 0~9
0 is brightness, 9 is
best brightness
makes micro:bit
display.show(val1)
46
sleep(500) LED at (1,0) blinks
sleep(500)
display.show(val2)
sleep(500)
(6)Reference
https://microbit-micropython.readthedocs.io/en/latest/utime.html
Dot matrices are very commonplace in daily life. They have found wide
47
announcement and so on.
The LED dot matrix of Micro: Bit main board contains 25 LEDs in a grid.
integrating its position value into the test code. Supported by the same
theory, we can turn on many LEDs at the same time to showcase patterns,
What’s more, we can also click”show icon“ to choose the pattern we like
to display. Last but not the least, we can design patterns by ourselves as
well.
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code:
Type
48
Code/Project Code/Project
49
Click“Check”to examine error in the code. The program proves wrong if
50
(4)Test Results:
After uploading test code to micro:bit main board and powering the main
board via the USB cable, we find that the 5*5 dot matrix start to show
northwest .
51
(5)Code Explanation:
of micro:bit
val =
val
show“hello!”
pattern
display.show(Image.ARROW_SE) “Northeast”arrow
display.show(Image.ARROW_NW) “Southeast”arrow
micro:bit shows
“Southwest”arrow
micro:bit displays
“Northwest”arrow
52
display.clear() The LED dot matrix of
micro:bit clears
(6) Reference:
display.scroll() :
The display scrolls to show the values, if it is integer or float, we use str()
https://microbit-micropython.readthedocs.io/en/latest/utime.html
button, the circuit is connected when pressing the button and it is open the
river in between.
The internal metal piece connect the two sides to let the current pass, just
53
Micro: Bit main board boasts three push buttons, two are programmable
buttons(marked with A and B), and the one on the other side is a reset
and the LED dot matrix shows A,B and AB respectively. Let’s get started.
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code1:
file Tutorial/Python
Code/Project Code/Project
4:Programmable Buttons
54
You can also input code in the editing window yourself.
55
If the code is correct, connect micro:bit to computer and click“Flash”to
56
(4)Test Results1:
After uploading test code to micro:bit main board and powering the main
board via the USB cable, the 5*5 LED dot matrix shows “A”if button A is
pressed and then released, “B” if button B pressed and released, and
(5)Test Code2:
57
file Tutorial/Python
Code/Project Code/Project
4:Programmable Buttons
58
Click“Check”to examine error in the code. The program proves wrong if
59
Please notice that the following sentences are just for warning so the
60
(6)Test Results2:
After uploading test code to micro:bit main board and powering the main
61
board via the USB cable, when the button A is pressed, the LEDs turning
red increase while when the button B pressed, the LEDs turning red reduce.
(7)Code Explanation:
bit
code of it.
62
break Variable a=5
if a == 0: Variable a=0
if a == 1: When a=0
if a == 2: When a=1
if a == 3: When a=2
if a == 4: If a=3
if a == 5: If a=4
If a=5
63
Project 5: Temperature Detection
(1) Project Introduction
The Micro:bit main board is not equipped with a temperature sensor, but
uses the temperature sensor built into NFR52833 chip for temperature
temperature of the chip, and there maybe deviation from the ambient
In this project, we use the sensor to test the temperature in the current
environment, and display the test results in the display data (device). And
then control the LED dot matrix to display different patterns by setting the
(2)Preparations:
64
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code1:
Tutorial/Python
Code/Project
Code/Project 5 :
Temperature Detection
You can also input code in the editing window yourself.(note:all English
65
Click“Check”to examine error in the code. The program proves wrong if
66
If the code is correct, connect micro:bit to computer and click“Flash”to
67
(4)Test Results1:
After downloading test code 1 to micro:bit board, keep USB connected and
click“REPL”and press the reset button on micro:bit. Then REPL window will
temperature unit)
68
(5)Test Code2:
Type
file Tutorial/Python
Code/Project Code/Project
5:Temperature Detection
69
You can also input code in the editing window yourself.(note:all English
The temperature value can be set in compliance with the real temperature.
70
If the code is correct, connect micro:bit to computer and click“Flash”to
71
(6)Test Results2:
After uploading the code 2 to the board, when the ambient temperature is
less than 35 ℃ , the 5*5 LED dot matrix shows . When the
appears.
72
(7)Code Explanation:
bit
code of it.
Temperature
temperature value
73
Project 6: Geomagnetic Sensor
which can not only detect the strength of the geomagnetic field, but also
supports four modes namely 100 kHz,400 kHz,1 MHz and 3.4 MHz and the
first, and then the original data of the magnetometer will be checked.The
direction.
the system will ask us to calibrate it the first time it is put into operation by
74
rotating the board.Please note that metal materials around may attenuate
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
file Tutorial/Python
Code/Project
Code/Project 6
Geomagnetic Sensor
75
Click“Check”to examine error in the code. The program proves wrong if
76
If the code is correct, connect micro:bit to computer and click“Flash”to
77
Note: We need to calibrate micro:bit due to different magnetic field in
different areas. Micro:bit will prompt you to calibrate when you use it first
time.
(4)Test Result1:
After uploading test code1 to micro:bit main board and powering the
board via the USB cable, and pressing the button A, the board asks us to
calibrate compass and the LED dot matrix shows “TILT TO FILL SCREEN”.
78
Then enter the calibration page. Rotate the board until all 25 red LEDs are
on as shown below.
will make the magnetometer reading display directly on the screen. And
the direction north, east, south and west correspond to 0°, 90°, 180° and
270° respectively.
(5)Test code2:
79
For the below picture, the arrow pointing to the upper right when the value
ranges from 292.5 to 337.5. Because 0.5 can’t be input in the code, the
80
File Type Route File Name
file Tutorial/Python
Code/Project
Code/Project 6
Geomagnetic Sensor
You can also input code in the editing window yourself.(note:all English
81
Click“Check”to examine error in the code. The program proves wrong if
82
If the code is correct, connect micro:bit to computer and click“Flash”to
83
(6)Test Results2:
Upload code 2 and plug micro:bit into power. After calibration, tilt
micro:bit board, and the LED dot matrix displays the direction signs.
(6)Code Explanation:
library file of
micro:bit
compass.calibrate() Compass
calibration
84
while True: This is a
permanent
loop that
makes
micro:bit
execute the
code of it.
display.scroll(compass.heading()) button A is
pressed
Micro:bit
scrolls to show
the value of
compass
x=0
compass to
variable x
if...elif...else Condition
judgement
statement:if...el
85
se if...else
display.show(Image("00999:""00099:""00909:""09000:""9 Micro:bit
display.show(Image("99900:""99000:""90900:""00090:""0 Northeast
display.show(Image("00900:""09000:""99999:""09000:""0 Micro:bit
display.show(Image("00009:""00090:""90900:""99000:""9 Northwest
display.show(Image("00900:""00900:""90909:""09990:""0 Micro:bit
0999")) Micro:bit
0900")) Southwest
0900")) Micro:bit
shows the
South arrow
sign
Micro:bit
shows the
86
South arrow
sign
Micro:bit
arrow sign
Micro:bit
shows the
North arrow
sign
Project 7: Accelerometer
8/10/12 bits. The code section sets the range to 1g, 2g, 4g, and 8g.
87
We often use accelerometer to detect the status of machines.
In this project, we will introduce how to measure the position of the board
with the accelerometer. And then have a look at the original three-axis data
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code1:
import code:
Code/Project
Code/Project 7 :
Accelerometer
You can also input code in the editing window yourself.(note:all English
88
Click“Check”to examine error in the code. The program proves wrong if
89
If the code is correct, connect micro:bit to computer and click“Flash”to
90
(4)Test Results1:
After uploading the test code 1 to micro:bit main board and powering the
board via the USB cable, if we shake the Micro: Bit main board,no matter at
any direction, the LED dot matrix displays the digit “1”.
When it is kept upright(make its logo above the LED dot matrix), the
number 2 shows.
91
When it is kept upside down( make its logo below the LED dot matrix) , it
shows as below.
When it is placed still on the desk, showing its front side, the number 4
appears.
When it is placed still on the desk, showing its back side, the number 5
exhibits.
When the board is tilted to the left , the LED dot matrix shows the number
6 as shown below.
When the board is tilted to the right , the LED dot matrix displays the
92
number 7 as shown below:
When the board is knocked to the floor, this process can be considered as
a free fall and the LED dot matrix shows the number 8. (Please note that
this test is not recommended for it may damage the main board.)
Attention: if you’d like to try this function, you can also set the acceleration
(5)Test code2:
import code:
Type
Code/Project Code/Project
7:Accelerometer
93
You can also input code in the editing window yourself.(note:all English
94
If the code is correct, connect micro:bit to computer and click“Flash”to
95
After referring to the MMA8653FC data manual and the hardware
96
(6)Test Results2:
Upload the test code 1 to micro:bit main board and power the board via
97
(7)Code Explanation:
accelerometer.current_gesture() to gesture
98
display.show("8") When it is free fall(accidentally
dot matrix.
shown
99
Project 8: Light Detection
In this project, we focus on the light detection function of the Micro: Bit
main board. It is achieved by the LED dot matrix since the main board is not
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code:
import code:
Tutorial/Python
Code/Project Code/Project
100
8:Light Detection
101
If the code is correct, connect micro:bit to computer and click“Flash”to
102
(4)Test Results:
Upload the test code to micro:bit main board, power the board via the USB
Download code onto micro:bit board, don’t plug off USB cable. Click
“REPL”and press the reset buttons, the light intensity value will be
When the LED dot matrix is covered by hand, the light intensity showed is
The 20 in the code is an arbitrary value of light intensity. If the current light
103
level is less than or equal to 20, the icon moon will appear on the LED dot
(5)Code Explanation:
accelerometer.current_gesture() to gesture
104
micro:bit execute the code of it.
display.read_light_level() Lightintensity
Project 9: Speaker
Micro: Bit main board has an built-in speaker, which makes adding sound
to the programs easier. It can also be programmed to air all kinds of tones,
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code1:
105
Enter Mu software and open the file“Project 9:Speaker-1.py”to import
code:
Tutorial/Python Speaker-1.py
Code/Project
Code/Project 9:Speaker
106
Click“Check”to examine error in the code. The underlines and cursors
107
(4)Test Results1:
After uploading the test code1 to micro:bit main board and powering the
board via the USB cable, the speaker utters sound and the LED dot matrix
(5)Test Code2:
code:
108
File Type Route File Name
Tutorial/Python Speaker-2.py
Code/Project Code/Project
9:Speaker
109
signal that the program is wrong.
110
The musical score of Ode to Joy is attached below:
111
Find more information about musical notations via this link:
https://en.wikipedia.org/wiki/Numbered_musical_notation
After uploading the test code2 to micro:bit main board and powering the
board via the USB cable, the speaker on built-in the Micro:bit board plays
the sound Ode to Joy and the LED dot matrix shows the logo of music.
112
(7)Code Explanation:
of it.
sounds
113
"D5:4", "C5:2", "C5:4",
114
"D5:4", "C5:2", "C5:4",
"C5:1", "C5:4" ]
115
The Micro: Bit main board V2 is equipped with a golden touch-sensitive
logo, which can act as an input component and function like an extra
button.
electric field when pressed (or touched), just like your phone or tablet
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code:
to import code:
116
File Type Route File Name
Tutorial/Python Logo.py
Code/Project Code/Project
10:Touch-sensitive Logo
117
B. When you press button A, a variable named start is set to the current
running time.
C. When you press button B, the start time will be subtracted from the new
running time to calculate how much time has passed since you started
the stopwatch. This difference is added to the total time, which is stored
D. If you press the golden logo, the program will display the total elapsed
Boolean variable can only have two values: true or false. If "running" is
G. (7) If the stopwatch has stopped and the "running" is false, when you
ensure that the time variable will only change when button B is pressed,
118
Click“Check”to examine error in the code. The underlines and cursors
119
(4)Test Results:
Upload the test code to micro:bit main board and power the board via the
USB cable, and press button A to start the stopwatch. When timing, the
beating heart pattern will be displayed on the LED dot matrix screen. Press
button B to stop it and you can start and stop it at any time. It will keep
recording time, just like a real stopwatch. Press the golden logo on the
front of the micro:bit to display the measured time in seconds. And time
can be reset to zero by pressing the reset button on the back of it.
120
Project 11: Microphone
The Micro: Bit main board is built with a microphone which can test the
indicator turns on. Since it can measure the intensity of sound, you can
make a noise scale or disco lighting changing with music. The microphone
proximity with holes that lets sound pass.When the board detects sound,
(2)Preparations:
A. Attach the Micro:bit main board to your computer via the USB cable;
(3)Test Code:
import code:
121
(How to load the project code?)
Tutorial/Python Microphone-1.py
Code/Project Code/Project
11:Microphone
You can also input code in the editing window yourself. (note:all English
122
signal that the program is wrong.
123
(4)Test Results1:
After uploading test code to micro:bit main board and powering the board
via the USB cable, the LED dot matrix displays pattern “ ”when you
(5)Test Code2:
import code:
124
File Route File Name
Type
Code/Project Code/Project
11:Microphone
125
signal that the program is wrong.
Upload test code to micro:bit main board, power the board via the USB
cable
126
(6)Test Results2:
Upload test code to micro:bit main board and power the board via the USB
cable. When the button A is pressed, the LED dot matrix displays the value
of the biggest volume( please note that the biggest volume can be reset
via the Reset button on the other side of the board ) while when clapping,
127
(7)Code Explanation:
micro:bit
SoundEvent.QUIET:
display.show(Image.HEART_SMALL)
intensity value
maxSound is 0
")
128
microphone.sound_level
() to the variable
soundLevel
soundLevel
value
level value
129
Project 12: Touch-sensitive Logo Controlled Speaker
and the speaker respectively. In the project, we will combine these two
components to play music. That’s the logo will be applied to control the
(2)Components Needed:
(3)Connection Diagram:
Attach the Micro:bit main board to your computer via the USB cable.
(4)Test Code:
130
Enter Mu software and open the file“Project 12:Touch-sensitive Logo
Type
Code/Project Code/Project
12 : Touch-sensitive Logo
Controlled Speaker
131
Click“Check”to examine error in the code. The underlines and cursors
132
If the code is correct, connect micro:bit to computer and click“Flash”to
133
(5)Test Results:
After uploading test code to micro:bit main board and powering the board
via the USB cable, the speaker plays the song Happy Birthday to You when
(6)Code Explanation:
code of it.
134
display.show(Image.MUSIC_QUAVER) Music logo shows on the LED
command
https://microbit-micropython.readthedocs.io/en/latest/ble.html
135
7.Expansion Projects:
The former 14 projects are the introduction of sensors and modules. The
Note: (G), marked on each sensor and module, is the negative pole and
(V) is the positive pole and linked with V , VCC, + or 5V on the sensor shield
or control board. And you need to connect a power in case that power
supply is weak.
(1)Project Introduction
We’ve set up the micro:bit smart home. Now let’s get started from the
136
germanium, etc.). It features unidirectional conductivity, that is, the
positive voltage is applied to the anode (long leg) and the cathode (short
leg) of the diode. when the voltage of its anode is higher than the voltage
of its cathode, thus, the diode is turned on(LED is on). When a reverse
Light-emitting diodes have an anode (+) and a cathode (-), and they can
only allow current to flow from one anode to the cathode. The
This LED module can be controlled by a basic code to turn on and off the
light alternatively, simulating the breathing effect. And the time gap can be
changed in the code. When the signal end S is at high level,the LED lights
137
(2)About the Yellow LED:
Working DC 3.3-5V
Voltage:
current:
Max 0.1W
Power:
Ports: (digital
input)
Working -10 ° C ~
Temperat +50°C
ure:
Display Yellow
Color:
(3)Test Code
Micro:bit
Yellow LED
Expansion
Module
Board
138
GND G
5V V
S(16) S
code:
Type
Code/Expansion Project
139
Click“Check”to examine error in the code. The underlines and cursors
140
If the code is correct, connect micro:bit to computer and click“Flash”to
141
(4)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
The micro:bit will show smile expression,and a yellow LED will flash with an
interval of 1000ms.
142
(5)Code Explanation:
bit
pattern
code of it.
143
Project 2:Breathing LED
breathing effect. Similarly, you can change the step length and delay time
PWM is a means of controlling the analog output via digital means. Digital
control is used to generate square waves with different duty cycles (a signal
that constantly switches between high and low levels) to control the analog
output.In general, the input voltages of ports are 0V and 3V. What if the
1.5V is required? Or a switch among 1V, 1.5V and 3V? We cannot change
144
For Micro:bit digital port voltage outputs, there are only LOW and HIGH
You can define LOW as“0”and HIGH as“1”, and let Micro:bit output five
This process can be likened to showing a movie. The movie we watch are
In the graphic below, the green lines represent a regular time period. This
with Micro:bit's PWM frequency at about 500Hz, the green lines would
such that analogWrite(255) requests a 100% duty cycle (always on), and
145
PWM is applied to light brightness adjustment, speed adjustment of motor
Parameters of PWM:
146
A.pulse width (minimum / max)
C.Voltage level(0V-3V)
D.There are commonly used PWM ports, namely P0, P1, P2, P3, P4 and P10.
And there are other rarely used ports, namely P5, P6, P7, P8, P9, P11, P12,
In the experiment, we connect the port S of yellow LED Module to the port
S (16) of the expansion board. And P16 can also be used as a PWM
interface.
Working DC 3.3-5V
Voltage:
Current:
Max 0.1W
Power:
Port: ( digital
147
input)
Working -10 ° C ~
Temperat +50°C
ure:
Display Yellow
Color:
(3)Test Code
Micro:bit Yellow
Expansion LED
Board Module
GND G
5V V
S(16) S
Enter Mu software and open the file“Project 2:Breathing LED .py”to import
code:
Type
148
file Tutorial/Python LED .py
Code/Expansion Project
Code/Project 2:Breathing
LED
149
If the code is correct, connect micro:bit to computer and click“Flash”to
150
(4)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
The micro:bit will show a smile expression, and LED smoothly changes its
brightness from light to dark and back to light, continuing to do so, which
151
(5)Code Explanation:
for index in range (0, 255): range() is a function; for index in range(0,
sleep(10) Delay in 10 ms
(1)Project Introduction
6812 2X2 full-color RGB module integrates the controlling circuit and the
illuminating circuit. Each LED is the same as a 5050 LED lamp bead, and
152
each component is a pixel point. The inner pixel point includes a amplify
driving circuit that latch signal from digital ports shapes, a high-precision
control portion, which effectively ensures that the color of the pixel point.
After the pixel point is reset, the S-terminal receives the data transmitted
from the controller. First, the 24bit data sent by the first pixel is extracted
by the first pixel point, and sent to the internal portion of the pixel point.
Voltage: Current:
153
Gray Scale: 256 Illuminating 180° Illuminating Red, yellow,
white
(3)Test Code1
Board Module
GND G
5V V
S(14) S
Enter Mu software and open the file “Project 3 :6812 2x2 full color
154
File Route File Name
Type
Code/Expansion Project
Color RGB
You can also input code in the editing window yourself.(note:all English
155
Click“Check”to examine error in the code. The underlines and cursors
156
If the code is correct, connect micro:bit to computer and click“Flash”to
157
(4)Test Results1:
Upload the test code1 to the micro:bit,plug in power, dial the DIP switch to
ON and press “1”on the rocket switch. You will view the 6812 RGB module
loop way.
158
(5)Test Code2:
Enter Mu software and open the file “Project 3 :6812 2x2 full color
Type
Code/Expansion Project
Color RGB
You can also input code in the editing window yourself.(note:all English
159
160
Click“Check”to examine error in the code. The underlines and cursors
161
162
If the code is correct, connect micro:bit to computer and click“Flash”to
163
(6)Test Results2:
Upload the test code 2 to the micro:bit,plug in power, dial the DIP switch
You can view four WS2812RGB lights light up,like a flowing light.
(7)Test Code3:
Enter Mu software and open the file “Project 3 :6812 2x2 full color
164
RGB-3.py”to import code:
Type
Code/Expansion Project
165
Click“Check”to examine error in the code. The underlines and cursors
166
If the code is correct, connect micro:bit to computer and click“Flash”to
167
(8)Test Results3:
Upload the test code 3 to the micro:bit,plug in power, dial the DIP switch
Then you will see 4 WS2812RGB lights light up with random colors, like a
flowing light.
(9)Code Explanation:
168
np = neopixel.NeoPixel(pin14, 4) Set Neopixel as pin P14 to initialize
pixel_id1
np[pixel_id1] = (255, 0, 0)
169
G=0 Set the initial value of G to 0
(1)Project Introduction
The Pyroelectric infrared motion sensor can detect infrared signals from
complex circuit and lower reliability. Yet, this new pyroelectric infrared
170
lower power consumption, light weight, small size, lower voltage working
Working DC
Voltage: 4.5-6.5V
Max 50MA
Working
Current:
Static <50uA
Current:
Control Digital
Port: output
(high level
is 3.3V,low
level is
0V)
Control Digital
171
Working -10 ~ 50 ℃
Tempera
ture:
Max 4m
detectio
distance
Sensing <100°
Angle:
Trigger L doesn’t
Way: repeatedly
trigger/H
trigger
repeatedly
Note:
2. In the test, open the white lens to check rectangular sensing part. When
the long line of the sensing part is parallel to the ground, the distance is
the best.
3. In the test, covering the sensor with white lens can sense the distance
172
precisely.
4. The distance is best at 25℃, and the detection distance value will reduce
5. After powering up and uploading the code, you can start testing after
(3)Test Code:
Micro:bit
PIR Motion Sensor
Expansion Board
GND G
5V V
S(15) S
import code:
Type
173
file Tutorial/Python PIR motion sensor.py
Code/Expansion Project
Sensor
174
If the code is correct, connect micro:bit to computer and click“Flash”to
175
(4)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
Click“REPL”and then press the reset button on the back of the board.
If PIR motion sensor detects someone nearby, the serial monitor will
display “1” , and the indicator on the module will be off. If nobody is
around, the serial monitor will show “0”, the indicator will be on.
As shown below:
176
(5)Code Explanation:
val to 0
177
of it.
sensor
(1)Project Introduction
principle of the PIR motion sensor and its control method. In this project,
(2)Test Code:
GND G GND G
178
5V V 5V V
S(15) S S(16) S
import code:
Type
Code/Expansion Project
Code/Project 5:Induction
Lamp
179
Click“Check”to examine error in the code. The underlines and cursors
180
If the code is correct, connect micro:bit to computer and click“Flash”to
181
(3)Test Results:
Upload the test code to the micro:bit, plug in power, dial the DIP switch to
ON and press“1”on the rocket switch. The micro:bit will show a smile
image.
When the PIR motion sensor detects people, the yellow LED will be on;
(4)Code Explanation:
182
the LED
of it.。
the LED
Project 6: Servo
183
(1) Project Introduction
The servo, window and door of this smart home have been fixed together
so the servo can be used to drive the window and door to open or close,
circuit board, a core-less motor, a gear and a position sensor. Unlike motor
which is often applied to control rotating speed and direction servo is used
It has 3 wires which are marked in brown, red, and orange respectively.
The rotation angle of servo motor is controlled by regulating the duty cycle
184
signal is 20ms (50Hz). Theoretically, the width is distributed
corresponds the rotation angle from 0° to 180°. But note that for different
brand motors, the same signal may have different rotation angles.
After measurement, it is found that the pulse range of the steering gear is
time (20ms)
level
185
level
level
range: μsec
current:
Current:
Working -30℃~60℃
temperature:
Note: Supplying power via USB cable or computer may burn the servo;
186
thus, we recommend using batteries.
(4)Test Code:
Micro:bit
Expansion Servo
Board
5V Red Wire
Orange
S(8)
Wire
Enter Mu software and open the file“Project 6:Servo .py”to import code:
Type
file Tutorial/Python
Code/Expansion Project
Code/Project 6:Servo
You can also input code in the editing window yourself.(note:all English
187
words and symbols must be written in English)
188
signal that the program is wrong.
189
If the code is correct, connect micro:bit to computer and click“Flash”to
190
(5)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
ON and press“1”on the rocket switch. The micro:bit will show smile
(1)Project Introduction
H-bridge driving part uses the PMOS and NMOS power tubes of low
on-resistance. In addition, the HR1124S chip has the low standby and static
current.
This motor is compatible with all kinds of MCU control boards. It comes
take advantage of the voltage direction of IN+和 IN- to control the rotation
191
of motor and alter its speed via PWM signals.
(2)Parameters:
Voltage:
port(digital
input)
Temperature: Attribute:
GND G
5V V
192
S(13) IN+
S(12) IN-
code:
Type
Code/Expansion Project
You can also input code in the editing window yourself.(note:all English
193
Click“Check”to examine error in the code. The underlines and cursors
194
If the code is correct, connect micro:bit to computer and click“Flash”to
195
(4)Test Code2:(PWM control)
code:
Type
file Tutorial/Python
Code/Expansion Project
Code/Project 7 : 130
Motor
196
You can also input code in the editing window yourself.(note:all English
197
If the code is correct, connect micro:bit to computer and click“Flash”to
198
(5)Test Results:
Upload the test code to the micro:bit, plug in power, dial the DIP switch to
ON and press“1”on the rocket switch. The fan will rotate clockwise for 5s,
stop 1, rotate anticlockwise for 5s and stop for 1s, in loop way. (How to
(1)Project Introduction
199
This module integrates a charging and discharging chip, which can be
It has a Micro USB port and a charging port for solar panels, which can
In addition, this module has a boost module which can increase the
voltage of batteries to 6.6V. The DIP switch on the module is the OUTPUT
switch of 6.6V. The pin G and V can output 6.6V and the pin S can read the
(2) Parameters:
panels
solar panel
200
Environmental Attribute ROHS
(3)Schematic Diagram:
(4)Features:
201
OLAR4.8-6.0V, the input port of power, is connected to polar panels.
The solar energy is converted into electric energy via solar panels.
BAT, the output port of power, is interfaced with the lithium battery
202
This is the switch. Slid to ON end, then the external lithium battery will be
We can connect the solar battery panel and an LED we provide together, as
shown below.
Disconnect the power, after a while, you will see the LED light up.
203
Project 9:1602 LCD
(1)Project Introduction
With I2C communication module, this is a display module that can show 2
It shows blue background and white word and connects to I2C interface of
On the back of LCD display, there is a blue potentiometer for adjusting the
The original 1602 LCD can start and run with 7 IO ports, but ours is built
Notice that when the screen gets brighter or darker, the characters will
204
(3) About 1602 I2C:
Voltage : Port:
communication
communication
(3)Test Code:
205
Micro:bit
I2C 1602 LCD Module
Expansion Board
GND GND
5V 5V
SDA SDA
SCL SCL
Enter Mu software and open the file“Project 9:1602 LCD.py”to import code:
Type
Code/Expansion Project
You can also input code in the editing window yourself.(note:all English
206
207
Click“Check”to examine error in the code. The underlines and cursors
208
209
If the code is correct, connect micro:bit to computer and click“Flash”to
210
211
212
(4)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
The micro:bit board will show a smile image. Then rotate the knob of the
at one row and numbers at the second row. In addition, the number
Note: When the display doesn’t show characters, you can adjust the
213
Project 10:Steam Sensor
(1)Project Introduction
This is a commonly used steam sensor. Its principle is to detect the amount
of water by bare printed parallel lines on the circuit board. The more the
water content is, the more wires will be connected. As the conductive
contact coverage increases, the output voltage will gradually rise. It can
detect water vapor in the air as well. The steam sensor can be used as a rain
water detector and level switch. When the humidity on the sensor surface
214
(2)About the Stream Sensor:
Working DC 3.3-5V
Voltage:
Working -10℃~+70℃
Temperatu
re Range:
in short circuit.
Port:
(3)Test Code:
Micro:bit Expansion
Steam Sensor
Board
GND G
3V3 V
S(0) S
215
Enter Mu software and open the file“Project 10:Steam Sensor.py”to import
code:
Type
Code/Expansion Project
Code/Project 10 : Steam
Sensor
You can also input code in the editing window yourself.(note:all English
216
Click“Check”to examine error in the code. The underlines and cursors
217
If the code is correct, connect micro:bit to computer and click“Flash”to
218
(4)Test Results:
Upload the test code, and plug in power with micro USB cable. Then the
micro:bit will show“❤”. Click“REPL” and then press the reset button on
the back of the board.The serial monitor will show the output data, and the
steam sensor will read the analog signals at the signal end. The more the
As shown below;
219
Project 11:Rains Alarm
(1)Project Introduction
combine Steam Sensor, Micro:bit, and yellow LEDs, making a simple rain
220
alarm.
(2)Test Code:
GND G GND G
3V3 V 5V V
S(0) S S(16) S
Type
Code/Expansion Project
Code/Project 11 : Rains
Alarm
221
(note:all words and symbols must be written in English)
222
If the code is correct, connect micro:bit to computer and click“Flash”to
223
(3)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
expression. When the detected analog signals are more than 500, the
micro:bit will emit “tick, tick”and the yellow LED will flash. Otherwise, no
224
Project 12:Analog Gas(MQ-2)Sensor
(1)Project Introduction
This gas sensor is used for household gas leak alarms, industrial
hydrogen, etc.,
The MQ-2 smoke sensor can be accurately a multi-gas detector, with the
advantages of high sensitivity, fast response, good stability, long life, and
It can detect the concentration of flammable gas and smoke in the range of
It must be heated for a period of time before using the smoke sensor,
otherwise the output resistance and voltage are not accurate. However, the
heating voltage should not be too high, otherwise it will cause internal
225
It belongs to the tin dioxide semiconductor gas-sensitive material. At a
certain temperature, tin dioxide adsorbs oxygen in the air and forms
When in contact with flammable gas in the air and smog, and the potential
barrier at the grain boundary is adjusted by the smog, it will cause the
of smoke or flammable gas in the air, the greater the conductivity, and the
lower the output resistance, the larger the analog signal output. In addition,
Working 3.3-5V
Voltage:
Current:
Temperature:
226
Control Port: Digital and analog
output
Detection 300-10000ppm
Rake Ratio: ≤
0.6(R3000ppm/R10
00ppm C3H8)
Sensitivity: Rs(in
air)/Rs(1000ppmiso
butane)≥5
Features:
(2) Dual-channel signal output (analog output and TTL level output)
(3) TTL output effective signal is Low Level. (When the Low Level is output,
(4) The analog output is 0 ~ 5V voltage. The higher the concentration, the
(5) a good sensitivity to liquefied gas, natural gas and urban gas.
227
(7) Fast response recovery.
(3)Test Code:
Micro:bit
Analog Gas
Expansion
(MQ-2)Sensor
Board
GND G
5V V
S(1) D
Type
Code/Expansion Project
(MQ-2)Sensor
You can also input code in the editing window yourself.(note:all English
228
Click“Check”to examine error in the code. The underlines and cursors
229
If the code is correct, connect micro:bit to computer and click“Flash”to
230
(4)Test Results:
Upload the test code to the micro:bit, plug in power and dial the DIP switch
When the gas sensor detects no flammable gases, the serial monitor prints
1. While when you turn on a fire lighter near it, it detects gases, the serial
monitor prints 0 and the red indicator on the module lights up as shown
below:
(By the way, the sensitivity of this sensor can be adjusted by rotating the
231
blue potentiometer on it.)
This MQ-2 gas sensor is used for household gas leak alarms, industrial
hydrogen, etc., and widely used in various fire alarm systems. It can be
232
accurately a multi-gas detector, and has the advantages of high sensitivity,
fast response, good stability, long life, and simple drive circuit.
It can detect the concentration of flammable gas and smoke in the range of
We will make a gas leakage detector with a MQ-2 gas sensor, a yellow LED
(2)Test Code:
GND G GND G
5V V 5V V
S(1) D S(16) S
to import code:
233
Type
Code/Expansion Project
Code/Project 13 : Gas
Leakage Detector
234
235
Click“Check”to examine error in the code. The underlines and cursors
236
237
238
If the code is correct, connect micro:bit to computer and click“Flash”to
239
240
(3)Test Results:
241
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
The micro:bit will show a smile image. Make a fire lighter close to the gas
sensor, 1602 LCD will display“MQ-2”at the first row and show“gas
(1)Project Description:
The final lesson is the combination of all modules and sensors. It is an
(2)Test Code:
to import code:
Type
file Tutorial/Python
Code/Expansion Project
242
Code/Project 14:Multiple
Functions
You can also input code in the editing window yourself.(note:all English
243
244
245
246
Click“Check”to examine error in the code. The underlines and cursors
247
248
249
250
If the code is correct, connect micro:bit to computer and click“Flash”to
251
252
253
254
(3)Test Results:
Upload the test code to the micro:bit,plug in power, dial the DIP switch to
255
ON and press“1”on the rocket switch.The micro:bit will show a smile
image.
When the analog value detected by the steam sensor is bigger than 400,
the 5 WS2812 RGB lights on the 6812 RGB module are all on, displaying in
red, orange,yellow, green, blue, Indigo, violet, purple and white, in loop
way.
Then the window is closed, the door and the fan rotate;
Make a fire lighter close to the gas sensor, 1602 LCD will display“MQ-2”
at the first row and show“gas leakage”at the second row. At same time, it
will emit“tick,tick” sound and the yellow LED will flash. Otherwise,the
speaker makes no sound, the LED reminds off and the 1602LCD displays no
characters.
8.Resources:
https://fs.keyestudio.com/KS4027-4028
256