Micro Servo with micro:bit


One of the most entertaining things to do with electronics is to make a mechanical device move. A popular device used to make things move is a servomotor or servo for short.

In this guide, learn to connect a micro servo to the micro:bit and program it in MakeCode.

Complete this guide to learn the basics in controlling a servo.


While there are many different types of motors, a servo is ideal for pointer-like position-control applications. They are commonly used in robotics, for steering model cars, boats, airplanes and for many other sensor-positioning purposes. Here are some key points on servos:

  • It receives control signals from a controller, in this case the micro:bit
  • Its position can be controlled more precisely than a standard DC motor
  • Its rotation angle can be set (limited to 180 degrees)
  • They are rated in kg/cm (kilogram per centimeter) which tells you how much weight your servo motor can lift at a particular distance.
    • For example, the TowerPro Analog SG90 servo used in this guide is rated 1.8kg/cm at an operating voltage of 4.8V.
    • So it should be able to lift 1.8kg if the load is suspended 1 cm away from the motor shaft.
    • The greater the distance, the lesser the weight carrying capacity.

It has three wires:

  • Ground (Brown) 
  • 5V Power (Red) 
  • PWM Signal (Orange)

In the following steps, you will learn to control a servo with the micro:bit. 

Pulse Width Modulation (PWM)

  • The micro:bit by itself is not able to send varying degrees of voltage out through its GPIO pins. So, what if you needed to produce analog effects such as when using a servo? 
  • To simulate analog effects, this can be done through pulse width modulation (PWM).
  • What PWM does is it turns a digital pin ON and OFF very quickly, which simulates various voltage levels. 
  • By turning a pin ON and OFF very quickly, this varies the duty cycle of the pin (how much time it is spent in the 'ON' state)
  • Yup, the more time the pin is spent 'ON' over the course of a second, the longer the duty cycle, and higher the simulated voltage.
  • The MakeCode editor has servo-specific blocks that will allow the control of a servo through PWM.
    • The PWM signal is mapped to specific angles from 0 to 180 degrees


  • There are two ways to power the micro:bit:
    • By using an external battery pack
    • By using a USB cable
  • However, there is a maximum amount of current that the micro:bit can handle. 
  • Using the micro:bit to power the servo via USB will get you very close to the limit. 
  • To power the servo and make sure you are getting the full amount of torque from the servo, a battery pack is the ideal choice. For greater accuracy, we recommend powering the servo with an operating voltage of 4.8V to 6V.
  • Note: While you could power the micro:bit via 3 volts, and get basic movement going from one position to another, it may not give you a highly accurate angle of rotation. Also, in a real application, it can cause issues with the micro:bit if the servo draws too much current when a load is placed on it.

Step 1  Pin Overview

Step 2  P0 to SIG

Step 3  5V

Step 4  GND to GND

Step 5  MakeCode Example 1!

  • The MakeCode editor comes with support for using the servo. We'll program it so that:
    • By pressing button A on the micro:bit, the servo will turn a 180 degrees, and so spinning around one way
    • By pressing button B, it will return to its starting position.
  • Simply go to Pins tab, and grab a 'servo write pin P0 to 180' block. 
  • Then get out a 'pause (ms) 1000' block from the 'Basic' tab, and place this under it.
  • Do the same with button B, this time setting the 'servo write pin ... to ...' block to 'servo write pin P0 to 0'

Step 6  MakeCode Example 2!

In this second example:

  • After pressing button A, the micro:bit will start to turn one way followed by a pause of 1 second or 1000 ms, before going back to its starting position. 
  • Then there is another pause of 1 second. 
  • This process continues, where the servo moves one way and then the other until the micro:bit's power source is disconnected.

Step 7  MakeCode Example 3!

  • The blocks in this example are kept exactly the same as Example 1, with an added 'servo set pulse pin ... to ... ' block used in 'on start'.
  • Changing the value of this block changes the pulse width to a specified value.
  • The following code sets the servo pulse to 500 microseconds.

Step 8  Upload the code to micro:bit

Finally, to upload the code to the micro:bit and test it out:

  1. Connect the micro:bit to your computer using a microUSB cable
  2. Click on the 'Download' button on the bottom left corner in MakeCode editor
  3. Once downloaded, you can find the hex file in your 'Downloads' folder.
  4. Drag and drop it to the MICROBIT drive