Micro:bit Basic Projects

Code Download

top1

Code download:

Download and unzip these files. Here all codes are in folder Code.

bottom1

Project 1: Heartbeat

j101

Introduction

3top

This project is easy to conduct with a micro:bit main board, a Micro USB cable and a computer. The micro:bit LED dot matrix will display a beating heart. It serves as a start for your entry to the programming world!

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code

Find code blocks:

In the resource file included with the tutorial, find the Project 1 code folder:

![Project 1 code folder](img/Project 1 code folder.png)

① Choose an icon to show on the LED matrix.

j102

line1

Build code blocks:

j103

Click “JSJavaScript” to see Java code:

j104

Pull down to click “Python” to see Python code:

j105

Test Result

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

The micro:bit on-board LED matrix circularly shows j106 and j107.

If the downloading is not smooth, please remove the USB cable from the main board and then reconnect them and reopen Makecode to try again.

4bottom

Project 2: Single LED Blinking

j101

Introduction

3top

In this project, we intend to control a certain LED on the micro:bit board to light up.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Principle

2top

The LED dot matrix consists of 25 LEDs arranged in a 5 by 5 square, corresponding to axis-X and Y. In order to locate these LEDs quickly, we can regarded this matrix as a coordinate system by marking 0~4 from top to bottom and from left to right.

Therefore, the LED sat in the second of the first line is (1,0) and the LED positioned in the first of the third row is (2,0), the fifth of the first column is (0,4), the second of the third column is (2,1), and others likewise.

2bottom

Test Code

Find code blocks:

① plot a coordinate to light up this LED.

② unplot the coordinate to turn off this LED.

③ toggle the state of a certain LED by a coordinate: if it is now on(off), turn it off(on).

j202

④ turn on/off the LED matrix.

j203

⑤ a pause of the program, whose unit is microseconds. It can be used to slow down the execution speed of the program.

j204

line1

Build code blocks:

j205

Test Result

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

The LED at (1,0) blinks for 1s and then LED at (3,4) blinks for also 1s. These actions repeat.

4bottom

Project 3: LED Dot Matrix

j101

Introduction

3top

Dot matrices are very commonplace in daily life. They have found wide applications in LED advertisement screens, elevator floor display, bus stop announcement and so on.

The LED dot matrix of Micro: Bit main board contains 25 LEDs in a grid. Previously, we have succeeded in controlling a certain LED to light by integrating its position value into the test code. Theoretically, we can turn on many LEDs at the same time to show patterns, digits and characters.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code

Find code blocks:

① show a number on LED matrix. If it is a number more than one bit, it displays content that scroll into view.

② show icons on the LED matrix.

③ choose an icon to show on the LED matrix.

④ show string on the LED matrix. If the string is too long, it displays content that scroll into view.

⑤ turn all LED on the matrix.

j302

⑥ a pause of the program, whose unit is microseconds. It can be used to slow down the execution speed of the program.

⑦show arrows on the LED matrix.

j303

line1

Build code blocks:

j304

Test Result

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

Numbers 1, 2, 3, 4, 5 on the 5×5 LED matrix, and icons repeatedly show on it, including:

“down” arrow j305;

string “Hello!”;

“heart” icon j306;

“northeast” arrow j307;

“southeast” arrow j308;

“southwest” arrow j309;

“northwest” arrow j310.

4bottom

Project 4: Programmable Buttons

j401

Introduction

3top

Buttons can be used to control circuits. In an integrated circuit with a button, the circuit is connected when the button is pressed and if you release the button, the circuit is open.

Micro: Bit main board boasts three buttons: two programmable buttons (marked with A and B), and a reset button at back. By pressing the two programmable buttons, three different signals can be input. We can press button A or B or both so that the LED dot matrix shows A, B and AB respectively.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code 1

Find code blocks:

① enable a program (when something happens, for instance, when a button is pressed). When button A or/and B is pressed, run the program.

When you enable this function in browser, please click the buttons on your screen rather than on the micro:bit board.

  • button A or B: press and release within 1s to enable the function.

  • button A and B: press A and B, release one of them within 1.5s after the second button is pressed to enable the function.

j402

② show string on the LED matrix. If the string is too long, it displays content that scroll into view.

j403

line1

Build code blocks:

j404

Test Result 1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

Press button A and release, the 5×5 LED matrix shows “A”; press button B and release, it shows “B”. Press button A and B at the same time and then release, the matrix shows “AB”.

4bottom

Test Code 2

