MakeCode_Tutorial
1. Programming On MakeCode
The following instructions are applied for Windows system but can also serve as a reference if you are using a different system.
1.1. Fast Start
Step 1 Connect to micro:bit
Connect the board to computer via USB cable.
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 flash when you burn a “.hex” file.
Then Micro: bit main board will appear on your computer as a driver named “MICROBIT”. Please note that it is not an ordinary USB disk as shown below.
Step 2 Write heartbeat program
Enter the link:online version of Makecode
Click “New Project” and you will see a “Creating a project”, fill it with “heartbeat” and click “Create √”.
Here we write programs on Google Chrome.
Let’s write a micro:bit code.
You can drag some Blocks to the editing area and then run your program in Simulator as shown below. Here we demonstrate how to edit heartbeat program.
Operation video guide:
Step3 Download codes
Generally, for Windows 10 APP (Get Windows 10 App)(Click), simply clicking the “Download” will directly download the code to the micro:bit board without any additional steps.
Yet for browsers, please:
Click “Download” in the editor. This will download a “hex” file, which is a format that the micro:bit board can read. After that, copy it to your micro:bit board just like you would copy a file to a USB drive. On Windows, you can also right-click on the “.hex” file and select “Send to → MICROBIT” to copy the file to the micro:bit board.
Or, you may directly drag the “.hex” file into MICROBIT.
During copying the “.hex” file to the Micro: bit, the yellow LED on the back of the board flashes. When the duplication is completed, the LED will stop flashing and remain on.
Step 4 Run porgram
After the program is uploaded to the Micro: bit, you can power it via USB cable or an external power. Then the 5 x 5 LED dot matrix displays a heartbeat pattern.
Caution: When you programs each time, the driver of Micro: bit will automatically eject and return so the hex files will disappear. The board only has access to hex files rather than save them.
1.2. MakeCode
Enter Makecode Google Chrome online version . Here is its main interface.
There are blocks “on start” and “forever”in the code editing area. After powering on, codes in “on start” only executes once, while those in “forever” runs cyclically.
Click “JS JavaScript” language:
Switch it to “Python” language:
1.3. Introduction to WebUSB Functions
As mentioned before, if your computer is Windows 10 and you have downloaded the APP MakeCode, you can quickly download codes to the board by “Download” button. We use the webUSB of Google Chrome to access the hardware device connected by USB.
Devices Pairing:
Connect the board to computer via USB cable.
Click “Download” -> “…” , and “Connect device”.
“Next”.
“Pair” .
Then select the corresponding device and “Connect” .
“Done”.
Download Program:
After connection, click “Download” and you will see the
becomes
. The program is downloaded to the micro:bit board.
If no device shows up for selection, please refer to Troubleshooting downloads with WebUSB. Browse the user guide to know how to update micro:bit firmware.
1.4. MakeCode Extensions Library
3.4.1 Import Library Extensions
Open makecode to enter a certain project, click
to choose
“Extensions”.
Or click “Extensions” above the Advanced.
Search the library you want.
We provide the code files for each project containing everything you need to run a project, so you can load it directly. If you want to build code blocks by yourself, remember to add the following three extensions.
OLED Extension:
Click “Extensions” to add library extensions.
Search “OLED” and click
.
Click the first oled-ssd1306 and wait for it to be added.
Add successful:
Ultrasonic sensor extension:
Click “Extensions” to add library extensions.
Search “sonar” and click
to find and load “sonar”.
Add successful:
DHT11 sensor extension:
Click “Extensions” to add library extensions.
2. Search “DHT11” and click
to find and load
“DHT11_DHT22”.
Add successful:
3.4.2 Update/Delete Extensions
Click “JavaScript” to switch to text code.
Click “Explorer”.
Find the “OLED” library and click
to delete it.
“Remove it”.
It is removed.
1.5. How to Import Codes to MakeCode
Let’s take the “heatbeat” project as an example to show how to load the code.
1. Open the Web version of Makecode or the Windows 10 App Makecode, and click “Import” .
“Import File…”
“Choose File” to import the file you want to load.
Here we load “heartbeat.hex” .
“Go ahead √”
In addition to the above method, you can also drag the the test code into the code editing area, as shown below:
Wait for loading.
2. Projects
Project 02: Traffic Lights
1. Overview
In this project, we adopt three LEDs( red, yellow and green), a speaker on micro:bit board and 5x5 LED matrix to make a model of traffic lights.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
red LED *1 |
yellow LED *1 |
green LED *1 |
|
|
|
220Ω resistor *3 |
jump wires |
breadboard *1 |
|
|
|
battery holder *1 (self-provided AA batteries *2) |
traffic lights card *1 |
3. Components Knowledge
Speaker
Micro: bit comes with a speaker, which makes it easy to make sound in your project.
4. Wiring Diagram
Note: the micro:bit board needs to be inserted into the T-type expansion board as shown below. The micro:bit board LED matrix should be on the same side with the logo of the expansion board.
5. Code Flow
6. Test Code
The code file is provided in folder Project 02:Traffic Lights, file Project-02-Traffic-Lights.hex.
Load code blocks:
7. Test Result
For Windows 10 App, click “Download” . For browsers, send the downloaded “.hex” file to the micro:bit board.
After downloading the code to the board, the green LED turns on and the 5×5 LED matrix counts down 6 seconds. After the green LED is off, the yellow LED flashes and the matrix counts down 3s with speaker sounding. At last, the red LED is on with a countdown of 6s. These actions repeat.
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
When powering on via external power supply, turn the DIP switch to ON.
Project 03: Ranging Bat
1. Overview
Based on an ultrasonic sensor, the ranging bat detects the distance of obstacles and displays it in real time on an OLED. When it is less than 10cm, the speaker alarms.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
ultrasonic sensor *1 |
OLED module *1 |
DuPont wires |
|
|
|
breadboard *1 |
jump wires |
battery holder *1 (self-provided AA batteries *2) |
|
|
|
bat card *1 |
OLED card *1 |
3. Components Knowledge
ultrasonic sensor
Ultrasonic waves bounce back when they hit an obstacle. We measure the distance by calculating the time interval between sending and receiving the waves. Since the propagation speed of sound in air is a constant v=340m/s, we calculate the distance between the sensor and the obstacle: s=vt/2.
The HC-SR04 ultrasonic module integrates a transmitter and receiver. The former converts electrical signals (electric energy) into high frequency (beyond human hearing) sound waves (mechanical energy), while the latter does the opposite.
The schematic diagram of the HC SR04:
Pin definition:
Parameters:
Operating voltage: 5V
Operating current: 12mA
Minimum measuring distance: 2cm
Maximum measuring distance: 200cm
Working principle:
A high level pulse lasting at least 10us is output on the Trig pin, and the module starts transmitting ultrasonic waves. At the same time, the Echo pin is pulled up. When the module receives an ultrasonic wave back when it encounters an obstacle, the Echo pin will be pulled down. The duration of the high level of the Echo pin is the total time of wave from sending to receiving: s=vt/2.
OLED module
OLED technology features rich color performance, high contrast and wide perspective, providing clear and vivid pictures, especially outstanding in black.
Each pixel of the OLED display emits light itself without backlight, so it consumes relatively low power. With small size, high resolution and low power consumption, the 0.9-inch OLED display is very suitable for wearable devices.
In this project, the OLED display module connects the SDA interface to pin P20 and SCL to pin P19.
Parameters:
Operating voltage: DC 3.3V-5V
Operating current: 30mA
Interface: Pin ports with a spacing of 2.54mm
Communication mode: I2C
Internal driver chip: SSD1306
Resolution: 128*64
Viewing Angle: greater than 150°
4. Wiring Diagram
When using the OLED display and ultrasonic sensor, we must connect an external power supply and turn the DIP switch to ON.
5. Code Flow
6. Test Code
The code file is provided in folder Project 03:Ranging Bat, file Project-03-Ranging-Bat.hex.
Load code blocks: The threshold in the condition 10 can be modified according to actual conditions.
7. Test Result
For Windows 10 App, click “Download” . For browsers, send the downloaded “.hex” file to the micro:bit board.
After downloading the code to the board, power on via external power supply and turn the DIP switch to ON, and the OLED displays the distance between the ultrasonic sensor and the obstacle in real time. When the distance value is less than 10cm, the speaker on micro:bit board alarms.
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
Project 04: Smart Paeking
1. Overview
Smart parking lots are everywhere. Can we also create a smart parking lot? Of course. We can use ultrasonic sensor to detect if there are vehicles ahead. When a vehicle (or thing) is detected approaching, we control servo to raise the lift rod; If it is detected to be moving away, the servo will lower the lift rod.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
ultrasonic sensor *1 |
servo *1 |
DuPont wires |
|
|
|
breadboard *1 |
jump wires |
battery holder *1 (self-provided AA batteries *2) |
|
|
|
bat card *1 |
lift rod card *1 |
3. Components Knowledge
Servo
Servo is a position driver. We can use servo to control the exact position or output high torque. Usually, it is used in robots, remote control cars, and even aircraft models. There are many specifications, but all servos comes with three wires: signal(orange), positive(red) and negative(brown). The color will vary from servo brands.
Internal structure diagram:
① Signal: receives control signals from the microcontroller;
② potentiometer: The position of the output shaft can be measured, which belongs to the feedback part of the whole servo;
③ Internal controller: The embedded board processes signals from external control, drives the motor and feedback position signals, which is the core of the whole servo;
④ DC motor: It is as an actuator to output speed, torque, position;
⑤ Transmission / servo mechanism: The mechanism zooms in the stroke output by the motor to the final output angle according to a certain transmission ratio.
Drive the servo
Send PWM signals to the servo signal line to control its output. The duty cycle of PWM directly determines the position of the output shaft. The period is usually 20 milliseconds and is typically set to generate pulses at a frequency of 50Hz.
For example (180° servo):
When we send a pulse width of 1.5 milliseconds (ms) to the 180° servo, the output shaft of the servo will move to the middle position (90 degrees);
If the pulse width is 0.5ms, the output shaft will move to 0 degree;
If the pulse width is 2.5ms, the output shaft will move to 180 degree;
Parameters:
Operating voltage: DC 3.3V~5V
Operating temperature: -10°C ~ +50°C
Dimensions: 32.25mm x 12.25mm x 30.42mm
Interface: 3pin interface with a spacing of 2.54mm
4. Wiring Diagram
When using the ultrasonic sensor and servo, we must connect an external power supply and turn the DIP switch to ON.
5. Code Flow
6. Test Code
The code file is provided in folder Project 04:Smart-Parking, file Project-04-Smart-Parking.hex.
Load code blocks: The threshold in the condition 10 can be modified according to actual conditions.
7. Test Result
After downloading the code to the board, when the ultrasonic sensor detect a vehicle (or thing) approaching, the servo controls the lift rod to raise; If the sensor detects it moving away, the servo will lower the lift rod.
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
Project 05: Car Dial
1. Overview
In this project, we combine an adjustable potentiometer, a servo and a beautiful dial card to make a simple car dial model.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
potentiometer *1 |
servo *1 |
jump wires |
|
|
|
breadboard *1 |
battery holder *1 (self-provided AA batteries *2) |
potentiometer card *1 |
|
||
car dial card*1 |
3. Components Knowledge
potentiometer
A potentiometer is also a resistor element with three contacts, whose resistance value can be adjusted according to some regularity.
They come in all shapes, sizes and values, but they all have the followings in common:
① Three terminals (or connection points).
② A movable knob or slider that can change the resistance between the intermediate terminal and any external terminal.
③ As the knob is moved, the resistance between the intermediate terminal and any external terminal varies from 0Ω to its maximum.
The circuit symbol of potentiometer:
(1). As a voltage divider
The potentiometer is a continuously adjustable resistor. When you rotate its slider, the moving contact slides across the resistor. At this point, a voltage can be output according to the voltage applied to potentiometer and the angle or stroke of rotation of the movable slider.
(2). As a variable resistor
When potentiometer is used as a variable resistor, connect its intermediate terminal to one of two additional terminals in the circuit. In this way, you can obtain a steady and continuously varying resistance value within the range of it.
(3). As a current controller
When it is used as a current controller, the moving contact must be connected as one of the output terminals.
4. Wiring Diagram
When using the servo, we must connect an external power supply and turn the DIP switch to ON.
5. Code Flow
6. Test Code
The code file is provided in folder Project 05:Car Dial, file Project-05-Car-Dial.hex.
Load code blocks:
7. Test Result
After downloading the code to the board, rotate the knob on potentiometer and the servo moves the pointer on the dial.
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
Project 06: Music Party
1. Overview
When we clap our hands, the microphone on the board picks up sound signals, and the speaker plays a cheerful birthday song while the RGB LED emits dazzling light.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
red LED *1 |
220Ω resistor *3 |
jump wire *2 |
|
|
|
breadboard *1 |
battery holder *1 (self-provided AA batteries *2) |
RGB card *1 |
3. Components Knowledge
Microphone
A high-quality digital microphone is integrated on the front side of the micro:bit V2 board to detect sound and audio signals. The chip that controls and processes the microphone is on its back.
The microphone is in a small round hole on the front of the board, which is convenient to capture surrounding sound signals. Just place the micro:bit board face up when using. Next to the hole is a microphone LED indicator. When the micro:bit measures sound levels, the indicator will light up.
RGB LED
RGB LED is imaged in the intersection of three primary colors (RGB): red, green and blue. Most colors can be synthesized by RGB in different proportions. The red, green and blue LEDs are packaged in a transparent plastic case to emit colors of light by changing the input voltage of R, G and B pins.
Trichromatic theory:
RGB LED can be divided into two types: common anode and common cathode:
In a common cathode RGB LED, the three LEDs share a negative connection (cathode);
In a common anode RGB LED, the three LEDs share a positive connection (anode).
Note: Herein, we provide a common cathode RGB LED.
RGB LED pins:
RGB LED boasts 4 pins: GND(the longest one), R(red), G(green) and B(blue). Place the RGB LED as shown below, pins from left to right are red, GND, green and blue.
4. Wiring Diagram
5. Code Flow
6. Test Code
The code file is provided in folder Project 06:Music Party, file Project-06-Music-Party.hex.
Load code blocks:
7. Test Result
After downloading the code to the board, when we clap our hands, the microphone on the board picks up sound signals, and the speaker plays a cheerful birthday song while the RGB LED emits dazzling light. Isn’t the music party in a happy and joyful atmosphere?
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
Project 07: Environment Monitoring
1. Overview
On the OLED, the smart environment monitoring system displays the temperature and humidity values detected by the DHT11 sensor in real time, as well as the brightness level value of ambient light detected by the on-board light sensor.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
XHT11 temperature and humidity sensor *1 |
OLED module *1 |
DuPont wires |
|
|
|
breadboard *1 |
jump wires |
battery holder *1 (self-provided AA batteries *2) |
|
|
|
cloud card *1 |
OLED card *1 |
3. Components Knowledge
XHT11 temperature and humidity sensor
XHT11 temperature and humidity sensor is a composite sensor with calibrated digital signal output, which can detect the humidity and temperature in the air.
Accuracy: humidity ±5%RH, temperature ±2℃
Detection range: humidity 5%RH ~ 95%RH, temperature -25℃ ~ +60℃
The sensor uses special digital module acquisition and temperature and humidity sensing technology to ensure extremely high reliability and excellent long-term stability. It includes a resistive humidity sensing element and an NTC temperature sensing element, which is very suitable for measurement with relatively low accuracy and real-time requirements.
XHT11 communication mode:
Single bus communication is adopted. It means that there is only one data line for data exchange and control in the system.
Definition of data bits transmitted by single bus:
Single bus data format: 40 bits of data are transmitted at a time, with the high bit coming first.
8bit humidity integer + 8bit humidity decimal + 8bit temperature integer + 8bit temperature decimal + 8bit parity bit (The decimal part of the humidity is 0)
Definition of parity bit
8bit humidity integer + 8bit humidity decimal + 8bit temperature integer + 8bit temperature decimal. 8bit parity bit = the last 8 bits of the obtained result
Data timeline:
After the user host (MCU) sends a starting signal, the XHT11 switches from low power mode to high speed mode. After the starting signal, XHT11 sends a response signal and 40bit data, and triggers a signal acquisition.
The signal transmission is shown in the figure:
Parameters
Operating voltage: DC 3.3V to 5V
Operating current: 2.1mA
Maximum power: 0.0105W
Temperature range: -25℃ ~ +60℃ (± 2℃)
Humidity range: 5%RH ~ 95%RH (accuracy ±5%RH under around 25 ° C)
Microbit Light Sensor
A light sensor is an input device that measures the brightness of external light. The micro:bit board does not include a built-in light sensor. It detects and senses ambient brightness by an LED matrix that repeatedly convert the light intensity into a value input, and then the voltage attenuation time is sampled. In this way, the detected brightness level is a relative value.
4. Wiring Diagram
When using the OLED display, we must connect an external power supply and turn the DIP switch to ON.
5. Code Flow
6. Test Code
The code file is provided in folder Project 07:Environment Monitoring, file Project-07-Environment-Monitoring.hex.
Load code blocks:
7. Test Result
After downloading the code to the board, the OLED displays the temperature and humidity values and the light brightness level in real time.
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
Project 08: Anti-theft Alarm
1. Overview
When the smart anti-theft alarm detects that the anti-theft box has been moved, the speaker on the micro:bit board will alarm and the red LED will flash.
2. Components
|
|
|
|---|---|---|
micro:bit board *1 |
micro:bit T-type expansion board *1 |
micro USB cable *1 |
|
|
|
red LED *1 |
220Ω resistor *1 |
jump wire *2 |
|
|
|
breadboard *1 |
battery holder *1 (self-provided AA batteries *2) |
alarm card *1 |
3. Components Knowledge
Accelerometer
The micro:bit board boasts a built-in LSM303AGR acceleration sensor (we called 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 it is slightly swung, the detected acceleration is much less than the that of gravity, so the difference can be ignored. Therefore, we mainly detect the change of gravitational acceleration on x, y, and z axes.
4. Wiring Diagram
The board control pin of LED is P1 (the pin of T-type expansion board is digital 1).
5. Code Flow
6. Test Code
The code file is provided in folder Project 08:Burglar Alarm, file Project-08-Burglar-Alarm.hex.
Load code blocks:
After importing the code, if the buzzer keeps sounding even though the breadboard is not moved; it may be caused by geographical factors. You can modify the threshold in the condition -60 and 50 according to actual conditions.
7. Test Result
After downloading the code to the board, move the breadboard. If the
acceleration value x<-60 or x>50, the speaker on the board alarms and
the LED flashes, and the micro:bit LED matrix shows
.
Otherwise, the speaker makes no sound and LED is off, and the micro:bit
LED matrix shows
.
ATTENTION: If the wiring is correct but you cannot see the results, press the reset button on the back of the board.
3. Troubleshooting
MAINTENANCE: Code fails to download to Micro:bit
1. Problem
Recently, many users encounter the issue that Micro:bit board doesn’t respond when download code.
If the way you operate is correct, maybe you accidentally press the reset button and enter the Maintenance mode or the firmware is lost due to mis-operation.
Plug in Micro:bit board, the “MAINTENANCE” drive appears, which means the program can’t be downloaded.
2. Solutions
Download the .hex file from this page to your computer.
Download the latest micro:bit firmware-0255. If you do not want to download from this website, we also provide it in our tutorial.
(2) After the latest firmware is downloaded, then drag Firmware for V2.20_V2.21 into the “MAINTENANCE” to make Micro:bit back to normal mode.
We install different firmwares according to micro:bit board models. Here it is Firmware for V2.20_V2.21.
3. Avoid to enter “MAINTENANCE”
(1) Make sure the Reset button is not pressed when plugging the board by USB cable.
(2) Don’t unplug the cable suddenly during downloading micro:bit program, otherwise, the firmware will be lost and micro:bit will enter “MAINTENANCE” mode.
(3) In the experiment, wrong wiring also cause a short circuit or firmware lost.
Troubleshooting Downloads with WebUSB
Clink:Troubleshooting Downloads with WebUSB
Having issues pairing your micro:bit with WebUSB? Let’s try to figure out why!
Step 1: Check your cable
Make sure that your micro:bit is connected to your computer with a micro USB cable. For example, in Windows Explorer you should see a MICROBIT drive appear when it’s connected.
If you can see the MICROBIT drive go to step 2. If you can’t see the drive:
Make sure that the USB cable is working.
Does the cable work on another computer? If not, find a different cable to use. Some cables may only provide a power connection and don’t actually transfer data.
Try another USB port on your computer.
Is the cable good but you still can’t see the MICROBIT drive? Hmm, you might have a problem with your micro:bit.
Try the additional steps described in the fault finding page at microbit.org. If this doesn’t help, you can create a support ticket to notify the Micro:bit Foundation of the problem. Skip the remaining troubleshooting steps.
Step 2: Check your firmware version
If your downloads still aren’t working, it’s possible that the firmware version on the micro:bit needs an update. Let’s check:
Go to the MICROBIT drive;
Open the DETAILS.TXT file;
Find the firmware version number; Look for a line in the file that says the version number. It should say Version:
If the version is 0234, 0241, 0243 you NEED to update the firmware on your micro:bit. Go to Step 3 and follow the upgrade instructions.
If the version is 0249, 0250 or higher, you have the right firmware go to step 4.
Step 3: Upgrade the firmware
Put your micro:bit into MAINTENANCE Mode.
To do this, unplug the USB cable from the micro:bit and then reconnect the USB cable while you hold down the reset button. Once you insert the cable, you can release the reset button.
You should now see a MAINTENANCE drive instead of the MICROBIT drive like before. Also, a yellow LED light will stay on next to the reset button.
(2) Download the firmware .hex file.
We install different firmwares according to micro:bit board models. Here it is Firmware for V2.20_V2.21.
Drag and drop that file onto the MAINTENANCE drive.
Look for the flashing LED.
The yellow LED will flash while the HEX file is copying. When the copy finishes, the LED will go off and the micro:bit resets. The MAINTENANCE drive now changes back to MICROBIT.
Upgrade complete.
The upgrade is complete! You can open the DETAILS.TXT file to check and see that the firmware version changed to the match the version of the HEX file you copied.
If you want to know more about connecting the board, MAINTENANCE Mode, and upgrading the firmware, read about it in the Firmware guide.
Step 4: Check your browser version
WebUSB is a fairly new feature and may require you to update your browser. Check that your browser version matches one of those below: Browser versions for Android, Chrome OS, Linux, macOS, and Chrome 65+ for Windows 10.
Step 5: Pair device
Once you’ve updated the firmware, open the Chrome Browser, go to the editor and click on Pair Device in the gearwheel menu. See WebUSB(/device/usb/webusb) for pairing instructions.
Enjoy fast downloads!









icon. Press button A, and 5x5 LED matrix shows
icon, LED turns
on. Press button B, 5x5 LED matrix shows
icon, LED goes off.
Does it look like a mini lamp?
















