Makecode Tutorial
Get Started with Makecode
The following instructions are applied for Windows system but can also serve as a reference if you are using a different system.
Write code and program
This chapter describes how to write program and load it to the Micro: Bit main board V2.
Please browse the link for more details: https://microbit.org/guide/quick/
Step 1: Connect the Micro: Bit
Firstly, link the Micro: Bit main board with your computer via the USB cable. Macs, PCs, Chromebooks and Linux(including Raspberry Pi)systems are all compatible with the Micro: Bit main board.
Note that if you are about to pair the board with your phone or tablet, please refer to this link: https:/microbit.org/get-started/user-guide/mobile/
Secondly, if the red LED on the back of the board is on, that means the board is powered. When your computer communicates with the main board via the USB cable, the yellow LED on it will flashes. For example, it will flicker when you burn a “.hex”file.
Then Micro: bit main board will display a driver named “MICROBIT(E:)”on your computer. Please note that it is not an ordinary USB disk, as shown below.
Step 2: Write programs
Access the link:https://makecode.microbit.org/,then click “New Project”, when the dialog box‘Create a Project’ appears, input‘heartbeat’in it and click ‘Create √’to edit.
(If you are running Windows 10 system, it is also viable to edit on the Windows 10 APP, which is exactly like editing in the website.Link of the Windows 10 APP: Windows 10 APP)
Write a set of micro:bit code. You can drag some blocks from the modules to the editing area and then run your program in Simulator of MakeCode editor, as shown in the picture below, which demonstrates how to edit ‘heartbeat’ program .
Here’s a demo video(path):
...\Important Resources\Makecode Code\Project 1:Heartbeat\heartbeat.mp4
.
Click “JS JavaScript”,then we enable to find the corresponding program in JavaScript language, as shown below:
You also can click the“JS JavaScript”and slide the button to choose“Python”and you will find the corresponding as shown below:
Step 3: Download Code
If your computer is Windows 10, you solely need to click the ‘Download’ button to download the program to your micro: bit main board.
If you are writing program through the website, following these steps:
Click the ‘Download’ button in the editor to download a “.hex” file, which can be read by the Micro: Bit main board. Once the hexadecimal file is downloaded, copy it to your board just like the process that you copy the file to the USB driver. If you are running Windows system, you can also right-click and select ‘Send to → MICROBIT(E:)’ to copy the hex file to the Micro: Bit main board.
You can also directly drag the “.hex” file onto the MICROBIT (E:) disk.
During the process of copying the downloaded hex file to the Micro: bit main board, the yellow signal light on the back side of the board flashes. When the copy is completed, the yellow signal light will stop flashing and remain on.
Step 4: Run the program
After the program is uploaded to the Micro: bit main board, you could still power it via an USB cable or an external power. The 5 x 5 LED dot matrix on the board displays the heartbeat pattern.
Power via USB cable
Power via external power(3V)
Caution:
When programming, the MICROBIT driver will automatically eject and return and your hexadecimal files will disappear. And the board can only have access to hexadecimal files (.hex) and won’t save other files.
Step5:Other programming languages
Go to the link: :https://microbit.org/code/ for different programming languages .
Makecode
Access the link:https://makecode.microbit.org/,and enter the online editor of makecode or open the APP makecode for micro:bit Windows 10.
Click“New Project”, and input“heartbeat”in the dialog box, then click “create √”to enter Makecode editor, as shown below:
There are blocks“on start”and“forever”in the code editing area.
When the power is plugged or reset,“on start”means that the code in the block only executes once, while“forever”implies that the code runs cyclically.
Quick Download
As mentioned before, if your computer is Windows 10 and you have downloaded the MakeCode APP for micro:bit, then the code can be quickly downloaded to the Micro: Bit main board by clicking‘Download’button.
While it is a little more trickier if you are using a browser to enter Makecode. However, if you use Google Chrome for Android,ChromeOS,Linux,macOS and Windows 10, the process can be quicker too.
We use the webUSB function of Chrome to access the micro USB hardware device.
You could refer to the following steps to connect and pair devices.
Device pairing:
Connect micro:bit to your computer by USB cable.
Click“…”beside to“Download”and tap“Connect device”;
Click“Next”;
Click“Next”again
Then select the corresponding device and click“Connect”. If no device shows up for selection, please refer to the link: https://makecode.microbit.org/device/usb/webusb/troubleshoot
And for updating the firmware of the Micro:bit: https://microbit.org/guide/firmware/ .
Click“Done”to finish the pairing.
Download Program:
After pairing, click “download”to directly download the program to the
board. If it is successfully downloaded, the icon will shift
to
.
Makecode Extension Libraries
We have made a makecode extension library for this Mecanum robot car V2.0
Add an extension library of the Mecanum robot car V2.0
Please follow the following steps to add extension files:
Open Makecode to enter a certain project → click the gear-shaped icon(for setting) in the upper right corner → choose“Extensions”;
Or click“Extensions”, as shown below:
Input the link
https://github.com/keyestudio2019/mecanum_robot _v2.git
to search;
Tap the searching result “MecanumRobotV2” to download and install it, This process may take a few seconds.
After the installation, you can find the extension files Mecanum RobotV2 and IrRemote on the left side.
And extension file Neopixel is also installed.
Note: The extension files added are only available for this project. Therefore, when you create a new MecanumRobotV2 project, you will need to add these extension files again.
Update or delete the MecanumRobotV2 extension files:
Please follow the following steps to update or delete extension files:
Click “Js JavaScript” to change to textual version:
Click the “Explorer” on the left side:
You can find these added files in the list; Click the dustbin icon beside the file to delete the MecanumRobotV2 file and tap the refresh icon to update it.
Import test code
We provide hexadecimal code files (project files) for each project. The file contains all the contents of the project and can be imported directly, and you can manually drag the code blocks to complete the program.
For simple projects, dragging a block of code to complete the program is recommended. For complex projects, it is recommended to conduct the program by importing the provided hexadecimal code file.
Let’s take the “Heatbeat” project as an example to show how to load the code.
Open the Web version of Makecode or the Windows 10 App version of Makecode then click “Import”.
Click“Import File”;
Select
..\Important Resources\Makecode Code\Project 1:Heartbeat\Project 1: Heart beat.hex
, then click“Go ahead”.
In addition to importing the provided test code file into the Makecode compiler above, you can also drag them to the code editing area of the Makecode compiler, as shown in the figure below:
After a few seconds, it has done.
Note: If your computer system isn’t Windows 10, the pairing cannot be done via Google Chrome. Therefore, digital or analog signals of sensors and modules cannot be read. However, we can use the CoolTerm software to read the serial port data. Next chapter is about how to install the CoolTerm.
Install CoolTerm
CoolTerm program is used to read the data on serial port.
Download CoolTerm program: https://freeware.the-meiers.org/
After downloading, we need to install CoolTerm program file, and we take PC Window system as an example.
Choose “win” to download the zip file of CoolTerm.
Unzip the file and open it. (also suitable for Mac and Linux system)
3. Double-click.(Please make sure that the driver of
Micro:bit is installed and the main board is connected to the computer.)
The functions of each button on the Toolbar are listed below:
Opens up a new Terminal |
|
Opens a saved Connection |
|
Saves the current Connection to disk |
|
Opens the Serial Connection |
|
Closes the Serial Connection |
|
Clears the Received Data |
|
Opens the Connection Options Dialog |
|
Displays the Terminal Data in Hexadecimal Format |
|
Displays the Help Window |
Projects
(Note: project 1 to 12 are basic courses conducted with the built-in sensors and LED dot matrix of the Micro:bit main board V2, while project 13 to 20 are extended courses)
Project 1:Heart beat
Description
This project is easy to conduct solely with a micro:bit main board, a micro USB cable and a computer. The micro:bit will display a “flickering heart” pattern. This experiment serves as a starter for your to entry to the magical world of the micro:bit.
Preparation
Attach the micro:bit main board to your computer via the USB cable
B. Open the Web version of Makecode and Click “New Project” into the code editing interface of Makecode.
Test Code
Click“JavaScript” to view the corresponding JavaScript code:
Test Result
Download code to micro:bit and keep the USB cable connected. The LED dot
matrix will display ❤ and ceaselessly.
If the download is not success, try to disconnect the micro:bit from your computer and then reconnect them and reopen Makecode to try again.
Project 2:Light A Single LED
Description
The LED dot matrix consists of 25 diodes arranged in a 5 by 5 square and placed at the intersection of row lines (X) and column lines (Y). We can control one of the 25 LEDs by setting coordinate points. For example, the first LED sits in the first line is (0,0)and the third LED positioned in the first line is (2,0)and others likewise.
Preparation
Attach the micro:bit main board to your computer via the USB cable
B. Open the Web version of Makecode and Click “New Project” into the code editing interface of Makecode.
Test Code
Click“JavaScript”to switch into corresponding JavaScript code:
Test Result
After uploading the test code to micro:bit main board V2 and powering it via the USB cable, the LED in (1,0) lights up for 0.5s and the one in (3,4) shines for 0.5s and repeat this sequence.
Project 3:5 x 5 LED Dot Matrix
Description
Dot matrix is very commonplace in daily life, which has 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 Diodes. Previously, we have succeeded in controlling a certain LED via its position point. Supported by the same theory, we can turn on many LEDs at the same time to showcase patterns, digits and characters.
What’s more, we can also click”show icon“ to choose the pattern we like to display. Last but not the least, we can design patterns by ourselves as well.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code1
Select“JavaScript”to switch into JavaScript language code:
Test Result1
Upload code 1 and power the board , we will see the icon
Test Code 2:
Select“JavaScript”to switch into JavaScript language code:
Test Result
Upload code 2 and plug micro:bit in power, Micro: bit starts showing
number 1, 2, 3, 4, and 5, then cyclically display
,“Hello!”,
,
,
,
and
patterns.
Project 5:Temperature Measurement
Description
The Micro:bit main board is not equipped with a temperature sensor, but uses the built-in temperature sensor in NFR52833 chip for temperature detection. Therefore, the detected temperature is more closer to the temperature of the chip, and there maybe deviation from the ambient temperature.
In this project, we will seek to use the sensor to test the temperature in the current environment, and display the test results in the display data (device). And then control the LED dot matrix to display different patterns by setting the temperature range detected by the sensor.
Note: the temperature sensor of Micro:bit main board is shown below:
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code1
Micro:bit detects temperature
Click“JavaScript”to view the corresponding JavaScript code:
Test Result1
Download code 1 to micro:bit board and keep USB cable connected, then
tap the button :
Temperature data is shown below:
Through the test, the room temperature is 35℃when touching the NFR51822 chip of micro:bit; however, the temperature rises to 37℃ when it touches water cup.
If you’re running Windows 7 or 8 instead of Windows 10, it won’t be able to match devices via Google Chrome. You’ll need to use the CoolTerm serial monitor software to read data.
You could open CoolTerm, click Options to select SerialPort. Set COM port and 115200 baud rate(the baud rate of USB serial communication of Micro:bit is 115200 through the test). Click“OK”and“Connect”.
The serial monitor shows the current ambient temperature value, as shown below:
Test Code2
Micro:bit display different pictures by temperature(the temperature value in the code could be adjusted).
Click“JavaScript”, the corresponding JavaScript code is shown below:
Test Result2
Upload the Code 2 and plug in power. And the 5*5 LED displays the
ambient temperature. When pressing the temperature sensor, the
temperature will grow on dot matrix. When the ambient temperature is
less than 35℃, the 5*5LED will show. When the temperature is
equivalent to or greater than 35℃, the pattern
will appear.
Project 6:Geomagnetic Sensor
Description
This project mainly introduces the use of the micro:bit’s geomagnetic sensor. In addition to detecting the strength of the magnetic field, it can also be used to determine the direction, which is an important part of the heading and attitude reference system (AHRS) as well.
It uses FreescaleMAG3110 three-axis magnetometer. Its I2C interface communicates with the outside, and the range is ±1000µT, the maximum data update rate is 80Hz. Combined with an accelerometer, it can calculate the position. Additionally, it is applied to magnetic detection and compass blocks.
Then we could read the value detected by it to determine the location. We need to calibrate the micro:bit board when the magnetic sensor works. The correct calibration method is to rotate the micro:bit board.
In addition, the objects nearby may affect the accuracy of readings and calibration.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code1
Press A on micro:bit, the value of compass is shown.
Select“JavaScript”to switch into JavaScript language code:
Test Result1
Upload the code 1, plug in micro:bit via an USB cable.
After uploading the test code1 to the micro:bit main board and powering the board via an USB cable, and the LED dot matrix shows “TILT TO FILL SCREEN”. Pressing the button A, the board asks us to calibrate the compass. Then enter the calibration page. Rotate the board until all 25 red LEDs are on, as shown below.
The calibration will be finished until you view the smile
patternappear.
The serial monitor will show 0°, 90°, 180° and 270° when pressing A.
Test Code2
This module can keep reading data to determine direction, and let arrow point to the current magnetic North Pole.
For the above picture, the arrow will point to the upper right when the value ranges from 292.5 to 337.5. Because 0.5 can’t be input in the code, the values we get are 293 and 338.
Make micro: bit board point to the north, south, east and west horizontally , LED dot matrix displays the corresponding direction patterns
Select“JavaScript”to switch into JavaScript language code:
Test Result2
Upload code 2 and plug micro:bit into power. After calibration, tilt the micro:bit board, and the LED dot matrix displays the direction signs.
Project 7:Accelerometer
Description
The micro:bit board has a built-in Freescale MMA8653FC three-axis acceleration sensor (accelerometer). Its I2C interface works on external communication, the range can be set to ±2g, ±4g, and ±8g, and the maximum data update rate can reach 800Hz.
When the Micro:bit is stationary or moving at a constant speed, the accelerometer only detects the gravitational acceleration; when the Micro:bit is slightly shaken, the acceleration detected is much smaller than the gravitational acceleration and can be ignored. Therefore, in the process of using Micro:bit, the main purpose is to detect the changes of the gravitational acceleration on the x, y, and z axes when the attitude changes.
For this project, we will introduce the detection of several special postures by the accelerometer.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code1
Click“JavaScript”, you will view the corresponding JavaScript code:
Test Result1
After uploading the test code 1 to micro:bit main board V2 and powering the board via the USB cable, if we shake the Micro: Bit main board V2, no matter at any direction, the LED dot matrix displays the digit “1”.
When it is kept upright (make its logo above the LED dot matrix), the number 2 shows.
When it is kept upside down( make its logo below the LED dot matrix) , it shows as below.
When it is placed still on the desk, showing its front side, the number 4 appears.
When it is placed still on the desk, showing its back side, the number 5 exhibits.
When the board is tilted to the left , the LED dot matrix shows the number 6, as shown below.
When the board is tilted to the right , the LED dot matrix displays the number 7, as shown below:
When the board is knocked to the floor, this process can be considered as a free fall and the LED dot matrix shows the number 8. (Please note that this test is not recommended for it may damage the main board.)
Attention: if you’d like to try this function, you can also set the acceleration to 3g, 6g or 8g.
Test Code2
Detect the value of acceleration speed at x, y and z axis
Click“JavaScript” to view the corresponding JavaScript code:
Test Result2
Download code 2 to micro:bit board, keep USB cable connected and click
:
After referring to the MMA8653FC data manual and the hardware schematic diagram of the Micro: Bit main board V2, the accelerometer coordinate of the Micro: Bit V2 motherboard are shown in the figure below:
The following interface shows the decomposition value of acceleration in X axis, Y axis and Z axis respectively, as well as acceleration synthesis (acceleration synthesis of gravity and other external forces).
If you’re running Windows 7 or 8 instead of Windows 10, it won’t be able to match devices via Google Chrome. You’ll need to use the CoolTerm serial monitor software to read data.
You could open CoolTerm software, click Options, 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 V2 is 115200), click OK, and Connect. The CoolTerm serial monitor shows the data of X axis, Y axis and Z axis , as shown in the figures below :
Project 8:Light Detection
Description
In this project, we focus on the light detection function of the Micro:Bit main board V2. It is achieved by the LED dot matrix since the main board is not equipped with a photoresistor.
When the light irradiates the LED matrix, the voltage change will be produced. Therefore, we could determine the light intensity by voltage change.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code
Click“JavaScript”to switch into the corresponding JavaScript code:
Test Result
Download code to micro:bit board don’t plug off the USB cable and click
:
The intensity value is 0 when covering the LED dot matrix. And the value varies with the light intensity. When placing micro:bit under the sunlight, the stronger the light is, the larger the intensity value will be. As shown below:
If you’re running Windows 7 or 8 instead of Windows 10, it won’t be able to match devices via Google Chrome. You’ll need to use the CoolTerm serial monitor software to read data.
You could open“CoolTerm”, click“Options”to select “SerialPort”, and set “COM” port and 115200 baud rate(the baud rate of USB serial communication of micro:bit is 115200 through the test).
Then click“OK”and“Connect”. The light intensity value is shown below:
Project 9:Speaker
Description
Micro: Bit main board has an built-in speaker, which makes adding sound to the programs easier. It can also be programmed to produce all kinds of tones, like playing the song Ode to Joy.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code
Select “JavaScript” to switch into JavaScript language code:
Test Result
After uploading the test code to micro:bit main board V2 and powering the board via the USB cable, the speaker utters sound and the LED dot matrix shows the logo of music.
Project 10:Touch-sensitive Logo
Description
The Micro: Bit main board V2 is equipped with a golden touch-sensitive logo, which can act as an input component like an button.
It contains a capacitive touch sensor that senses small changes in the electric field when pressed (or touched), just like your phone or tablet screen. When you press it , the program can be activated.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code
Select“JavaScript” and“Python”to switch into JavaScript and Python language code:
Test Result
via the USB cable, the LED dot matrix exhibits the“❤” pattern when the touch-sensitive logo is pressed or touched and displays digit when the logo is released.
Project 11:Microphone
Description
The Micro: Bit main board has a built-in microphone, which can test the volume of ambient environment. When you clap, the microphone LED indicator turns on. Furthermore, it can measure the intensity of sound, thereby you can make a noise scale or disco lighting changing with music.
The microphone is placed on the opposite side of the microphone LED indicator and in proximity with holes that lets sound pass. When the board detects the sound, the LED indicator lights up.
Preparation
Attach the micro:bit main board to your computer via the USB cable
Open the Web version of Makecode.
Test Code1
Select“JavaScript”to switch into JavaScript language code:
Test Results 1
After uploading test code1 to micro:bit main board V2 and powering the
board via the USB cable, the LED dot matrix displays pattern“❤””when you
claps and patternwhen it is quiet around.
Test Code2
Select“JavaScript”to switch into JavaScript language code:
Test Results 2
Upload test code 2 to micro:bit main board V2, power the board via the USB cable and click “Show console Device”as shown below.
When the sound is louder around, the sound value shows in the serial port is bigger as shown below.
What’s more, when pressing the button A, the LED dot matrix displays the value of the biggest volume( please note that the biggest volume can be reset via the Reset button on the other side of the board ) while when clapping, the LED dot matrix shows the pattern of the sound.
Project 12:Bluetooth Wireless Communication
Description
The Micro: Bit main board V2 comes with a nRF52833 processor (with a built-in BLE(Bluetooth Low Energy) device Bluetooth 5.1 ) and a 2.4GHz antenna for Bluetooth wireless communication and 2.4GHz wireless communication. With the help of them, 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 functions of this main board. Linked with Bluetooth, it can transmit code or signals. To this end, we should connect an Apple 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, so we don’t need to illustrate again.
Preparation
Attach the micro:bit main board to your computer via the USB cable
IPhone device (phone /iPad) or Android phone.
Procedures
For Apple devices, enter this link: https://www.microbit.org/get-started/user-guide/ble-ios/ with your computer first, and then click “Download pairing HEX file”to download the Micro: Bit firmware to a folder or desk, and upload the downloaded firmware to the Micro: Bit main board V2.
Opento search “micro bit”in your App Store to download the
APP micro:bit then click“
”.
Connect your Apple device with Micro: Bit main board V2:
Firstly, turn on the Bluetooth of your Apple device and open the APP micro:bit to select item “Choose micro:bit”to start pairing Bluetooth.
Please make sure that the Micro: Bit main board V2 and your computer are still linked via the USB cable.
Secondly, click“Pair a new micro:bit”;
Following the instructions to press button A and B at the same time(do not release them until you are told to) and press Reset & Power button for a few seconds.
Release the Reset & Power button, you will see a password pattern shows on the LED dot matrix. Now , release buttons A and B and click Next.
Set the password pattern on your Apple device as the same pattern showed on the matrix and click Next.
Still click Next and a dialog box props up as shown below. Then click “Pair”. A few seconds later, the match is done and the LED dot matrix displays the “√” pattern.
After the match with Bluetooth, write and upload code with the App.
Click “Create Code” to enter the programming page and write code.
Clickand the box
appears, and then select
“Create √”.
Name the code as “1 “and click to save it.
Click the third item“Flash”to enter the uploading page. The default code program for uploading is the one saved just now and named “1” and then click the other “Flash” to upload the code program “1”.
If the code is uploaded successfully a few seconds later, the App will emerge as below and the LED dot matrix of the Micro: Bit main board V2 will exhibit a heart pattern.
Projects above all conduct with the built-in sensors and the LED dot matrix of the main board while the following ones will carry out with the help of external sensors of this turtle car.(Attention:In order to avoid burning the the Micro:bit main board V2, please remove the USB cable and the external power from the board before fix it with the shield of the car; likewise, the USB cable and the external power should be cut from the main board before disconnect the shield from the board.)
Project 13:Seven-Color LED
Description
This module consists of a commonly used LED with 7colors but in white appearance. It can automatically flash different colors to create fantastic light effects when high level is input like a normal LED.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode.
Test Code1
Make the RGB light flash 7 lights alternatively.
Click“JavaScript”to view the corresponding JavaScript code:
Test Result1
Download code 1 to micro:bit board and dial POWER switch to ON end, 2 RGB lights of smart car emit red, green, blue, indigo, dark red, yellow and white color cyclically.
Test Code2
Click“JavaScript”to view the corresponding JavaScript code:
Test Result2
Download code 2 to micro:bit board, 2 RGB lights will flash for 1 second and then stop flashing for 1 second, cyclically.
Project 14:4 WS2812 RGB LEDs
Description
The driver shield cooperates 4 pcs WS2812 RGB LEDs, compatible with micro:bit board and controlled by P7. In this lesson, we will make the RGB LEDs display different colors by P7. In this lesson, 3 sets of test code are provided to make the 4 WS2812 RGB LEDs display different effects.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode.
Test Code1
Click“JavaScript” to switch into the corresponding JavaScript code:
Test Result1
Download code 1 to micro:bit, and dial POWER to ON end. All four WS2812RGB LEDs light up a different color a time cyclically.
Test Code2
Click“JavaScript” to switch into the corresponding JavaScript code:
Test Result2
Download code 2 to micro:bit, WS2812RGB LEDs display like flow light.
Test Code3
Click“JavaScript”to switch into the corresponding JavaScript code:
Test Result3
Download code 3 to micro:bit, every WS2812RGB light shows random color one by one.
Project 15:Servo
Description
For those DIY smart cars, they often have the function of automatic obstacle avoidance. In the DIY process, we need to use a servo to control the ultrasonic module to rotate left and right, and then detect the distance between the car and the obstacle, so as to control the car to avoid the obstacle. If other microcontrollers are used to control the rotation of the servo, we need to set a certain frequency and a certain width of pulse to control the servo angle.
However, if the micro:bit main board is used to control the servo angle, we only need to set the control angle in the development environment where the corresponding pulse will be automatically set to control the servo rotation. In this project, you will learn how to control the servo to rotate back and forth between 0° and 90°.
Information of the Servo
Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor. Its working principle is that the servo receives the signal sent by MCU or receiver, and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtains the voltage difference output.
For the servo used in this project, the brown wire is the ground, the red one is the positive wire, and the orange one is the signal wire.
The rotation angle of servo motor is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it’s between 0.5ms-2.5ms. The width corresponds to the rotation angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotation angle.
More details:
Parameters
Working voltage: DC 4.8V ~ 6V
Operating angle range: about 180 ° (at 500 → 2500 μsec)
Pulse width range: 500 → 2500 μsec
No-load speed: 0.12 ± 0.01 sec / 60 (DC 4.8V) 0.1 ± 0.01 sec / 60 (DC 6V)
No-load current: 200 ± 20mA (DC 4.8V) 220 ± 20mA (DC 6V)
Stopping torque: 1.3 ± 0.01kg · cm (DC 4.8V) 1.5 ± 0.1kg · cm (DC 6V)
Stop current: ≦ 850mA (DC 4.8V) ≦ 1000mA (DC 6V)
Standby current: 3 ± 1mA (DC 4.8V) 4 ± 1mA (DC 6V)
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Test Code
Click“JavaScript” to view the corresponding JavaScript code:
Test Result
After uploading the test code and dial POWER switch to ON end, the servo rotates from 0 degree to 180 degrees.
Project 16:Motor
Description
The Keyestudio 4WD Mecanum Robot Car is equipped with 4 DC reduction motors, also called gear reduction motor, which is developed on the ordinary DC motor. It has a matching gear reduction box which provides a lower speed but a larger torque. Furthermore, different reduction ratios of the box can provide different speeds and torques.
Gear motor is the integration of gearmotor and motor, which is applied widely in steel and machine industry
Micro:bit motor driver shield comes with a DRV8833 chip. In order to save the IO port resource, we control the rotation direction and speed of 4 DC gear motors with the DRV8833 chip.
Front
Back
STC8G1K08 Chip circuit
HR8833 Motor driver circuit
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Test Code1
Click“JavaScript” to view the corresponding JavaScript code:
Test Result1
Download code 1 to micro:bit board, dial POWER switch to ON end. Smart car goes forward for 2s and stops for 2s.
Test Code2
Click“JavaScript” to view the corresponding JavaScript code:
Test Result2
Download code 2 to micro:bit board, the car goes forward for 2s, turns back for 2s, turn left for 2s, turn right for 2s and stops for 2s and repeats this pattern.
Project 17:Line Tracking Sensor
Project 17.1:Detect Line Tracking Sensor
Description
The motor driver board of the Keyestudio 4WD Mecanum Robot Car comes with a 3-channel line tracking sensor, which adopts TCRT5000 IR tubes and 3 potentiometers.
The TCRT5000 IR tube contains an IR emitting tube and an IR receiving tube. When the infrared signals of the emitting tube is received by the receiving tube through reflection, the resistance of the receiving tube will change, which is generally reflected in the voltage change on the circuit.
The resistance varies depending on the intensity of the infrared signals received by the receiving tube, which is often in the color of the reflecting surface and the distance of the reflecting surface receiving tube. At the time of detection, black is high level active and white is low level active.
Working Principle
When the car runs above a white road, the IR emitting tube installed under the car emits infrared signals to detect the road and the receiving tube will receive signals sending back. Then the output end outputs low level(0); when it detects black lines, it outputs high level(1).
After putting a white paper on the bottom of the 4WD Mecanum Robot Car, we will rotate the potentiometers on the 3-way tracking sensor. When the indicator light on the sensor module is on, pick up the car to make the two wheels on the 4WD Mecanum Robot Car separate. The height of the white paper is about 1.5cm, when the indicator light on the sensor module is off, and then the sensitivity is adjusted.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Test Code
Click“JavaScript” to view the corresponding JavaScript code:
Test Result
Download code to micro:bit board, dial POWER switch to ON end.
Open CoolTerm, click Options to select SerialPort. Set COM port and 115200 baud rate. Click“OK”and“Connect”.
The CoolTerm serial monitor displays the digital signals read by the line tracking sensors.
Project 17.2:Tracking Smart Car
Description
In this lesson we will combine a line tracking sensor with a motor to make a line tracking smart car.
The micro:bit board will analyze the signals and control the smart car to show the line tracking function.
Working Principle
The smart car will make different moves according to the value received by the 3-channel line tracking sensor.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Warning: The 3-way tracking sensor should be used in environments without infrared interference such as sunlight. Sunlight contains a lot of invisible light, such as infrared and ultraviolet. In an environment with strong sunlight, the 3-way tracking sensor cannot work properly.
4.Flow Chart
Test Code
Click“JavaScript”to view the corresponding JavaScript code:
Test Result
Download code to micro:bit and dial POWER to ON end, line tacking car goes forward along black line .
Note: turn on the switch at the back of micro:bit car, the width of black line should be larger than the width of line tracking sensor.
Avoid to test smart car under the strong light.
Project 18:Ultrasonic Sensor
Project 18.1:Ultrasonic Ranging
Description
The ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with ultrasonic transmitter and receiver modules.
The ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications.
The ultrasonic module will emit the ultrasonic waves after trigger signals. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of object from the time difference between trigger signal (TRIG)and echo signal(ECHO).
As the picture shows, it is like two eyes. One is transmitting end, the other is receiving end.
According to the above wiring diagram, the integrated port of the ultrasonic sensor module is connected to the 5V G P15 P16 port on the micro:bit motor driver base plate. The Trig (T) pin is controlled by P15 of the micro:bit and the pin of Echo (E) the P16.
Working Principle
(1)Pull down TRIG then trigger high level signals with least 10us;
(2)After triggering, the module will automatically send eight 40KHz ultrasonic pulses and detect whether there is a signal return;
(3)If there is a signal return, when ECHO (E) outputs a high level, then the duration of the high level is the time from transmission to reception of the ultrasonic waves. Then test distance = high level duration *340m/s*0.5.
Parameters
Working voltage: 3-5.5V (DC)
Working current: 15mA
Working frequency: 40khz
Maximum detection distance: about 3m
Minimum detection distance: 2-3cm
Precision: up to 0.2cm
Sensing angle: less than 15 degrees
Input trigger pulse: 10us TTL level
Output echo signal: output TTL level signal (high), proportional to range
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Test Code
Click“JavaScriptto view the corresponding JavaScript code:
Test Result
Download code to micro:bit, keep USB cable connected, dial POWER switch to ON end. The distance value will be displayed on monitor.
The monitor shows the distance between the obstacle and ultrasonic sensor(as shown below).
Open CoolTerm, click Options to select SerialPort. Set COM port and 115200 baud rate(the baud rate of USB serial communication of Micro:bit is 115200 through the test). Click “OK” and “Connect”.
CoolTerm serial monitor displays the distance value as follows:
Project 18.2:Ultrasonic Avoidance
Description
In this project, we will integrate an ultrasonic sensor and a car to make an ultrasonic avoidance car.
Its principle is to detect the distance between the car and obstacle via the ultrasonic sensor to control the motion of smart car.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Flow Chart
Test Code
Click“JavaScript”to view the corresponding JavaScript code:
Test Result
Download code to micro:bit, dial to ON end, and dial POWER to ON end. When the obstacle distance is greater than 20cm, the car goes forward ; on the contrary, smart car turns left.
Project 18.3:Ultrasonic Following
Description
In previous lesson, we’ve learned the basic principle of line tracking sensor. Next, we will combine the ultrasonic sensor with the car to make an ultrasonic following car.
The ultrasonic sensor detects the obstacle distance and control the motion status of car.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Flow Chart
Test Code
Click“JavaScript”to view the corresponding JavaScript code:
Test Result
Download code to micro:bit, dial POWER switch to ON end on shield, smart car could follow the obstacle to move.
Project 19:IR Remote Control
Project 19.1:Decode IR Remote Control
Description
There is no doubt that infrared remote control is ubiquitous in daily life. It is used to control various household appliances, such as TVs, stereos, video recorders and satellite signal receivers. Infrared remote control is composed of infrared transmitting and infrared receiving systems, that is, an infrared remote control, an infrared receiving module and a single-chip microcomputer capable of decoding.
The 38K infrared carrier signal emitted by remote controller is encoded by the encoding chip in the remote controller. It is composed of a section of pilot code, user code, user inverse code, data code, and data inverse code. The time interval of the pulse is used to distinguish whether it is a 0 or 1 signal and the encoding is made up of these 0, 1 signals.
The user code of the same remote control is unchanged. The data code can distinguish the key.
When the remote control button is pressed, the remote control sends out an infrared carrier signal. When the IR receiver receives the signal, the program will decode the carrier signal and determines which key is pressed. The MCU decodes the received 01 signal, thereby judging what key is pressed by the remote control.
Infrared receiver we use is an infrared receiver module. Mainly composed of an infrared receiver head, it is a device that integrates reception, amplification, and demodulation. Its internal IC has completed demodulation, and can achieve from infrared reception to output and be compatible with TTL signals. Additionally, it is suitable for infrared remote control and infrared data transmission. The infrared receiving module made by the receiver has only three pins, signal line, VCC and GND.
According to the picture above, the integrated port of the infrared receiver is connected to the P9 5V G port on the motor driver board and controlled by the the P9 of the micro:bit.
Parameters:
Operating Voltage: 3.3-5V(DC)
Interface: 3PIN
Output Signal: Digital signal
Receiving Angle: 90 degrees
Frequency: 38khz
Receiving Distance: about 5m
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Test Code
Click“JavaScript” to switch into the corresponding JavaScript code:
Code explanation: If the buttons are not pressed, the serial monitor constantly shows 0; when pressed, the corresponding key values are displayed.
Notes:
The remote control in this kit is not inclusive of batteries. We recommend you to purchase them online.(battery type:CR2025).
Make sure IR remote is good before test. There is a tip for you to check it.
Open the cellphone camera , make IR remote control point at camera and press button. The remote control is good if you see the purple flashing light in the camera.
Test Result
Download code to micro: bit board and don’t plug off USB cable
Click
Make IR remote control point at IR receiver and press the button, the serial monitor will display the corresponding key values, as shown below:
Open CoolTerm, click Options to select SerialPort. Set COM port and 115200 baud rate. Click“OK”and“Connect”.
CoolTerm serial monitor shows the key value as follows:
The key value is displayed as for your reference:
Project 19.2:IR Remote Control
Description
In this project, we combine IR remote control with car shield to make an IR remote smart car. Its principle is to control the motion of car by sending key signals from IR remote control to IR receiving module of car shield.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Note: The infrared sensor and infrared remote control should not be used in environments with infrared interference such as sunlight for it contains a lot of invisible lights, such as infrared and ultraviolet. In an environment with strong sunlight, they cannot work normally.
Flow Chart
Test Code
Click“JavaScript” to switch into the corresponding JavaScript code:
Test Result
Download code to micro:bit board, and dial POWER to ON end.
Make IR remote control point at micro:bit and press the button to control smart car to move.
button makes smart car move forward,
stands
for turning left,
implies rightward turning,
indicates moving backward,
stops car.
Note: The distance between IR remote control and IR receiving head of smart car are supposed less than 5m during the test.
Project 20:Bluetooth Multi-purpose Smart Car
Project 20.1:Read Bluetooth Data
Description
Micro:bit main board comes with a built-in Bluetooth which can be used to communicate with it. And the Micro:bit can also be controlled by Bluetooth or transmit signals back to smartphone or computer via it. This Bluetooth can communicate with the Bluetooth equipped in other devices or with Bluetooth App to control other equipment.
It is compatible with both Android system ans IOS system. And we have designed two Bluetooth Apps for both systems.
The connection of the Bluetooth on the board with these two Apps is similar. In this lesson, we will introduce the functions of all keys and patterns on the Apps and control the smart car via Bluetooth App.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
If you choose to drag the code manually, you need to add the Bluetooth extension library first. Click the gear icon (Settings) in the upper right corner, then click on Extensions to go to the library file selection screen, and then click on the “Bluetooth” extension library (if it doesn’t exist, search Bluetooth to find it), as shown below:
As the Bluetooth and extension radio can’t work together, therefore, their extension libraries are not compatible.
Therefore, remove extension(s) and add Bluetooth please if you see the following prompt box pop up.
Test Code
Click“JavaScript”to view the corresponding JavaScript code:
Test Result
If you drag blocks step by step, you need to set as follows after finishing test code.
However, you could skip this step if you directly import test code.
After setting, download code to micro:bit board, don’t plug off the USB cable.Next to download App.
For IOS System:
Open App Store;
b. Search mecanum_robot and click“”to download the
Bluetooth App of mecanum_robot;
c. After downloading the APP, click “OPEN” or click the application mecanum_robot on the phone/iPad desktop to open the APP. A dialog box appears on the APP interface, and click “OK” in the dialog box.
d. First turn on the Bluetooth of the mobile phone/iPad, and then click the connect button (control) in the upper left corner of the APP interface to perform a Bluetooth search. In the search results, click “BCC micro:bit”. After a few seconds, the Bluetooth is connected.
For Android System:
a. Use the scanning function in the browser to scan and identify the QR code
or enter the link:http://8.210.52.206/mecanum_robot.apk to download. After the identification is successful, click “go to website” to enter the download mecanum_robot.apk page , click “Download” to download the mecanum_robot application.
b. Click“Allow allow”to enter Installation Diagram; click“install”to install the App.
c. Click “Open” or click the application mecanum_robot on the mobile phone desktop to open the APP, and a dialog box appears. In the dialog box, click “Allow” to turn on the Bluetooth of the mobile phone. You can also turn on the phone’s Bluetooth before opening the APP.
d. Click on the upper right corner to search for Bluetooth
and click“connect”; a few seconds later, the Bluetooth is paired.
Open CoolTerm, click Options to select SerialPort. Set COM port and 115200 baud rate. Click“OK”and“Connect”.
Point at micro:bit board and press the icons on APP, the corresponding characters are shown on CoolTerm monitor.
Through the test, we get the functions of every icon, as shown below:
Project 20.2:Multi-purpose Smart Car
Description
In this lesson, we will control the smart car to perform multipurpose functions.
Preparation
Insert the micro:bit board into the slot of keyestudio 4WD Mecanum Robot Car V2.0
Place batteries into battery holder
Dial power switch to ON end
Connect the micro:bit to your computer via an USB cable
Open the Web version of Makecode
Steps: Click the gear icon (Settings) in the upper right corner, then click on Extensions to go to the library file selection screen, and then click on the “Bluetooth” extension library (if it doesn’t exist, search Bluetooth to find it), as shown below:
As the Bluetooth and extension radio can’t work together, therefore, their extension libraries are not compatible.
Therefore, remove extension(s) and add Bluetooth please if you see the following prompt box pop up.
Test Code
Since the code is quite long, it won’t be displayed here. You can directly go to the following path to find the corresponding code.
Click“JavaScript” to view the corresponding JavaScript code: :
Test Result
This experiment combines the previous projects to make the car to perform actions via Bluetooth.
Enter Makecode online editor→Projecting Settings→, enable
“No Pairing….”(you could skip this step if you import test code
directly)
Download code to micro:bit board, dial POWER to ON end, and connect the Bluetooth, then you can control the car via the Bluetooth App of mecanum_robot.
Note: is used to adjust the speed, and
can
only be dragged.
Common Problems
The car has no reaction
Please check whether the batteries are sufficient
Please check whether the wirings are correct
Computers can’t recognize the USB ports
Please ensure whether the microbit driver is installed
Please check whether the USB wire is in good condition.
Code fails to burn and dot matrix displays expressions
Please check whether the Mecanum Robot Car_V2.py library file i mported