Find code blocks:

① enable a program (when something happens, for instance, when a button is pressed). When button A or/and B is pressed, run the program.

When you enable this function in browser, please click the buttons on your screen rather than on the micro:bit board.

  • button A or B: press and release within 1s to enable the function.

  • button A and B: press A and B, release one of them within 1.5s after the second button is pressed to enable the function.

j402

② create a variable.

j405

j407

③ enable an equal sign to assign a number or string to the variable.

AssignmentOperator

When enable an equal sign to make an assignment, this sign is also called assignment operator, and the content is called a value.

e.g. assign number:

j411

This block assigns 5 to variable item and then it will show on the LED matrix.

e.g. assign string:

j412

This block set variable name to Joe and then it will show on the LED matrix.

NOTE: The assignment operator are available to many types of variables, including numbers and strings.

④ add the value of the variable by a certain value.

ChangeValue

With this block, the variable will add by a certain value as you set. This is called addition assignment.

⑤ a variable.

item

⑥ turn on/off the LED matrix.

j203

⑦ show a bar graph of the value.

Bar graph displays numbers as lines of varying lengths. The value is plotted on the LED matrix as the ratio of the value to the height value of the set maximum range. Therefore, if there are 25 LEDs on the matrix, number 9 with a height value of 50 will display about 5 LEDs on the matrix.

j408

⑧ the condition can be true or false to determine whether to run the code.

Code blocks in “if” only run when the condition is true. A true condition can be obtained from the comparison of a variable to a value or to a variable.

⑨ comparison operator, comparing whether the left and right are equal, and the result of the comparison is Boolean true or false.

j409

line1

Build code blocks:

j410

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

Press the button A to increase the bar height, and the lighting LEDs on the matrix rise. Press the button B to decrease the bar height, and the lighting LEDs on the matrix descend.

4bottom

Project 5: Temperature Detection

j501

Introduction

3top

The micro:bit main board is actually not equipped with a temperature sensor, but uses nNFR52833 chip for temperature detection. Therefore, the detected value is much closer to the temperature of the processor, so there maybe deviation from the ambient value. Its detection range is -40 ~ 105℃.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code1

Find code blocks:

① write Name: value and a line-break(\r\n) on the serial monitor in pair.

j502

It is common to use Name value pairs when reporting or recording data. They are displayed together as a text output string in the form of Name and value. The Name and value are separated in the string by a colon”:”. A Name value pair that reports a temperature of -15 degrees looks like:

Temperature: -15

② set the USB used to serial input and output.

j503

③ ambient temperature. Temperature is measured in degrees Celsius (metric). The micro:bit can find ambient temperatures by the temperature of its computer chip.

j504

④ a pause of the program, whose unit is microseconds. It can be used to slow down the execution speed of the program.

j204

line1

Build code blocks:

j505

Test Result1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

Click “Show console Device”.

j506

Display the serial output data, press the nNRF52833 processor of the micro:bit board and the temperature begins to rise slowly after a period of time, as shown below:

j507

If you’re running Windows 7 or 8 instead of Windows 10, Google Chrome won’t be able to match devices. so you’ll need CoolTerm.

Open CoolTerm and click Options to select SerialPort, set COM port and put baud rate to 115200 (after testing, the baud rate of USB SerialPort communication on Micro: Bit main board is 115200), and then click OK and Connect.

j508

CoolTerm serial monitor shows the change of ambient temperature as shown below:

j509

4bottom

Test Code 2

Find code blocks:

① turn on/off LED matrix.

j203

② run code according to whether the Boolean condition is true or false. If you want to run other code when the opposite condition is true, you can put them in “else”.

③ comparison operator, comparing the left and right value, and the comparison is Boolean true or false.

j510

④ ambient temperature. Temperature is measured in degrees Celsius (metric). The micro:bit can find ambient temperatures by the temperature of its computer chip.

j504

⑤ show icon on the LED matrix.

j102

line1

Build code blocks (The threshold 35 in the code can be modified as actual needs):

j511

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

When the ambient temperature is colder than 35℃, micro:bit LED matrix shows j107.

Press the temperature sensor on the board to let the ambient temperature reach or exceed 35℃, LED matrix shows j106.

4bottom

Project 6: Geomagnetic Sensor

j601

Introduction

3top

In this project, we introduce the geomagnetic sensor (magnetometer) on the micro:bit board, which can not only detect the geomagnetic field strength, but also be used as a compass to determine the direction.

