MakeCode extensions add new block categories for specific hardware
The servo extension provides blocks that set a servo to a specific angle
A sweep program loops through angles from 0 to 180 and back, with small pauses between steps
Changing the pause duration changes the sweep speed
Common Mistakes:
Forgetting to install the servo extension before trying to use servo blocks
Setting angles outside the 0-180 range (servo will not respond or may buzz)
Making the pause too short (servo cannot physically move fast enough and will vibrate)
Using the wrong pin number in the servo block (must match the physical connection)
Assessment Cues:
Student’s servo sweeps smoothly through the full range without stuttering
Student can explain what would happen if the pause value were changed
Student successfully modifies the program for at least one challenge
Differentiation:
Struggling: Provide the sweep program pre-built and focus on downloading and testing; have them make one small change (pause value)
Advanced: Write a program that moves the servo to specific angles based on light level or accelerometer tilt
Installing the Servo Extension
Before writing a servo program, you need to install the servo extension in
MakeCode. Open the MakeCode editor, click on Extensions, and search for
“servo”:
Click on the servo extension to install it. You will see new blocks appear in
the block palette.
Sweep Program
Create a new program like this one:
You can import this program from GitHub:
https://github.com/League-Microbit/Servo-Sweep.git
Download the program to your Micro:bit. Your servo should now sweep back and
forth through the full range of its motion, 180 degrees.
Tip
If the servo buzzes or vibrates instead of sweeping, check that your pin number
in the code matches the pin where the servo is physically connected (Pin 0).
Challenges
Here are a few things to try:
Faster sweep – Make the servo sweep faster by reducing the pause between
angle changes.
Button control – Make the servo move with the A and B buttons.
For example, A moves it 10 degrees clockwise and B moves it 10 degrees
counterclockwise.
Tilt control – Use the accelerometer to control the servo angle based on
how you tilt the Micro:bit.
Light tracker – Use the light sensor to point the servo toward the
brightest direction.
Learn More
If you are interested in the details of how a hobby servo works, this video
offers a complete teardown and explanation of the parts.