Micro:bit Stone Thrower Projects
Code Download
Click to download the code package.

2. Rotary Potentiometer

2.1 Introduction
Potentiometer module is essentially an adjustable resistor. When we rotate it, we change its resistance. We set up the corresponding circuit to convert the change in the resistance into a change in the voltage, which is then detected by the analog input port of the micro: bit board through the signal end.
2.2 Schematic Diagram

2.3 Parameters
Operating voltage: DC 3.3-5V
Operating temperature: -10°C ~ +50°C
Dimensions: 35mm x 20mm x 28mm
Interface: 3PIN interface
Output signal: analog signal
2.4 Wiring Diagram

2.5 Test Code
Load the code from computer:
Download the code package first, and unzip and upload the code to micro bit board. If you do not know how to do it, please learn how to upload code in basic projects.

Build the code blocks manually:
In
, put
into 

In
, put
into
, and modify the write value to Pot:In
, add a
and set the pin to P2, and put it into the last box of 

In
, add a delay
under
and set to 200ms
Complete code:

2.6 Test Result
After uploading code, you can see the analog values of the potentiometer. If you are a Windows10 System user, the values can be displayed online via WebUSB.

If values cannot be displayed via WebUSB, you may need a serial tool. Click to learn how to use a serial tool in basic projects.
After uploading code, open the CoolTerm and click Options, click SerialPort to choose the COM port and set baud rate to 115200 (the tested USB serial baud rate of Micro:bit board is 115200). Click OK and Connect.

Now you can see the analog values of the potentiometer on the serial monitor, and these results will be refreshed every 0.2s.

3. Servo
3.1 Introduction
The servo is a kind of position servo driver, which is mainly composed of housing, circuit board, core-less motor, gear and position detector. The receiver or microcontroller sends a signal to the servo which has an internal reference circuit that generates a reference signal with a period of 20ms and a width of 1.5ms, and compares the DC bias voltage with the voltage of the potentiometer to output voltage difference.
There are many specifications of servo, but they all contain three wires respectively in brown, red, and orange (colors may vary from brands). The brown is GND, the red is the positive power supply, and the orange is the signal line.

The rotation Angle of the servo is controlled by adjusting the duty cycle of the PWM (pulse width modulation) signals. Theoretically, the period of the standard PWM signal is fixed at 20ms (50Hz), so the pulse width should be 1ms ~ 2ms. But in fact, it is 0.5ms ~ 2.5ms, corresponding to the servo angle of 0° ~ 180°.

3.2 Parameters
Operating voltage: DC 3.3V〜5V
Operating angle range: approx. 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)
Stop 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)
3.3 Wiring Diagram

3.4 Test Code
Load the code from computer:
Download the code package first, and unzip and upload the code to micro bit board. If you do not know how to do it, please learn how to upload code in basic projects.

Build the code blocks manually:
Click
to import the servo library

Search “Servo” and click
, and you can see the Servo extension. Click to load it.

Successfully loaded.

In
, put
into
, and set pin to P1 and angle to 0°.

In
, add
under
and set the delay to 1000ms

repeat step 4 and 5, set the servo angle to 90° and 180° with delays of 1000ms
Complete code:

3.5 Test Result
After uploading code, the servo rotates from 0 degree to 90 degree and then to 180 degree with intervals of 1 second of each.
4. Ultrasonic Sensor
4.1 Introduction
Like bats, HC-SR04 ultrasonic sensor adopts sonar to determine the distance to objects. It provides excellent contact-less range inspection with high accuracy and stable readings. Internally, it is equipped with ultrasonic transmitter and receiver.
In application, it is used in a wide range of electronics projects for obstacle detection and distance measurement.
4.2 Parameters
Operating voltage: 3.3-5V
Static current: <2mA
Working current: 15mA
Valid angle: <15°
Distance range: 2cm – 400 cm
Accuracy: 0.3 cm
Measuring Angle: 30 degrees
Trigger input pulse width: 10 microseconds

4.3 Wiring Diagram

VCC(red) |
Trig(white) |
Echo(brown) |
Gnd(black) |
|---|---|---|---|
3V3 |
P8 / io4 |
P12 / io15 |
GND |
4.4 Test Code
Load the code from computer:
Download the code package first, and unzip and upload the code to micro bit board. If you do not know how to do it, please learn how to upload code in basic projects.

Build the code blocks manually:
Click
to load ultrasonic library.

search
sonarand click
. You can see the sonarlibrary, click to load it.

successfully loaded.

In
, add
into 

In
, click
to define a variable named value.

In
, add
into 

In
, add
into the value box of
, and set Trig to pin P8, echo to pin P12, and unit to CM

In
, add
under
, and set the write value to “distance=”

In
, find
and put it into the last box of 

