Servo

The Tower Pro SG90 is a small, 9 gram servo that can run directly from the microbit power supply. This is a 180° servo meaning it can turn from 0° to 180°. It has many applications: open a flap to dispense treats for to a dog, create a robotic walker, or to drive window wipers on a car.

Electronics

Which Wire is Which?

Most servos have three wires: GND, POWER, and SIGNAL.

Wire colours differ based on manufacturer. Here’s the Tower Pro servo; look at the wire colours:

SG90 Servo Wire Colours

Information about the wire colours can be found in the datasheet. This can be found by Googling SG90 datasheet:

SG90 Data Sheet What Coloured Wires?

We now know how to connect the servo:

Servo Wire ColourData Sheet LabelConnect to Microbit Pin
RedVCC (+)3V pin
BrownGround (-)GND pin
OrangePWM (Signal)pin0

The angle of the servo is set by a PWM pulse to the orange wire. This is connected to pin0.

Code

Servos are difficult to control in Python on the microbit. I’ve tried to make it easy below.

A module must be installed to tell Python how to use the servo.

Steps

There are detailed instructions on adding a module to the microbit on this website.

Code

from microbit import *

from servo import Servo

while True:
    Servo(pin0).write_angle(0)
    sleep(200)
    Servo(pin0).write_angle(90)
    sleep(200)
    Servo(pin0).write_angle(180)
    sleep(200)

The angle of the servo is controlled by Servo(pin0).write_angle(30). If you want to control a servo attached to pin1 as well, it would be:

Servo(pin1).write_angle(30)

We can also make this a variable for ease of use:

sv1 = Servo(pin0)
sv2 = Servo(pin1)

sv1.write_angle(180)
sv2.write_angle(0)

There is additional detail about the Servo class on github

Experiment

Share On
Facebook Twitter Google Plus

Published: December 23, 2016 Updated: December 23, 2016

Acknowledgements: Servo teaser & diagram by [Upgrade Industries](https://www.upgradeindustries.com/licensing/)