Maqueen & Microbit Graphical Programming
Maqueen & Microbit Graphical Programming
html
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html
Introduction
The programming courses in this book are specially designed for all robotic
hobbyists and STEM educators. Most people may think that robotic learning is a
high-cost investment, actually, it is not the case when compared with what
achievements we may make in robotic learning process. A high-quality and
funny teaching equipment can arouse students interests in learning robot. By
designing and controlling robots, they can get known about robotic engineering,
programming and machine learning and so on. Maqueen is a mini robot based
on micro:bit with various output modes, integrating sound, temperature, and
light sensor. Students can program with Maqueen to realize all kinds of playing
methods. Furthermore, the large quantity of interacting cases could let them
enjoy the happiness of programming learning. Besides that, it can also develop
students’ logic thinking and creativity. The book is suitable for the middle and
elementary students, also can be used as an introduced textbook for STEM
beginners’ training education.
There is an anti-fake label of TSINGHUA UNIVERSITY PRESS on every book’s cover, books
without the label are not allowed to be sold.
The book is published by TSINGHUA UNIVERSITY PRESS. All rights reserved.
Copyright infringement complaining Tel: 010-62782989 13701121933
Preface
Why should kids learn to program? What’s the significance and value of learning
programming? We may ask these questions often as parents, but for kids, instead of
considering why and what, they just thought that programming robot is fun and
interesting, and they are so pleased to show their awesome robots work to others.
Learning robotics and coding can be a kind of gift this era brought to them.
Programming is an exciting and satisfying progress, since in programming, we have to
analyse and ponder all kinds of possibilities and then to come up with an acceptable
method to solve the problem. In this process, it can greatly improve kids’ creativity,
logical thinking and problem-solving ability, furthermore, coding is able to instill
persistence, build frustration tolerance and enhance concentration.
Teaching robotics in school is getting more and more prevalent throughout the country.
“like English, programming will become a personal ability rather than programmer’s
professional skill in the future, and it will integrate into every aspects and details of our
social life.” says Chen Yue, a professor at Computer College of Zhejiang University.
Programming robots helps to develop children’ imagination and creativity so that they
can easily adapt the society of AI in the future.
Maqueen is a mini robot car intensively designed for STEM education, small but powerful.
It is developed on the basis of the popular micro:bit board. Children can code and
create by the board, low cost and easy to learn, which is precisely what the original
intention of STEM education is. Accelerometer, temperature sensors and other
commonly used sensors are all integrated on micro:bit board, and it is also equipped
with Bluetooth and wireless communication. There are ultrasonic sensor, motor and line-
tracking sensor on the car body. Everyone can control the robot car easily by
programming.
Content
Chapter 1. Customize Emoji .................................................................................................. 1
1.1 Micro:bit.............................................................................................................................. 1
1.2 Mind+ Interface ................................................................................................................. 3
1.3 Algorithms Analysis ............................................................................................................ 7
1.4 Example Program.............................................................................................................. 7
1.5 LED Matrix ......................................................................................................................... 10
1.6 Training Camp ................................................................................................................. 12
Chapter 2. Funny Electronic Organ .................................................................................... 13
2.1 Sound Module ................................................................................................................. 13
2.2. Function Module ............................................................................................................ 15
2.3 Micro:bit Pins .................................................................................................................... 16
2.4 Training Camp ................................................................................................................. 19
Chapter 3. Catching Apple ................................................................................................. 20
3.1 Algorithms Analysis .......................................................................................................... 20
3.2 Programming ................................................................................................................... 20
3.3 Programming Result ........................................................................................................ 25
3.4 Training Camp ................................................................................................................. 26
Chapter 4. Cat and Mouse.................................................................................................. 27
4.1 Accelerometer ................................................................................................................ 27
4.2 Algorithms Analysis .......................................................................................................... 28
4.3 Programming ................................................................................................................... 28
4.4 Variable and Broadcast................................................................................................. 34
4.5 Programming Result ........................................................................................................ 36
4.6 Training Camp ................................................................................................................. 37
Chapter 5. Stress Buster ....................................................................................................... 38
5.1 Algorithms Analysis .......................................................................................................... 38
5.2 Programming ................................................................................................................... 38
5.3 Program Description ....................................................................................................... 43
5.4 Programming Result ........................................................................................................ 44
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html
In this chapter, we will get to know micro:bit, and try using its LED panel display
function. Besides, we will also get familiar with mind+, and learn how to create a
sprite and build a background and so on.
1.1 Micro:bit
Micro:bit is an open source hardware ARM-based embedded system designed
by BBC for use in computer education, developed under the cooperation of
Microsoft, Samsung, ABM, and so on. From robots to Lego, micro:bit can be used
to build all sorts of fantastic projects-the possibilities are endless!
Figure 1-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 2 / 158
Micro:bit board, with a credit card size of 5cm×4cm, has 25 programming LEDs,
two programmable buttons, light sensor, accelerometer, magnetometer,
temperature sensor and Bluetooth module and so on.
Micro:bit is equipped with built-in common sensors and its LED can display text,
numbers and images. We can carry out many interesting experiments by
graphical programming or coding. In addition, micro:bit can also be used
together with crocodile clips and expansion board to interact with other
electronics, for instance, to read sensor data, control servo and LED strip. It can
totally fit all kinds of programming teaching and developing situations.
Programming micro:bit
Mind+ interface includes: function menu bar, control, block, script, sprite and
stage section.
1) Create a sprite
This is the backstage of the whole “stage”. Without the help of the menu bar, we
can do nothing with this software. What functions are there behind the “stage”?
Sprite is the main character in the play, and will show his performance on the
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 4 / 158
stage. Users can give instructions to a sprite by snapping blocks together in the
script area. A robot Micko will be regarded as the main character by default
when creating a new project. If you want to add new sprites to the project, just
click the sprite library at the lower-right corner (next to the background library)
to select the one you like. Then drag and combine the blocks in the script area
to program and control the sprite. Each sprite has its own script, when you click
the sprite in the sprite area, the related codes will be displayed on the script area.
There are so many ways to create a sprite, let’s start to learn! Frist we will take a
look about the sprite tool in Chart 1-1.
TOOL Description
Chart 1-1
Open the sprite library, in which there are different character classifications. We
can choose them according to our actual project.
Figure 1-3
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 5 / 158
In order to make the sprite fit the whole story plot, you may need to adjust its size
and costumes after importing it to stage, or even sometimes you have to draw
a new sprite.
Figure 1-4
We can design different costumes for every sprite, for instance, when we add
“Butterfly 1” to the stage, we can find there are two different costumes (some
sprites only have one shape). This can basically make our sprite come to alive.
Of course, we can add new sprite or switch the sprite’s costumes.
Figure 1-5
Figure 1-5
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 6 / 158
2) Build a background
Stage is the place where your sprites show their performance. Add background
to the stage to provide a suitable living environment for the characters so as to
make the entire story fun and lively. In the background area, we can choose an
image we like from the background library, also we can upload a picture from
the computer. Let’s get to know the background library.
Figure 1-6
Figure 1-7
The center point of the stage is (0,0). The horizontal direction is X axis and vertical
direction is Y axis. Take the center point as the original point, the right part of X
axis, positive (+); the left part negative (-). The upper section of Y axis, positive (+);
the lower section, negative (-). Get known about the coordinate system then we
can well control the sprites to perform on the stage.
Click the paint to draw new sprites, make a smiling face and a crying face.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 8 / 158
Figure 1-8
2. When the space key is pressed, the program starts and it will switch to next sprite.
Sprite Blocks
Chart 1-2
Add Blocks:
Drag blocks from the module area to script area, snap blocks together to control
the sprite. The different color of the block represents different functions such as
motion, look, sound etc.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 9 / 158
Delete blocks:
Drag block to the left to remove it, or right-click to delete block. Notice: when
we want to delete block that has been nested in a group of blocks, we have to
separate it from the others firstly.
⑥ When I receive (message 1): scripts that begin with this block will be invoked
once the specified broadcast has been sent by a calling script.
⑦ Broadcast (): send a broadcast throughout the whole program. Any scripts
in any sprites that are hatted with the When I Receive () block that is set to a
specified broadcast will activate.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 10 / 158
⑧ Broadcast () and wait:this block lets scripts send broadcasts, and have them
wait until all scripts activated by the broadcast end.
Blocks Description
Switch the costume to the next in the costumes pane, if the
current costume is the last in the list, the block will loop to
the first.
Change the sprite’s costume to a specified one.
Result:
Add a background for the stage. When the space key is pressed, the program
starts and the two different emojis are switched.
Figure 1-10
Hardware Connection
Connect micro:bit to the computer with Micro USB, select the corresponding
COM port in the Connect Device menu bar. Please note: if we selected the
Scratch mode at the right-upper corner, then the program result can be
displayed without downloading codes.
Figure 1-13
Result:
Press the space key, let’s see what the Mind+ and micro:bit LED screen will
display.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 12 / 158
Figure 1-14
The eight numbers 1 to 8 on the keyboard respectively represent Do, Re, Mi, Fa,
Sol, La, Ti, Do(high-pitch), and the corresponding English letters are C, D, E, F, G,
A, B, C2. Shown as below.
Program Analysis:
⚫ Set the sprite “keyboard”, there are two different built-in shapes for keyboard:
keyboard a and keyboard b.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 14 / 158
Keyboard
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 15 / 158
Add background for the stage, run the programs and check the result.
Figure 2-1
Figure 2-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 16 / 158
The first three, labelled 0, 1 and 2 are flexible and can be used for many different
things-which means they are often called “general purpose input and output”
(GPIO for short).
Figure 2-3
Connect micro:bit board and the computer with micro USB. Let pin P0 play the
music “twinkle, twinkle little star”, and P1 and P2 display different instruments.
Press button A and button B to play faster or slower. As shown in Figure 2-4.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 17 / 158
Figure 2-4
Code Blocks Descriptions:
Blocks Description
Result:
Touch the micro:bit pin with your one hand, and touch the GND with another
hand, by which to form a closed loop between the board and your body, then
program starts. Touch pin P1 or P2 to add accompaniment for the performance,
and press button A and B on the micro:bit to control the playback speed.
⚫ Duplicate the sprite “Apple”, and the apples appear and fall from the upper
of the screen constantly. If the apple touched the cat, it disappears and
reappears in the upper-side of the screen;
⚫ Duplicate the sprite “Apple”, revise the waiting time after it falls;
⚫ Add the sprite “Lightning”, the falling effect is same to apple, but when it hits
the cat, game over.
3.2 Programming
1. Create the main characters: cat, apple and lightning.
2. Program the cat: the cat has two shapes. When the cat moves left or right,
change its shape to the related action gesture so as to make it more like a
cat walking.
Figure 3-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 21 / 158
Figure 3-2
Press the button A and B on the micro:bit board to control the cat’s movement.
Change the value of x axis and the movement direction of the cat. The cat will
catch the apple as it walks, if the lightning hits the cat, stop all and game over.
Figure 3-3 is the entire program.
Figure 3-3
Chart 3-1
Falling state control: since in the stage area, the value of Y ranges from 180 to -
180, then the program needs run 360 times. The value of Y minus 1 every time.
We can directly duplicate the sprite apple and its codes. However, when there
are 4 apples to be appearing on the stage, what will happen? Since only the
value of x is random, that is to say, these apples will fall from high at a same time.
To make the game look much more realistic, we need to set different initial falling
time for the apple, how to do that? There are many acceptable ways. Here we
decide to realize the effect by adding more “wait” blocks before “display”.
And we can adopt random functions for the wait time. Just like this:
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 24 / 158
Figure 3-5
Duplicate the sprite and code orderly, as shown figure 3-6.
Figure 3-6
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 25 / 158
What’s the difference of the codes between the lightning and apple? The
program of the lightning is shown below.
Figure 3-7
Figure 3-10
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 27 / 158
Players get 1 point if the mouse gets hit once. The one who get the most scores
within the given time will be the winner.
In this tutorial, we will also learn variable and the micro:bit accelerometer.
4.1 Accelerometer
Accelerometer, also called gravity sensor, is widely used in our daily life, for
instance, gradienter, passometer in the smart phone. The accelerometer on a
micro:bit measures acceleration as its name suggests or detects the movement
along three axes. Get the data of these three axes, then the micro:bit board’s
gesture such as moving, shaking, tilting or free falling can be detected.
Figure 4-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 28 / 158
⚫ The mouse moves randomly in the stage, if it touches the edge of the stage,
it will point to the opposite direction from which it was coming;
⚫ The ball is the cat’s hidden weapon. It is placed under the cat. When button
b is pressed, the ball moves forward until it touches the edge. If the ball hits
the mouse, the player gets one point, at the same time, the mouse changes
its shape and the screaming sound is played.
4.3 Programming
1. Add the sprites mouse, cat and ball, and set the size to 50%, 70% and 40%
respectively. Then add a background for the stage. Shown as figure 4-2.
Figure 4-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 29 / 158
2. Shape design
Click the sprite mouse, we can find there are two shapes, mouse a and mouse b. Here
we set the mouse b as the state when it gets hit. To make it obviously, we can fill the
Figure 4-3
Figure 4-4 is the program that the mouse runs on the floor. To multiply the routine
of the mouse, we add a “pick random () to ()” block to make the mouse move
randomly.
Figure 4-4
Figure 4-5
Except the gestures of tilting to left or right, micro:bit also can recognize several
other gestures.
How to solve the first problem? —create a variable “direction”. When the cat
turns around, we assign a value to the direction, then the ball points to that
variable “direction”.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 32 / 158
Let’s get to know about the “point in direction ()” block. As shown
in figure 4-8, the top is 0 degree, and 180 degrees is a semicircle,
which points to the down. The left semicircle ranges from 0 to -
180 while the right semicircle from 0 to 180. -180 and 180 point in
the same direction.
Figure 4-8 The final script of the cat is shown in figure 4-9, tick the checkbox
next to the variable “direction” block, then the value of it will be displayed on
the left-upper corner of the stage area. Tilt micro:bit board to make the cat turn
around, we can see the value of the direction equals to that shown below the
stage. Now let’s go to the second question. What will happen after the ball hits
the mouse? Firstly, we need to add a score system. Set a variable “Score”, and
its initial value is 0. If the ball hits the mouse, add scores for the player. Meanwhile,
broadcast the message “got hit”. The program is shown as below.
Figure 4-9
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 33 / 158
Figure 4-10
Then we need to program the mouse. The ball broadcast the message “got hit”.
When the mouse received the message, it will make screaming noise. So, we
need to add the sound play block here. Select “scream1” from the sound library.
The final program of the mouse is shown in figure 4-11.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 34 / 158
Figure 4-11
Boolean data type has two possible values: True and False. One of the most
common uses for Boolean variable is inside if statement.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 35 / 158
Create a variable: click “Variable” module, a default variable “my variable” can
be found in its block area. Create a variable and name it. For instance, name it
as “score”. Now we can design our variable: assign, increase by degree, hide
or show the variable. Shown as figure 4-12.
Figure 4-12
2. Broadcast
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 36 / 158
We can use the “broadcast” function of mind+ to order a sprite or several sprites
to execute the preset scripts. The function related with “broadcast” can be
found in “event” module (Three in total).
Use or block to
send the message you want to broadcast. Click the drop-down box of the block,
select “New Massage” then input a content into the pop-up box “new message
name”. You can input Chinese, English or numerical symbol.
Figure 4-13
Figure 4-14
How about adding a timing system for the game? Just give it a try!
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 38 / 158
⚫ When the program starts, display the shape 1, add a variable “blood volume”
and set its value to 100;
⚫ When the variation of the camera’s position is over 50, the blood volume
decreases randomly, play screaming sound and switch to shape 3. If the
blood volume is 0, switch to shape 2 and display the sprite “KO!”;
⚫ Shake the micro:bit board , then the blood decreases greatly, paly the
screaming sound and switch to shape 4. when the blood is depleted,
change to shape 2 and display sprite “KO!”.
5.2 Programming
1. Create sprites
Figure 5-1
Use the paint to create a sprite “KO!” One shape is enough for him.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 40 / 158
Figure 5-6
2. Add sound effect
Open the sound library, add two screaming sound effect. The method is shown
below.
Tool Description
Record a sound
Figure 5-7
3. Load camera function
Figure 5-8
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 42 / 158
Sprite Blocks
Unknown
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 43 / 158
Sprite Blocks
Chart 5-2
Except the above play modes, you can also shake the board to relieve the stress,
by which to accelerate the decrease of the blood volume. The code is similar to
that of the above playing modes. But the blood will be depleted more quickly
by shaking. Besides, we set a different screaming sound for this mode and adopt
shape 3 here.
We need to focus on the details when programming, for example, the value of
the blood volume decreases by 5-10 randomly, but when the current blood
volume is 4, the blood value will be negative after the last step. Besides that, after
the blood of the sprite has been run out, can we still make him make screaming
noise and switch shapes by punching? Absolutely not. Therefore, we set that if
the blood value is less than 10, the blood volume is set to 0, and in this
circumstance, stop playing sound and changing shapes. All programs stop
running. We can use “if then… else” function to realize all of these.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 44 / 158
Blocks Description
Chart 5-3
Figure 5-9
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 45 / 158
Start the game, the blood volume decreases gradually, as the figure 10 and 13
shows.
Chapter 6. Chatbot
Have you ever chatted with iphone’s Siri or interact with a smart robot? If not,
that’s ok, cause now we can directly program an intelligent chatbot by ourselves
with mind+ and micro:bit. Isn’t that sounds cool! Well, let’s begin.
The AI translations will be involved in this chapter, also we will get to know about
the temperature sensor on the micro:bit board.
In this case, we are gonna create three sprites: Santa Claus, a boy and a girl, as
figure 6-2 shows. The boy and girl asked Santa Claus respectively “what can I get
for my Christmas present?” Santa Claus thinks for a while and then determines to
pick a gift randomly from his pocket. But here comes a problem: there are crystal
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 47 / 158
ball and Transformer in his pocket, so a situation like this could happen: the crystal
ball was sent to a boy and the Transformer went to a girl, then it would be
awkward! To deal with that problem, we need to classify our gifts into For Girl
and For Boy. Cause it could be much reasonable to pick a present from For Girl
list when a girl asked for her gift.
Figure 6-1
Figure 6-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 48 / 158
Sprites Blocks
Chart 6-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 49 / 158
In this case we learned how to create a list, add items for the list, hide and
acquire a certain item form the list and so on. We will use a new block
here . It can be found in “Operators” module. The block
concatenates or “links” the two values together and reports the result.
We will use list to detect chat language in this case. The functions the programs
need to realize: obtain a speech the user input, divide it into single character
and put them into a list, then check every item in the list.
Figure 6-3
Figure 6-4
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 50 / 158
Secondly, divide a speech into single characters and place the characters into
a list.
Figure 6-5
Character Detection
Input “I am Micko” and check the result. So far, we can detect some particular
characters of the chatting language.
With the knowledge of list we can make much more fun programs, detect
language, chatting room and so on. Ok, now let’s start to make the chatbot.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 51 / 158
Figure 6-6
⚫ Repeatedly run the program “ask … and wait”, and “answer”. Add “answer”
to the end of the list to make it like an effect of asking question.
⚫ Embed the “if then…else” repeatedly and detect if the words like “time”,
“joke”, “song”, “translation”, and “temperature” includes in the “answer”.
6.3 Programming
We can ask the chatbot to tell us time, temperature, or let him tell jokes, play
music and do translations. The scripts are shown in figure 6-7.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 52 / 158
You can play the sound in the sound library, record your own sound or upload a
sound file from your computer. We use the sound “dance around” in the loops
of the sound library.
Figure 6-7
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 53 / 158
Figure 6-8
Figure 6-9
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 54 / 158
the micro:bit board will detect the current temperature of the device. (unit: °/℃)
Figure 6-10
Figure 6-11
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 55 / 158
Figure 6-12
Figure 6-13
Gorgeous Fireworks always comes together with a backdrop of night sky. We will
use light sensing function of micro:bit to simulate the firework exploding in the
night sky.
We also will get to know how to use the light sensor on micro:bit.
7.1 Clone
In some projects, you want several identical characters, they must have exactly
same motion scripts and shapes. The only difference is their positions. How to do
that? Well, the first idea coming to us is to duplicate. Yes, we could first design
the motion scripts and shapes of one sprite and then duplicate it as much as we
need. But if we want to revise some functions of these sprites, we have to do it
one by one or delete all the duplicated sprites, revise the original one and
duplicate it again. So complicated, right? But now with the function of clone,
this will be just as easy as a pie!
What’s the difference between clone and duplicate? We can explain it in this
way: duplicating is a step that happens in the period of editing and there will be
more sprites appearing on the editing area of sprite and background when you
start duplicating, but cloning is a feature that allows to create a clone,
or semi-duplicate, of itself while the project is running and there will be only one
sprite remaining on that editing area. The “when I start as a clone” block will
perform in the clone once it is created. The blocks relating to clone can be found
in “Control” module. As figure 7-1 shows.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 57 / 158
Figure 7-1
Next, in the case of Starry Night Sky, we will learn how to use clone in the actual
application.
We set the blue sky and star as the background and sprite respectively. Look at
the scripts below, we can find that the star is cloned four times. When it starts as
a clone, its size gets smaller, and it will move to the position (55, 44). Shown as
figure 7-2. The four clones of the star will stay at the same place, drag and move
it, the lovely stars will appear.
Figure 7-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 58 / 158
How to scatter the stars over the blue sky? Yes, a random function totally works
here.
Figure 7-3
At last, we need to make these stars seem to twinkle. Where to place the scripts
of this part?
Of course, it should be put under the block “when I start as a clone”. Since the
parent of clone. do not have the function of shining, we have to use the “delete
this clone” or “hide” block to realize that effect.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 59 / 158
Figure 7-4
The effect of the program will be like this: click the mouse, then the colorful
flowers appears on the stage, as figure 7-5 shows. Well, now, consider if the clone
function is needed here. If so, what is the parent of the clone? Take a look at the
pictures below, some flowers have one layer of petal while the others have two,
so obviously that is randomly chosen. To achieve an effect like that, we have to
use the loop function.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 60 / 158
Figure 7-6
Revise the parameters in the blocks below, and check the result.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 61 / 158
⚫ Create a sprite “red dot” as the initial shape (shape 1) of the firework. Upload
the pictures of firework exploding as the other shapes of the sprite;
⚫ Click green flag, determine if the ambient light is less than 1, if so, the sprite
clones itself for 20 times;
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 62 / 158
⚫ When starting as a clone, the shape 1 slides to a random position from the
lower part of the stage, play the sound effect of firework exploding;
⚫ Meanwhile, set size of the sprite to 20%, switch the shape to the rest shapes,
repeatedly run the program to enlarge the sprite, add color and ghost effect
for the sprite. After one firework exploded, delete the clone to realize an
effect of randomly exploding.
7.3 Programming
Costumes design: paint a red dot (shape 1) as the initial state of the firework,
then upload several fireworks pictures as the rest shapes. As figure 7-9 shows.
Figure 7-9
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 63 / 158
Sound Design: search for the sound of fireworks exploding on the internet, upload
it into Mind+. You can trim the sound to make it shorter.
Figure 7-10
Sprites Blocks
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 64 / 158
Chart 7-1
Figure 7-11
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 65 / 158
Figure 7-12
We will learn how to use micro:bit expansion board and buzzer in the actual
application.
Figure 8-1
Chart 8-1
By default, the center point is right at the middle of the canvas, also is the centre
of the paintbrush. If we don’t adjust the position of the pen, the picture it draws
will be like that showing in figure 8-2. We can use “select” function to select the
pen, and drag the nib of the paint to the center of the canvas. Shown as figure
8-3.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 68 / 158
Figure 8-2
Figure 8-3
Next, we will go to the programming part. Please make the distinctions of the
two pictures below and get to know the usage of “pen up and pen down”.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 69 / 158
Figure 8-4
Figure 8-5
Case 2: Draw Snowflake
All snowflakes have six sides. But we just need to draw one side of the snowflake
and then run the loop body for 6 times to complete the rest. The entire programs
are displayed below.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 70 / 158
Add stamps at the bottom of the above programs. There will be one more
butterfly on the stage.
⚫ Draw a sprite “disc”, and make it rotate to simulate the effect of playing
music;
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 71 / 158
⚫ Add script blocks for the background: click green flag to increase the color
effect, and play the music 1 to 10 randomly.
8.3 Programming
There are two sprites in the programs: a girl whose outline can be used as a paint
to draw lines on the stage, and a rotating disc. Besides, we add a spot-light
background for the stage, and we can also make the background to change
color effect and play music randomly by programming.
Sprite Blocks
Ballerina
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 72 / 158
Music Disc
Background Blocks
Spot-light background
Chart 8-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 73 / 158
We can tick the loudness in the sensing module to display the loudness value on
the stage. Shown as below.
Figure 8-8
There are rubber bumpers, silkscreen and power indicator on the back of the
board that could keep the expansion installed stably on the board as well as
prevent reverse connections. Besides that, Micro:Mate adopts standard Gravity
colorful pins to help users to distinguish different pins. The expansion board is
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 74 / 158
perfectly suitable for electric experiments, small robot, interact device, wearable
device and so on.
Figure 8-10
Voltage Switch: there is a voltage switch on the expansion board that can switch
the power between 3V, 5V and OFF.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 75 / 158
D&A: We can find the silkscreen “D” and “A” on the board. “D” represents the
digital signal (digital pin). “A” is the analog signal (analog pin)
One of the most advantages of the IO expansion board is that it greatly expands
the number of the power and GND interfaces. So, there would be no need to
worry about the interface number problem when you want to connect more
sensors.
Connect micro:bit to the computer with micro: USB. Micro:bit USB, and complete
the installation. Connect the buzzer to the pin 8 of the expansion board, please
note that the black wire should be plugged into the GND pin, as figure 8-12 shows.
Run the program and check the result.
Figure 8-12
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 76 / 158
Figure 8-13
The programs in this chapter will be completed under the code mode. So, switch
to code mode before getting started!
⚫ When the loudness is over 100, the motion sensor senses a body’s
approaching or the touch sensor detects an object touched the sensor
electrode, the buzzer makes noise and the LED lights up.
⚫ If an adult receives the alarm, he needs to press down the button to stop the
buzzer and turn off the LED.
Figure 9-1
Figure 9-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 79 / 158
3. Blocks descriptions:
Block Description
4. Programming
Connect all parts to micro: bit expansion board. Output module: buzzer to Pin 8,
LED to Pin 1. Input module: sound sensor to Pin 0, IR motion sensor to Pin 2, digital
push button to Pin 12, digital touch sensor to Pin 16.
There are two programming ways to achieve the effect of security alarm.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 80 / 158
Method 1: put all the detection parts in a main program, if any detection is
triggered, the buzzer begins playing sound and the LED module lights up until the
button is pressed down. Then the whole programs stop and all devices stop
detecting, which is just similar to the function of the “stop ()” block in mind+.
Figure 9-3
Method 2: put each kinds of detection in a thread independently, and the three
threads execute concurrently. If the button is pressed down, turn off the buzzer
and LED module. However, all sensors are still in detecting state. As figure 9-4
shows.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 81 / 158
Figure 9-4
layer of material with certain thickness like plastic and glass, but the sensitivity will
change with different thicknesses and touch areas.
Note: to reduce the sensing error, do not let the sensor touch any object that
you are not going to detect in using.
The analog sound sensor has an on-board microphone, through which Arduino
can sense the loudness of a sound, then covert it to analog signal. That is to say,
the loudness of a sound is presented by the returned voltage value.
Infrared motion sensor can detect the IR light radiating from the moving objects
(human or animal). It can be used in the occasion requiring body movement
detection.
3.3-5v, 6V(max)
Voltage
Connector PH2.0-3
22mmx30mm
Dimension
Detect the motion of human or animal.
Output delay time (high level):2.3-3s;
Principle Detecting angle: 100 degrees;
detecting distance: 7m
Chart 9-5 Figure 9-8
module: sound sensor to Pin 0, IR motion sensor to Pin 2, digital push button to Pin
12, digital touch sensor to Pin 16.
Figure 9-11
Drag and combine blocks to code, and upload the program to micro:bit, then the
Figure 9-12
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 85 / 158
sensors we used in the project, there are much more sensors in mind+ waiting for you,
you can create more fun and cool projects with them, come and explore!
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 86 / 158
⚫ Scratch Mode
Click green flag, repeatedly execute the program that the rabbit switch to next
shapes to form an effect of running;
The bubble clone itself continuously, and run the programs: generate from the
bottom, move up, disappear if it touches the edge, then delete the clone.
⚫ Code Mode
When micro:bit main program starts, light up two LED as the eyes of the rabbit
bubble machine, the servo repeatedly rotates from 0 to 60 degree to realize the
waving arms function of bubble machine. Use a blower to blow bubble.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 87 / 158
We are going to make a scene in this mode: a rabbit is running in a forest, and
there are full of bubble around it.
Key point:
There we should know a little bit about relative movement. The background
forest is moving left in relation to the rabbit. Hence, the rabbit keeps changing
shapes at a fixed position. The value of X coordinate of the background sprite
constantly decreases, so it will seem like the rabbit is running forward.
Figure 10-1
Clone
We have learned about the clone before. Go to chapter 7 if you forgot. The
clone the bubble constantly appears, and disappears when touching the edge,
then delete the clone.
Sprites Blocks
Background
Bubble
Rabbit
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 89 / 158
Figure 10-2
1) Connection
Connect servo to pin P8 of the expansion board, two LED module to P0 and P1.
Use external power for the device.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 90 / 158
We can shop a blower online, and it usually comes with power supply and switch.
Shown as figure 10-5. Or we can make it by ourselves. Thus, we need a servo, fan
blade, and a device like a funnel for gathering wind.
Figure 10-5
First, we cut a rabbit shape with a piece of cardboard, then arrange all parts
together inside a box. Tie up the servo’s shaft and bubble ring with iron wire.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 91 / 158
Figure 10-6
Switch to “Code Mode”, drag and snap blocks to code, and upload to the
micro:bit.
4. Programming effect
Prepare the bubble solution, turn the blower and power up the expansion board,
the bubble machine will start to work.
10.4 Servo
Figure 10-9
Servo is a rotary actuator and linear actuator that allows for precise control of
angular or linear position, velocity and acceleration. It is known as servomotor.
Red wire to positive pole, brown wire to negative pole and orange wire is signal
line.
Type 180°Servo
Voltage 4.8V
PH2.0-3
Connector
23x12.2x29mm
Dimension
Torque: 1.6kg/cm(4.8V)
Speed: 0.14 Second /60degree(4.8V)
Principle
Operating Temperature:30~+60℃
Dead Zone Width: 5 micro-second
Chart 10-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 93 / 158
What is a robot in your imagination? Does a robot must look like people?
Look at these robot cooks, they are familiar with traditional Chinese 8 cuisines:
Shandong cuisine, Sichuan cuisine, Cantonese cuisine, Chinese Cabbage,
Fujian Cuisine, Huizhuo cuisine, Hunan cuisine and Zhejiang cuisine. They can
cook more than 40 dishes. Their programs to cook strictly follow the standards
about temperature, time, materials that given by famous cooks. When the food
is cooked, the cook robot passes it to waiter robot, then the waiter robot could
serve customers. In the restaurant, waiter robots can drive by themselves and
avoid obstacles automatically, and auto optimize the path. They can pass dishes
and recover dishes.
There are different function robots in life, but what is the robot? Its components
are similar to human. In fundamental, human body are composed by 5 main
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 95 / 158
parts: body and muscle to move; sensor to receive environment information and
control muscle to move; energy source to provide energy to muscle and sensor.
Brain to process sense-information and command muscle movement. A typical
robot should be equipped with a moveable body, a device similar to motor, a
sense system, a battery and “brain” to compute and control all. Essentially
speaking, robot is the “animal” made by human. It is a machine that imitate
human activities and animal activities. In our life, it can help or even replace
human to work.
Execute Device: first of all, almost all robots have one moveable body. Some
only has motorized wheel, some equip with a large amount of moveable parts.
Similar to bones, these independent parts are connected by joints. Normally, the
number of joints represents the degree of freedom.
Drive: according to the commands from main control, the drive motivates robot
based on the power part. Input electronic signal, then the power output the line
and the angular displacement. Majority of robot drives are electricity drive
device, such as stepper motors, servo motors and so on. Besides, hydraulic and
pneumatic drives are also adopted.
Detect: monitor the robot movement and work in reality, and feedback the
necessary information to the control(brain). There are two categories of detect
device, one is the internal sensor and the other is the external sensor. The internal
sensor can detect all internal part of the robot, for an example, the joint position,
speed, acceleration and so on. Then it will pass the detected information to the
controller in the form of feedback signals, forming a close-cycle control. The
external sensor is used to get information about object, environment and so on.
In this way, the robot activity can adjust the outside changes, and can reach
the high-level automation, and even make the robot be sensitive, be smart.
Specifically, with the help of information about object and environment given
by the eye sensor and sound sensor, the robot can form a big feedback cycle,
then the robot activity is more precise.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 96 / 158
Control: similar to the human brain, process the information and make decisions.
It can be a small single chip, it also can be a microcomputer.
Except industry area, the special robots are used in all areas to serve human.
Service robot: robots around our lives such as restaurant robot, sweep robot and
so on.
Entertainment robot: have you ever seen robot band? So as paint robot, toy
robot, dance robot. Entertainment robot makes our lives more joys.
Underwater robot: guess what is used for? Yes! The underwater robot can detect,
research and rescue. In artist works, Tibetan Plateau waters are called as tear of
earth. People has adopted smart underwater robot to detect and collect water-
quality information about tear of earth waters. Moreover, the underwater robot
has already put into emergency rescues. It can observe, analyze and judge the
object position and situation in water, then pass data in real-time.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 97 / 158
Figure 11-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 98 / 158
Here we can see, there are many sensors in Maqueen, line-tracking, ultrasonic,
infrared input. And 2 motors, 2 LEDs and 4 RGBs, and a buzzer. BBC micro:bit is
an ARM single chip, equipped with many onboard resources, such as Bluetooth,
wireless, acceleration, e-compass and buttons, and 5 x 5 matrix. The
combination of Maqueen and micro:bit can realize many functions such as
infrared remote control, line-tracking, light-tracking, obstacle avoidance,
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 99 / 158
Figure 11-3
Figure 11-4
Here Maqueen is already, wait, the body is ready, but we totally forget one
important thing, make Maqueen intelligent. What should we do now? Yes, we
need to programming the brain. My great engineers, come to work!
Better tools make for good work. Firstly, I would love to introduce Mind+, a
programming software developed based on Scratch3.0. It is suitable for middle
high school education; the download website is http://www. mindplus.cc/.
Let’s explore mind+ and find its features and make Maqueen a smart robot.
12.1 Programming
Mind+ is designed based on Scratch, but beyond. Through mind+, you can
control robot, know more about artificial intelligent, IOT(internet of things) in
extensions. It is one of the best choices to begin programming.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 101 / 158
Figure 12-1
Block section: This is the “tool section” of the stage. We need all kinds of tools to
give an excellent performance on the stage, and we can get more extra tools
in “Extensions” to support different hardware.
Sprite: adjust the size, color and position of your main character.
After a successful installation, Mind+ will open scratch in default. We can check
the mode in the right top. Click the icon to switch to code. The mode code aims
at hardware, including kits, main boards, sensors, actuators, communication
modules, displays, modules… Which means scratch is mainly for software
programming, and code is mainly for hardware programming.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 102 / 158
Figure 12-2
Switch to Code mode, upload to device. The lower right black area is the serial
port printing area. Serial port is also called as COM. It is expansion port that
adopts serial communication protocol.
Figure 12-3
2. Close serialport
3. Clear data
4. Command box
5. Click to send
Chart 12-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 103 / 158
Figure 12-5
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 104 / 158
Please uninstall and re-install if the serial port you had installed the serial port.
Figure 12-6
Figure 12-7
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 105 / 158
Imaging, Maqueen is walking at a steady step with his back straight. Then, he
walks in front of the platform, stops and turns to audience, and smile. Finally, he
tuns back and moves on. We expect all his excellent performance.
1. Analyze Program
It is a simple program, the original idea is to move forward for 1s and turn left for
0.5s, then show smile and turn right for 0.5s, and keep going forward for 1s.
Figure 12-8
so on; they all could be trigger events. And the trigger event block should be put
in the program begins. So, we just need to drag blocks from the blocks to the
scripts and combine, then the program is done.
2. Optimize Program
How about your thinking? Is that similar to this program? Try uploading to the
device, is there any problem?
Yes, the robot does not move as expected. What’s the problem?
We find the left wheel does not turn when turning left, while the right wheel is still
running. At this time, Maqueen running in circle and the left wheel is the Centre.
How about make Maqueen rotate on its axis?
Figure 12-9
After repeating test, you will find that we need to obey the rule to control motors.
That is to control port, speed and direction in priority, then control time and
sensor conditions, finally set to the rotation speed to 0 and stop move. In this way,
the motor will not keep running. The finally program is shown as below:
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 107 / 158
Figure 12-9
Figure 12-10
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 108 / 158
13.1 Programming
Figure 13-1
How the effect? The program will light up 4 RGBs one by one every second. Let’s
make a little change to switch lights.
Figure 13-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 109 / 158
Figure 13-3
Check the figure below, the ultrasonic distance sensor transmits ultrasonic waves
in a certain direction, and starts timing when emitting. The ultrasonic waves
propagate in the air, and return to the emission area once encountered on the
way, and the ultrasonic receiver stops timing immediately upon receiving the
reflected wave. The ultrasonic wave travels at 340 m/s in air at 15 °C. Based on
the time recorded by the timer, the distance from the launch point to the
obstacle can be calculated.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 111 / 158
Figure 14-1
Figure 14-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 112 / 158
Figure 14-3
Make a test, you will find the robot is obedient. It really turns right to avoid
obstacles. How could it achieve? Let’s analyze the program.
Maqueen keeps going straight, only change status when meets obstacles. There
are two movements, go straight and turn right. Execute 2 conditions but without
forever cannot let Maqueen keep running. So, the block forever is needed at
here. The simple movement go straight can be realized by a simple block that
given in mind+.
The hard part is to realize the way to turn right. Take it easy, let’s ask and answer.
When we walk straight and need to turn, how could we do? We may stop and
turn, or just turn and keep walking. But a response time is needed at here.
Especially, a robot little Maqueen. So, we need to leave some time like 1s for
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 113 / 158
Maqueen to restart. Ok, now we just need to combine turn right with space time
together, then the other condition is finished. But how could we set the effective
distance range of obstacle be found? The answer is the blocks if… then…, and
<=. Then we combine all, it works.
Firstly, we should know how could Maqueen knows the environment light
strength? Is there a light sensor in micro:bit? Where is it?
In fact, there is no real light sensor in micro:bit, but how could it sense the light
strength? LED matrix. The LED matrix in micro:bit is used to sense the environment
lights. The principle is to convert LED drivers to inputs and sampling the voltage
weaken time, which is roughly in direct proportion to the environment light level.
Figure 15-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 115 / 158
When the driver is installed, click Connect device to connect hardware, then
click the serialport icon in the black serialport area. And the icon “open Serial”
will turn orange.
Programming to get the environment light value. From the data below we know
the current light is low. When light it with flashlight, the light strength is close to
200.
Figure 15-2
15.2 Light-chasing
Thinking draft: Maqueen keeps turning left in low light. While, when the light
strength is over 100, Maqueen follows the light direction to move. The speed
relates to the light strength. The larger the light, the faster the speed. And in other
conditions, Maqueen still executes turning left to find lights.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 116 / 158
Figure 15-3
Forever (infinite loop) repeat for limited times loop with conditions
Chart 15-1
Forever, as the name suggests forever is to repeat forever, again and again. The
lack of the block Forever will lead to short movement in the project above.
2. Repeat for () times: means specify the robot to repeat for concrete times
Of course, we can proceed this via sequence structure, like move forward for 2s,
then turn left for 0.75s, then turn left again for...
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 117 / 158
In fact, we can use the block infinite loop to realize it. The key point is the part to
loop and loop time. Move square, which means moving for forward and turns
direction and repeat for 4 times. And the repeat time regards to power supply
and friction, so you can make test and change it.
Figure 15-4
Judge the condition after executed loop body for once, keep executing until
the condition is satisfied. Flow chart is shown in the Figure 15-5.
Figure 15-5
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 118 / 158
Have a try: try modifying the light-tracking program to a repeat until structure,
the result is shown in the figure 15-6:
Figure 15-6
The infrared transmitter keeps emitting infrared light to the ground. When the
ground (white and light colors) reflect IR light, the receiver receives and the
output is 1(the blue LED will light on). When the ground (black or deep colors)
observes or cannot reflect the IR light, the receiver cannot receive the IR signal,
the output value is 0. In line-tracking, the sensor will keep detecting reflect
value from the ground to distinguish black and white line.
Design Idea: put the sensor to the left of the black wire and turn right, if the sensor
detects black line, turn left; and if the sensor detects white line, turn right. And
run it in loop.
Result: the robot keeps turning right and turning left, and finally moving with the
path “Z”.
Figure 16-3
Chart 16-1
Figure 16-4
Figure 16-5
Figure 16-6
Use functions to present the robot motion. And we just need to call functions in
programming.
Figure 16-7
Figure 16-8
5. Test
The line-tracking test path is shown in the figure 16-9, what’s the disadvantages?
Can we modify these disadvantages by decreasing the speed? Besides, will the
environment influence the robot judgement?
After learned the principle of line-tracking, can you realize line-tracking by other
programming methods?
Figure 16-9
Try programming to make Maqueen move in the light flat and stop in the edge.
Shown in the figure16-10.
Figure 16-10
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 125 / 158
Infrared remote-control device is common to see in our life. For low produce cost,
it is widely used in electric device like TVs, air conditioners, CD players,
telephones and so on. In general, infrared remote-control system is divided to 2
parts, emitter and receiver. The main part of the emitter is the infrared emitting
diode (LED). In fact, LED is a special emitting diode, because its inner material is
different from common emitting diodes, so it emitting infrared lights when be
powered. In practical applications, the LED only work when reverse bias is added,
which means LED should be used in reverse in circuit to get high sensitivity.
Drag the plastic strip in the battery to power-on. Toward to Maqueen and press
keys, the serialport will show related key value in real time.
2. Set variables
Variables are named storage zones in computer. Once you create a new
variable, the program will set a storage zone to store the variable and name the
zone at the same time. Therefore, you can get all stored in this zone by using the
variable name.
Figure 17-2
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 127 / 158
There are 3 types of variables are provided in Mind+, Numbers, Strings and Lists.
The program above uses Numbers, try name the variable as number. Create a
new function: Create number -> Name the variable. Then you can find the
variable number in Variables.
Figure 17-3
Pause 95 4 215
6 151 8 103
Chart 17-1
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 128 / 158
Task: remote-control Maqueen to move forward, move back, turn left and turn
left, then pause. Take keys like VOL+ as an example:
Figure 17-4
The difference between IR and RF Remote Control is different from carrier. The
infrared remote-control is directional, cannot be obstructed, short work distance
(usually 7meters at most), free from electromagnetic interference. While, radio
control transmits signal via radio wave. It is unidirectional, not need face to face
control, long distance (more than 10meters, even several miles) but easy to be
interfered. Radio remote-control can be applied to long distance wild and other
unidirectional areas, such as industrialized control.
The rf remote controller is a device that control remote objects by radio signals.
When signals are accepted by the receiver, you can drive and control machines,
electricity device to work, such as turn on/off circuit, control motor and so on. As
a complementary controller with IR controller, RF controller is widely used in
garage door, electronic door, barrier gate remote-control, alarm, wireless smart
home and industrial control.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 130 / 158
18.1 Gamepad
Figure 18-1
Pinout description:
Chart 18-1
Task: code to control LED matrix by gamepad, when up is pressed, show arrow
UP in micro:bit; when down is pressed, show arrow Down; when left is pressed,
show arrow left; when right is pressed, show arrow Right.
Figure 18-2
Result: when up is not pressed, the arrow UP shows. So, let’s make some change,
change conditions to logic NOT.
Figure 18-3
Figure 18-4
Have a try: try adding functions up and down, press X and the buzzer is ON, press
Y and the gamepad vibrates.
1. Task: remote-control Maqueen to move forward, move back, turn left, turn right
and pause by up, X, A, B, Y in micro: gamepad.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 133 / 158
Firstly, 2 micro:bit boards must in a same group, so that they can communicate
with each other without interference from other groups. Secondly, the transmitter
(Client) can send different types of information when different keys are pressed.
Information could be strings and numbers. Thirdly, the receiver can execute
corresponding actions when different numbers are received.
Figure 18-5
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 134 / 158
Figure 18-6
This class, we will take advantage of common INPUT and OUTPUT pins in
Maqueen to realize a simple research and rescue robot. According to the
requirements, Maqueen need to satisfy two conditions, research and rescue.
So the robot must integrate temperature sensor and smoke sensor. It can
feedback the on-site situation and remote transmit information to inform
searchers.
Nowadays, research and rescue robots become more and more important in
earthquake, fires, floods.
data received by the receiver, it would drive the servo rotate and animate
rescue operations, such as providing nutrition liquid and so on.
But only P0~P4 and P10 can analog read voltage. Below is the chart about all
pins and pin functions in micro:bit.
1. Connect servo
Method 1:
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 138 / 158
Connect servo and Maqueen with 3 pins Dupont wires. Connect one side of the
Dupont to a servo, the red wire to red wire of servo, VCC.
Connect the black wire to brown wire of servo, GND. Connect the blue wire to
yellow wire of servo, digital wire. The other side, plug the red wire to 3v, black
wire to GND, blue wire to P1. To fix these connectors, you can dismiss these wires
and tie to metal.
Method 2:
We also can use the port that ultrasonic sensor uses. Remove the ultrasonic
sensor from Maqueen, then plug the red wire to VCC and the black wire to GND,
blue wire to E(P2).
To distinguish it more easily, here we also use red wire, black wire and blue wire
to connect. As the figure 19-5 shows, we connect P1 to get data.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 139 / 158
The reason why we connect the data wire to P1 is that the thermoelectric sensor
belongs to input device.
Figure 19-7
19.3 Programming
Algorithm:
Put the detect transmitter and the receiver into the same wireless channel 7.
Open wireless functions of both sides.
Detect transmitter:
Once the detect transmitter finds vital signs, its thermoelectric sensor will get
signal and LED matrix will show image ‘Heart’ and send string ‘signal’ to the
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 140 / 158
receiver. Otherwise, LED matrix just shows image ‘---'which suggests no object is
detected.
Receiver: show the image ‘Square’ when no signal is received, otherwise, show
image ’Smile’. Press the button a, the receiver will send ‘ok’ to the detect
transmitter.
When the detect transmitter receives the signal ‘OK’, it will enable the servo to
90°to animate nutrition liquid transportation. Then the servo returns to the original
place in 1s.
Figure 19-8
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 141 / 158
3. Connection Diagram
Connect the servo of the detect transmitter to the P2 port in the rear end, and
the thermoelectric sensor to P1 in the front.
The receiver only has one micro:bit board, and supplied by external battery.
Figure 19-9
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 142 / 158
Figure 19-10
Bike-share make our life more convenient, just open your telephone and scan
the QR Code, then you can unlock and ride the bike. The IoT principle used in
Bike-share is a simple construction Telephone-Cloud-Bike. Through the APP
installed in smart phone, we can check bikes around, add credit and reserve
and so on. Cloud is the server controls all bike share system. It can communicate
with all bikes, collect command data, respond to operations from users and
managers. And the bike is the receiver to receive data and execute commands,
like GPS and unlock…
The smart lock represents the practical use of Internet of Things. It is a typical IOT
and Internet application. By communication technologies like smart sense,
identify technology and computing, it can transmit information between users
and bikes. The smart lock integrates GPS system and SIM, which can upload
position and e-lock status to cloud. It has many functions, such as buzzer alarm
(make sound like di-di-di before unlocking), vibration detection (alarm when the
bike falls or crashed), lock status detection (end billing automatically when
locked)
1st the core and foundation of IoT is internet, but expand and extent.
2nd the client expands and extends to objects with objects to exchange
information and communicate.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 144 / 158
Application is the biggest part of IOT development. The soul to develop IOT is the
innovation that based on users experience. Nowadays, IOT has widely used in
industry monitor, city manage, smart home and smart traffic and so on.
Figure 20-1
Let’s check the interfaces. TX is the transmitter, RX is the receiver, GND is the
cathode (-), VCC is the anode. The signal lights show different colors in different
situations. RED: abnormal status; GREEN: normal status; BLUE: Wi-Fi connecting;
Purple: MQTT disconnect; WHITE: Oblq firmware upgrade Please check the signal
light to get the module status before any adjustment.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 145 / 158
Figure 20-2
20.2 Functions
We want to remote control Maqueen by Internet. Maqueen moves forward
when received 1 from the transmitter, move back when received 2. Of course,
you can design your code and realize more actions.
Using pin-pin DuPont wire to connect the module to the port that used by
ultrasonic wave sensor. RED to VCC, BLANK to GND, GREEN to T(P1), BLUE to E(P2).
Surely, you can also connect the OBLOQ module to the mental port which is in
rear end of Maqueen.
2. IOT Platform
Log in IOT platform to set, now we take the network IOT as an example.
Figure 20-5
3. Programming
Firstly, load OBLOQ IOT module, then you can see blocks with different functions
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 147 / 158
Figure 20-6
Figure 20-7
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 148 / 158
In the beginning of program starting, we need to initialize the IOT setting. To start
with, we need to set the wi-fi environment. Please choose the available Wi-Fi to
guarantee communication between Maqueen and receiver. Secondly, the IOT
platform parameters Iot_id, Iot_pwd and topic in Mind+ must consistent with IOT
platform. You can copy and past to avoid wrong input. Finally, set GREEN wire
to P1 and BLUE wire to P2. Click the icon eye can show and hide password.
Figure 20-8
IOT platform can send data to Maqueen and receive data from Maqueen. But
please notice the data is string, so do not forget to add “ ”, otherwise min+ will
report error.
Welcome to the DFRobot blog: https://www.dfrobot.com/blog-tag-microbit.html 149 / 158
Figure 20-9
Then, let’s try send command in IOT platform and check how Maqueen responds.
Test result:
Let’s check the information received and sent in platform. We can also realize it
by telephone IOT app.
Figure 20-10
This program uses MQTT agreement to connect IOT. And OBLOQ also can use
HTTP agreement. Different from MQTT agreement, HTTP agreement does not
need to use parameters iot_id, iot_pwd but use the methods get, post and put.
Bibliography