The main component of a common compass is a magnetic needle, which can be turned and pointed to the geomagnetic North Pole under the force of the geomagnetic field to identify the direction.

The built-in geomagnetic sensor (magnetometer/compass) reads the geomagnetic field data to obtain Azimuth Angle relative to the north magnetic pole, so as to convert the result to between 0 and 359 degrees for orientation.

j615

We need to calibrate it for the first by rotating. Please note that metal materials around may attenuate the accuracy of the reading and calibration.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code 1

Find code blocks:

① enable a program (when something happens, for instance, when a button is pressed). When button A or/and B is pressed, run the program.

When you enable this function in browser, please click the buttons on your screen rather than on the micro:bit board.

  • button A or B: press and release within 1s to enable the function.

  • button A and B: press A and B, release one of them within 1.5s after the second button is pressed to enable the function.

j402

② the direction that the micro:bit is facing on the compass. The micro:bit measures the orientation of the compass from 0 to 359 degrees through its magnetometer chip. Different numbers indicate north, east, south and west.

j602

③ calibrate compass.

j603

④ show a number on the LED matrix. If it is a number more than one bit, it displays content that scroll into view.

j604

line1

Build code blocks:

j605

Attention: We need to calibrate it for the first by rotating. Please note that metal materials around may attenuate the accuracy of the reading and calibration.

Test Result 1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

Press the button A and the micro:bit on-board LED matrix shows “TILT TO FILL SCREEN”. Rotate the board until all 25 red LEDs are on as shown below.

j606

After completing calibration, a smile j607 will appear. Press button A, and the detected magnetometer value will show. And the direction north, east, south and west correspond to 0°, 90°, 180° and 270° respectively.

4bottom

Test Code 2

Find code blocks:

① the direction that the micro:bit is facing on the compass. The micro:bit measures the orientation of the compass from 0 to 359 degrees through its magnetometer chip. Different numbers indicate north, east, south and west.

j602

② calibrate compass.

j603

③ Create a variable.

j608

④ show icons on the LED matrix.

j609

⑤ run code according to whether the Boolean condition is true or false. If you want to run other code when the opposite condition is true, you can put them in “else”.

j610

⑥ comparison operator, comparing the left and right value, and the comparison is Boolean true or false.

⑦ logic operator, output true only when both conditions are true.

j611

line1

Build code blocks:

j612

Read the value of the magnetometer relative to the north magnetic pole continuously, and then determine the range of the value, and display the corresponding arrow on the matrix: the arrow points to the current magnetic North Pole.

j613

For the above picture, the arrow pointing to the upper right when the value ranges from 292.5 to 337.5. It points in the direction of the magnetic North Pole.

0.5 can’t be input in the code, so the values we get are 293 and 338. We add other statements to make a set of complete code.

j614

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

After calibrating, tilt micro:bit board and the LED dot matrix displays the direction signs that points in magnetic North Pole.

j613

4bottom

Project 7: Accelerometer

j701

Introduction

3top

The micro:bit board boasts a built-in LSM303AGR acceleration sensor (accelerometer) which includes standard, fast, plus and high-speed mode (100 kHz, 400 kHz, 1 MHz and 3.4 MHz) of I2C serial bus interface and SPI serial standard interface for external communication, with resolution of 8/10/12 bits and range of ±2g, ±4g, or ±8g.

When the micro:bit board is at rest or in uniform motion, the accelerometer only detects the acceleration of gravity. If the board is slightly swung, the detected acceleration is much less than the that of gravity, but the difference can be ignored. Therefore, we mainly detect the change of gravitational acceleration on the x, y, and z axes.

In this project, we will introduce how to measure the position of the board with the accelerometer. And then we may have a look at the original three-axis value output by the accelerometer.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code 1

Find code blocks:

① enable a program (when something happens). When you do actions on the board, execute the code.

j702

There are many optional actions.

j703

② show a number on the LED matrix. If it is a number more than one bit, it displays content that scroll into view.

j604

line1

Build code blocks:

j704

Test Result 1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading code and powering on, if we shake the board(any direction), the LED matrix displays number “1”.

When the micro:bit board is perpendicular to the ground with the logo on the top, it shows number 2.

j705

When the micro:bit board is perpendicular to the ground with the logo at the bottom, it shows number 3.

j706

When the micro:bit board is parallel to the ground with the matrix facing sky, the number 4 appears.

j707

When the micro:bit board is parallel to the ground with the matrix facing ground, the number 5 exhibits.