In
, add a delay
under
and set to 500ms
Complete code:

4.5 Test Result
After uploading code, you can see the distance values detected by the ultrasonic sensor on the serial monitor. If you are a Windows10 System user, the values can be displayed online via WebUSB.

If values cannot be displayed via WebUSB, you may need a serial tool. Click to learn how to use a serial tool in basic projects.
After uploading code, open the CoolTerm and click Options, click SerialPort to choose the COM port and set baud rate to 115200 (the tested USB serial baud rate of Micro:bit board is 115200). Click OK and Connect.

5. Stone Thrower
5.1 Introduction
In this project, we build a stone thrower with a button module, a potentiometer, an ultrasonic sensor and servos.
5.2 Wiring Table
Connections are the same, except the servo 2. Here is the wiring table.
Module |
GND |
VCC |
S / Echo |
Trig |
|---|---|---|---|---|
button |
GND (black) |
3V3 (red) |
P9 / io16 (yellow) |
|
potentiometer |
GND (black) |
3V3 (red) |
P2 / io32 (yellow) |
|
servo 1 (base) |
GND (brown) |
3V3 (red) |
P0 / io12 (yellow) |
|
servo 2 (arm) |
GND (brown) |
3V3 (red) |
P1 / io14 (yellow) |
|
ultrasonic sensor |
GND (black) |
3V3 (red) |
P12 / io15 (brown) |
P8 / io4 (white) |
5.3 Test Code
Load the code from computer:
Download the code package first, and unzip and upload the code to micro bit board. If you do not know how to do it, please learn how to upload code in basic projects.

Complete code:

5.4 Test Result
After successfully uploading the code, the throwing direction of the catapult can be controlled by rotating the button, and the trigger of the catapult can be controlled by pressing the button.
Troubleshooting
1. Code fails to download to Micro:bit
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.

Solution
Download the hex file from this page to your computer.
Down load the latest micro:bit firmware-0255: https://www.microbit.org/get-started/user-guide/firmware/ If you do not want to download from this website, we also provide it in our tutorial.
After the latest firmware is downloaded, then drag it into the “MAINTENANCE” to make Micro:bit back to normal mode.

Avoid to Enter “MAINTENANCE”
Make sure the Reset button is not pressed when plugging the board by USB cable.

Don’t unplug the cable suddenly during downloading micro:bit program, otherwise, the firmware will be lost and micro:bit will enter “MAINTENANCE” mode.
In the experiment, wrong wiring also cause a short circuit or firmware lost.
2.Troubleshooting-Download with WebUSB
Having trouble pairing the Micro: bit with WebUSB (/ device/usb/webusb)?
Step 1: Check cable
Make sure that your micro:bit is connected to your computer with a micro USB cable. You should see a MICROBIT drive appear in Windows Explorer when it’s connected.

If you can see the MICROBIT, please go to step 2.
If you can’t:
Make sure that the USB cable is working. Does the cable works 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 falut finding at microbit.org.
If this doesn’t help, you can create a support ticket to notify the Micro:bit Foundation of the problem. If you do so, skip the rest of these steps.
Step 2: Check firmware version
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.

Look for the version number. It should say Version: …

Or Interface 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, just go to step 4.
Step 3: Update firmware
Put your micro:bit into MAINTENANCE Mode.
To do this, please unplug the USB cable from the micro:bit and then re-connect the USB cable after pressing and holding the reset button. Once you insert the cable, you can release the reset button. You should now see MAINTENANCE instead of the MICROBIT drive. Also, a yellow LED indicator will stay on.

Download firmware .hex file: https://microbit.org/guide/firmware/
Drag the file into the MAINTENANCE drive.
The yellow LED will flash while the HEX file is copying. After that, the LED will go off and the micro:bit resets. The MAINTENANCE drive now changes to MICROBIT.
The upgrade is complete! You can open the DETAILS.TXT file to check the firmware version that matches the one of the HEX file you copied.
If you want to know more about connecting the board, MAINTENANCE Mode, and upgrading the firmware, please refer to Firmware guide.
Step 4: Check version of Browser
WebUSB may require you to update your browser.
Check that your browser version matches one of these: Android, Chrome OS, Linux, macOS and Windows 10 Chrome 65+.
Step 5: Pair device
Once you’ve updated the firmware, open the Chrome Browser, go to the editor and click on Pair Device in settings.
See WebUSB (/ device / usb / webusb) for pairing instructions.
Resources
Keyestudio official:
Keyestudio wiki main page:
https://wiki.keyestudio.com/Main_Page
MicrobitCode:
https://makecode.microbit.org/
Microbit official:





, and set delay to 100ms


into the 
, add a
into 
into the condition box of 
and find
and set the pin to 
under “then”
under “else” 
. Press the button and it displays
. Release the button and it shows back to