Sweep Programs

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”:

Servo Extension

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:

Sweep Program

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:

  1. Faster sweep – Make the servo sweep faster by reducing the pause between angle changes.
  2. 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.
  3. Tilt control – Use the accelerometer to control the servo angle based on how you tilt the Micro:bit.
  4. 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.