Example Code for micro:bit-Servo Control
This article provides step-by-step instructions on how to control a servo motor using a micro:bit, including hardware and software setup, a wiring diagram, and sample codes in MakeCode and Python editors. The servo rotates between 0-180 degrees every 4 seconds, making it ideal for educational and DIY robotics projects.
Hardware Preparation
- micro:bit x 1
- Servo x 1
- MicroUSB power supply
Software Preparation
- MakeCode Block Editor/ JavaScript Editor / BXY Python Editor
Wiring Diagram

Sample Code

Microsoft MakeCode JavaScript Editor
basic.forever(() => {
basic.pause(1000)
pins.servoWritePin(AnalogPin.P8, 0)
basic.pause(1000)
pins.servoWritePin(AnalogPin.P8, 90)
basic.pause(1000)
pins.servoWritePin(AnalogPin.P8, 180)
basic.pause(1000)
pins.servoWritePin(AnalogPin.P8, 90)
})
#http://docs.dfrobot.com.cn/bxy/examples/servo.html
from microbit import *
import Servo
sv=Servo(pin8)
while True:
sv.angle(0)
sleep(1000)
sv.angle(90)
sleep(1000)
sv.angle(180)
sleep(1000)
sv.angle(90)
sleep(1000)
Result
The Servo turns back and forth from 0-180 degrees every 4 seconds
The mounting screws should be well tightened to ensure a secure connection.
Micro: Mate only supports 3V (3.3V) analog input from Pin 0, 1, 2. The Micro USB Power port on Micro: Mate cannot be used for data transmission.
Components with large power consumption should be connected to Pin 8, 12, 16, with 5V power supply.
Was this article helpful?