When the board is tilted to the left, the LED dot matrix shows the number 6.

j708

When the board is tilted to the right , the LED dot matrix displays the number 7.

j709

4bottom

Test Code 2

Find code blocks:

① write Name: value and a line-break(\r\n) on the serial monitor in pair.

j502

It is common to use Name value pairs when reporting or recording data. They are displayed together as a text output string in the form of Name and value. The Name and value are separated in the string by a colon”:”. A Name value pair that reports a temperature of -15 degrees looks like:

Temperature: -15

② set the USB used to serial input and output.

j503

③ obtain the value of acceleration in one of the three dimensions (mg), or the resultant force in all directions (axis x, y, and z).

acceleration of the micro:bit board: how fast it accelerates or decelerates.

j710

④ Short pause for the milliseconds to slow down the program.

j204

line1

Build code blocks:

j711

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board. Click “Show console Device”.

j712

After referring to the MMA8653FC data manual and the hardware schematic diagram, the accelerometer coordinate of the micro:bit are shown in the figure below:

j713

As shown below, the decomposition values of the acceleration on the X, Y, and Z axes, and the synthesis of the acceleration (acceleration synthesis of gravitational acceleration and other external forces), are displayed respectively:

j714

If you’re running Windows 7 or 8 instead of Windows 10, Google Chrome won’t be able to match devices. so you’ll need CoolTerm.

Open CoolTerm and click Options to select SerialPort, set COM port and put baud rate to 115200 (after testing, the baud rate of USB SerialPort communication on Micro: Bit main board is 115200), and then click OK and Connect.

j508

CoolTerm serial monitor shows the decomposition(in X axis, Y axis and Z axis respectively) and the synthesis(acceleration synthesis of gravitational acceleration and other external forces) of acceleration. The data changes can be seen as follows:

j715

4bottom

Project 8: Light Brightness Detection

j101

Introduction

3top

In this experiment, we will use the micro:bit board to detect light intensity. Since the micro:bit board does not contains its own photoresistor, the LED dot matrix will shoulder this job. The light signal will convert into input, and the voltage decay time is sampled so that the detected light intensity is a relative value.

(Note: Output the light brightness level to the serial port, and the output is a relative value.)

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code

Find code blocks:

① write Name: value and a line-break(\r\n) on the serial monitor in pair.

j502

It is common to use Name value pairs when reporting or recording data. They are displayed together as a text output string in the form of Name and value. The Name and value are separated in the string by a colon”:”. A Name value pair that reports a temperature of -15 degrees looks like:

Temperature: -15

② set the USB used to serial input and output.

j503

③ ambient temperature. Temperature is measured in degrees Celsius (metric). The micro:bit can find ambient temperatures by the temperature of its computer chip.

j504

④ run code according to whether the Boolean condition is true or false. If you want to run other code when the opposite condition is true, you can put them in “else”.

⑤ comparison operator, comparing the left and right value, and the comparison is Boolean true or false.

j801

⑥ show an icon on the LED matrix.

j609

line1

Build code blocks:

j802

Test Result

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board. Click “Show console Device”.

j803

Cover the LED dot matrix with your hand, so the light intensity is approximately 0; when the LED dot matrix is exposed to light, the light intensity gets stronger with the light as shown below:

j804

20 in the code is an arbitrary value of light intensity. If the current light value is less than or equal to 20, the icon moon will appear on the LED dot matrix. If it’s bigger than 20, the sun will appear.

If you’re running Windows 7 or 8 instead of Windows 10, Google Chrome won’t be able to match devices. so you’ll need CoolTerm.

Open CoolTerm and click Options to select SerialPort, set COM port and put baud rate to 115200 (after testing, the baud rate of USB SerialPort communication on Micro: Bit main board is 115200), and then click OK and Connect.

j508

CoolTerm serial monitor shows the light intensity values.

j805

4bottom

Project 9: Speaker

j901

Introduction

3top

Micro: Bit board boasts an built-in speaker, which makes sound to the programs easier. It is also able to make sound such as utter giggles, greetings and yawning as well as all kinds of tones, notes and beats. For example, play the song Ode to Joy.

You can also turn off the built-in speaker to enjoy the beautiful music via headphones connected to GND and P0. In MakeCode, you need to turn off the speaker by “Turn off built-in speakers” block.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code 1

Find code blocks:

① show an icon on the LED matrix.

j102

Choose the music icon.

j902

② a pause of the program, whose unit is microseconds. It can be used to slow down the execution speed of the program.

