Published 2026-04-02
This guide provides a complete, beginner-friendly solution for making a standard microservowork with a micro:bit. Whether you are building a simple robotic arm, a moving indicator, or a small animatronic project, the core challenge is the same: you need to accurately control theservo’s position using the micro:bit’s output signals. By following the steps below—connecting the hardware correctly, writing the control code, and powering the setup properly—you will have a functioningservowithin minutes. This guide focuses on the most common scenario, controlling a single servo, and provides solutions for the typical issues users face, such as jittering, insufficient power, and unresponsive movement.
Before connecting any wires, it is essential to understand what aMicro Servoneeds to function. A typicalMicro Servo, such as the common 9g servo, has three wires:
Brown or Black: Ground (GND)
Red: Power (VCC, typically 4.8V to 6.0V)
Orange or Yellow: Signal (PWM)
The micro:bit operates at 3.3V logic. This is critical because the servo’s control signal must be compatible. A standardMicro Servocan be controlled by a 3.3V signal from the micro:bit without any level shifting. However, the power requirement is different. The servo’s motor draws significantly more current than the micro:bit can supply through its standard pins.
Key Fact:The micro:bit’s 3V pin (Pin 1) is regulated and cannot supply the current needed for a servo motor under load without causing the board to reset or behave erratically. Therefore,do not power the servo directly from the micro:bit’s 3V pin. You must use an external power source.
The connection method ensures both proper control and stable operation. For a single servo, this is the recommended setup.
1x micro:bit board
1x micro servo (e.g., SG90 or similar 9g servo)
1x external power source (3x AA battery pack providing ~4.5V, or 4x AA providing ~6V, is ideal)
Jumper wires (female-to-female or male-to-female as needed)
A micro:bit breakout board or edge connector (optional, but highly recommended for reliable connections)
This is the most stable configuration. All grounds must be connected together to form a common reference point.
1. Servo Ground (Brown/Black)→ Connect tomicro:bit GND pinANDExternal Power Supply GND (- terminal) .
2. Servo Power (Red)→ Connect toExternal Power Supply VCC (+ terminal) .
3. Servo Signal (Orange/Yellow)→ Connect tomicro:bit Pin 0(or any other available pin like 1, 2,8, 12, 13, 14, 15, 16).
Example with a 3xAA Battery Pack:
Connect the black wire from the battery pack to the micro:bit’s GND pin and the servo’s brown wire.
Connect the red wire from the battery pack to the servo’s red wire.
Connect the servo’s yellow wire to micro:bit Pin 0.
This ensures that the servo receives its operating power from the batteries while the signal reference is shared with the micro:bit.
The micro:bit uses Pulse Width Modulation (PWM) to control the servo’s angle. The servo expects a 50Hz signal (a period of 20ms). The position is determined by the pulse width:
0 degrees:0.5 ms pulse
90 degrees:1.5 ms pulse
180 degrees:2.5 ms pulse
Modern coding environments abstract this complexity. Below are complete, verified code examples for the two most common programming environments.
This is the simplest method for beginners.
1. Go to the MakeCode editor for micro:bit.
2. Add theServo extension:
Click on “Extensions” in the toolbox.
Search for “servo” and add theservolibrary.
3. Use the following block code:
on start servo write pin P0 to 0° pause 1000 ms servo write pin P0 to 90° pause 1000 ms servo write pin P0 to 180°
To run the servo continuously in a loop, use theforeverblock. Theservo write pinblock automatically sets up the PWM signal at 50Hz and adjusts the pulse width for the angle.
For more control, MicroPython provides direct PWM access. This code is verified and includes a function to map angles to duty cycles for the micro:bit’s 20ms PWM period.
from microbit importimport music # Define the PWM period for a servo (20ms)# On micro:bit, the PWM period is set in microseconds. 20ms = 20000us# The duty cycle is a value from 0 to 1023 (10-bit resolution). # Pulse width: 0.5ms (0 deg) to 2.5ms (180 deg) def set_servo_angle(pin, angle): # Constrain the angle to 0-180 if angle 180: angle = 180 # Map angle to pulse width in microseconds: 0.5ms to 2.5ms pulse_width = 500 + (angle2000 / 180) # Convert pulse width to duty cycle for 20ms period duty = int(pulse_width * 1023 / 20000) pin.set_analog_period(20) # 20ms period = 50Hz pin.write_analog(duty) # Main execution while True: # Sweep from 0 to 180 degrees on pin 0 for angle in range(0, 181, 5): set_servo_angle(pin0, angle) sleep(50) # Sweep back from 180 to 0 for angle in range(180, -1, -5): set_servo_angle(pin0, angle) sleep(50)
This code initializes the servo on Pin 0 and sweeps it back and forth continuously.
When making a micro servo work with a micro:bit, users frequently encounter three specific problems. Here are the verified solutions.
Symptom:The servo moves back and forth rapidly or makes buzzing sounds without a command.
Cause:The most common cause is an unstable or insufficient power supply. When the servo tries to move, it draws a surge of current. If the voltage drops below a critical level, the micro:bit may reset, or the servo’s logic circuit may malfunction.
Solution:
Use a fresh battery pack:Ensure the external batteries are new or fully charged.
Check ground connection:Verify that the external power supply ground and the micro:bit ground are securely connected.
Add a capacitor:Placing a large electrolytic capacitor (100µF to 1000µF) across the power and ground lines of the servo (red and brown wires) can smooth out voltage spikes. This is a standard practice in robotics.
Symptom:The servo is silent and does not respond to code.
Cause:This is typically a wiring or signal issue. The servo may not be receiving power, or the signal pin may not be correctly assigned.
Solution:
1. Test the servo:Connect the servo directly to the battery pack (red to +, brown to -) without the micro:bit. The servo should make a slight humming sound or attempt to center. If it does nothing, the servo or battery pack may be faulty.
2. Verify the signal pin:Confirm that the yellow/orange wire is connected to the pin specified in your code (e.g., Pin 0).
3. Check the code:Ensure the servo initialization code (likepin.set_analog_period(20)in MicroPython or theservo writeblock in MakeCode) is being executed.
Symptom:The servo only moves to 0° and 180°, ignoring intermediate angles.
Cause:The PWM signal is not being generated correctly. This is often due to using a simple digitalwritefunction instead of a proper PWM output or using an incorrect period.
Solution:
In MakeCode:Do not use the standarddigital writepin block. Always use theservo writeblock after adding the extension.
In MicroPython:Do not usepin.write_digital(). Usepin.set_analog_period()followed bypin.write_analog()with a calculated duty cycle as shown in the example above. A 50Hz signal (20ms period) is mandatory for standard servos.
Understanding the power requirements ensures long-term reliability.
Critical Note:The micro:bit’s edge connector pins are not designed to supply more than 90mA total. Attempting to power a moving servo from the micro:bit’s 3V pin will cause the board’s voltage regulator to overheat or shut down, leading to unpredictable behavior or permanent damage. The external power source used must be capable of delivering at least 1A for a single servo to handle startup and stall currents safely.
Recommended External Power Sources (in order of preference):
1. 3x AA battery pack (alkaline):Provides ~4.5V, which is ideal for most micro servos. It is simple and safe.
2. 4x AA battery pack (alkaline):Provides ~6.0V, giving the servo more torque. This is also acceptable.
3. Lithium-ion battery pack:A single-cell (3.7V) is insufficient. A 2-cell (7.4V) pack requires a voltage regulator to step down to 5V-6V.
To ensure your micro servo works reliably with your micro:bit from the first attempt, follow this consolidated action plan:
1. Prepare your hardware:Gather a micro:bit, a micro servo, and a 3xAA or 4xAA battery pack. Do not attempt to use only the micro:bit’s USB power for the servo.
2. Wire correctly:Connect the servo’s brown wire to both the battery pack’s black wire and the micro:bit’s GND. Connect the servo’s red wire to the battery pack’s red wire. Connect the servo’s yellow wire to micro:bit Pin 0.
3. Write or upload the code:Use the MakeCode servo extension or the MicroPython script provided above. Start with a simple test that moves the servo to 0°, 90°, and 180° with pauses.
4. Test power first:Before connecting the micro:bit, briefly connect the servo directly to the battery pack to confirm it responds (it will center or hum). Disconnect it before proceeding.
5. Run the test:Connect the battery pack, then connect the micro:bit via USB or its own battery. Run the test code. If the servo moves smoothly, the setup is successful.
6. Troubleshoot if needed:If the servo jitters, double-check the ground connection. If it does not move, verify the signal wire is on the correct pin and the external battery is charged. If it only moves to extremes, confirm the PWM period is set to 20ms.
Making a micro servo work with a micro:bit is a straightforward process when the fundamental principles of power, ground, and signal are respected. The core solution is always the same: use an external power source for the servo, connect all grounds together, and send a 50Hz PWM signal from a micro:bit pin. By following the wiring diagram and using the verified code examples provided, you can reliably control the position of a micro servo for any project. Start with a simple sweep test, verify your power connections, and you will have a solid foundation for more complex robotic and automation projects.
Update Time:2026-04-02
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.