j204

③ play sounds.

j903

line1

Build code blocks:

j904

Test Result 1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading the code powering on, the micro:bit speaker makes sounds and the LED matrix shows a music icon.

4bottom

Test Code 2

Find code blocks:

① Display the selected icon on the LED matrix.

j102

Choose the music icon.

j902

② a pause of the program, whose unit is microseconds. It can be used to slow down the execution speed of the program.

j204

③ play tones.

j905

line1

Build code blocks (because this code is too long and repetitive, only part of it is shown here):

j906

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading the code powering on, the micro:bit speaker plays Ode to Joy.

Ode to Joy:

j907

For more information about musical notations: https://en.wikipedia.org/wiki/Numbered_musical_notation

4bottom

Project 11: Microphone

j1101

j1102

Introduction

3top

The micro:bit V2 board is built with a microphone which detects sounds and audio signals. The microphone is placed on the top of the board (the small hole, which is used for picking up ambient sound signals, and an LED indicator is next to the hole). The chip that controls and handles the microphone is located on the back of the board.

When using, simply place the micro:bit board face up.

j1113

When the board detects sound, the LED indicator lights up.

j1112

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code 1

Find code blocks:

① when the microphone detects sounds, execute the code.

j1103

② show an icon on the LED matrix.

j102

line1

Build code blocks:

j1104

Test Result 1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and powering the board via the USB cable, the LED dot matrix displays a music icon and the speaker plays sounds.

4bottom

Test Code 2

Find code blocks:

① set the USB used to serial input and output.

j503

② Create a variable.

j1105

③ check whether a button is pressed. There are two buttons on the micro:bit board: button A and B.

j1106

④ sound level detected by the microphone.

j1107

⑤ show a bar graph of the value.

Bar graph displays numbers as lines of varying lengths. The value is plotted on the LED matrix as the ratio of the value to the height value of the set maximum range. Therefore, if there are 25 LEDs on the matrix, number 9 with a height value of 50 will display about 5 LEDs on the matrix.

j408

⑥ the condition can be true or false to determine whether to run the code.

Code blocks in “if” only run when the condition is true. A true condition can be obtained from the comparison of a variable to a value or to a variable.

⑦ run code according to whether the Boolean condition is true or false. If you want to run other code when the opposite condition is true, you can put them in “else”.

⑧ comparison operator, comparing the left and right value, and the comparison is Boolean true or false.

j1108

⑨ show a number on the LED matrix. If it is a number more than one bit, it displays content that scroll into view.

j604

line1

Build code blocks:

j1109

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board. Click “Show console Device”.

j1110

The louder the sound is, the greater the sound value will show on the serial monitor:

j1111

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.

When the sound becomes louder, the number of lighting LED will also increase.

4bottom

Project 12: Play Music

Introduction

3top

In the previous projects, we have learned about the touch-sensitive logo and the speaker respectively.

In the project, we will combine these two components to play music. We will apply the Logo to control the speaker to sing songs.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code

Find code blocks:

① play music.

j1201

Choose the melody of birthday.

j1202

② show an icon on the LED matrix.

j102

Choose the music icon.

j1203

③ when the micro:bit logo is pressed, touched or released, execute the code.

j1002

line1

Build code blocks:

j1204

Test Result

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and powering the board via the USB cable, the speaker plays Birthday Song and the matrix shows a music icon when the logo is touched.

4bottom

Project 13: Dodge Bullets

Introduction

3top

We have learned about the two programmable buttons: button A and B. In this project, we will combine them with LED dot matrix to design a game: Dodge Bullets.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Test Code 1

Find code blocks:

①Create a variable.

j1301

② enable a program (when something happens, for instance, when a button is pressed). When button A or/and B is pressed, run the program.

When you enable this function in browser, please click the buttons on your screen rather than on the micro:bit board.

  • button A or B: press and release within 1s to enable the function.

  • button A and B: press A and B, release one of them within 1.5s after the second button is pressed to enable the function.

j402

③ create a new LED Sprite pointing to the right. A Sprite is an LED object that takes your instructions to move or turn as well as check whether it comes across another Sprite.

x: The left to right position on the LED matrix where the Sprite will start. y: The top to bottom position on the LED matrix where the Sprite will start. 0 and 4 represent the edge of the matrix, and 2 means the middle position.

④ delete a Sprite.

⑤ move the Sprite according to the number of LEDs you set.

⑥ turn the Sprite in the direction you set.

⑦ check whether the Sprite comes across another Sprite. If they share a same LED, they touch each other.

j1302

⑧ end the game and display the score.

j1303

⑨ basic arithmetic operations: multiplication.

⑩ return a random value within a certain range.

j1304

⑪ a pause of the program, whose unit is microseconds. It can be used to slow down the execution speed of the program.

j204

⑫ run codes as many times as you set.

j1305

⑬ the condition can be true or false to determine whether to run the code.

Code blocks in “if” only run when the condition is true. A true condition can be obtained from the comparison of a variable to a value or to a variable.

j1306

⑭ logic operator. Output true when only one of the conditions is true

If one/both of the conditions is/are true, it outputs true. Only both are false, it outputs false.

j1307

line1

Build code blocks:

j1308

Test Result 1

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

The game begins when the code is uploaded to the main board. The bullets fall off and we need to control the role G by Button A and B to shun them. If the role fail to avert the attacks, game is over.

4bottom

Test Code 2

Find code blocks:

The code blocks of Test Code 2 are similar to that of Test Code1, so we only explain the differences.

① pause the game rendering engine to allow other animations on the screen.

j1309

② show a number on the LED matrix. If it is a number more than one bit, it displays content that scroll into view.

j604

line1

Build code blocks:

j1310

Test Result 2

4top

For Windows 10 App, just click download. For browser, send the “hex” file to the micro:bit board.

After uploading test code to micro:bit main board and keeping the connection with the computer to power the main board.

The game begins when the code is uploaded to the main board. The bullets fall off and we control the role G by Button A and B to shun them. 1 score will be tallied for each successful dodging. If the role fail to avert the attacks, the game is over and gained scores displays.

4bottom

Project 14: Bluetooth

j1401

Introduction

3top

The Micro:bit main board comes with a nRF52833 processor with a built-in 5.1 BLE(Bluetooth Low Energy) device and a 2.4GHz antenna for Bluetooth wireless communication, so that the board is able to communicate with a variety of Bluetooth devices, including smart phones and tablets.

In this project, we mainly concentrate on the Bluetooth wireless communication to transmit code or signals. Firstly, we should connect a device (a phone or an iPad) to the board. Since setting up Android phones to achieve wireless transmission is similar to that of Apple devices, no need to illustrate again.

3bottom

Components

Name

Picture

Micro:bit mainboard*1

microbitV2

Micro USB cable*1

usb

Connection

Connect the board to your computer via micro USB cable.

microbit&usb

Procedures

We will demonstrate on Apple system devices. Android devices may take these as a reference.

  1. connect micro:bit V2 board to computer via micro USB cable.

  2. \Visit the web page on your computer: https://www.microbit.org/get-started/user-guide/ble-ios/

    Download the corresponding micro:bit firmware based on your micro:bit board version.

j1402

Or find the firmware we provided in the file resources: firmware

upload the downloaded firmware to the Micro: Bit main board.

j1403

j1404

  1. Open App Store j1405 and search “micro:bit”.

j1406

Click j1407 to download the app.

  1. Connect your Apple device with Micro: Bit main board.

    (1) ensure the board is connected to the device and select “Choose micro:bit”to start pairing Bluetooth.

    (2) click j1408 to open the APP and click “ Manage Connections ” to start pairing.

    j1409

    (3) tap “ Pair a micro:bit ” .

    j1410

    (4) press RESET for three times as prompted.

    j1411

    “ Show another way ” to try another way of pairing.

    j1412

    Press button A and B at the same time, and press and release the RESET button.

    j1413

    (5) LED matrix lights up from the first line till all lighting up.

    j1414

    LED matrix flashes the following icon once.

    j1415

    LED matrix shows the icon for about 5s, and click “ Next ” to enter the icon password.

    j1416

    j1417

    Click “ Next ”. Note that the password is an icon.

    j1418

    (6) when a request show up, click “ Pair ”.

    j1419

    (7) pairing successful. click “ OK ”.

    j1420

    (8) after pairing, click “ Home ”.

    j1421

    (9) “ Create Code ” via APP and upload the code.

    j1422

    (10) create a new project.

    j1423

    (11) name to “ Heart ”

    j1424

    (12) build code blocks and save. Back to main page.

    j1425

    (13) click “ My Programs ”

    j1426

    (14) the default program is the “Heart”, and click “ Send to micro:bit ” to upload the code.

    j1427

    j1428

    (15) After uploading, click “ok” and the LD matrix shows a beating heart.

    j1429