Skip to content

Latest commit

 

History

History
125 lines (83 loc) · 3.7 KB

servo.md

File metadata and controls

125 lines (83 loc) · 3.7 KB
title layout meta-description share author about cats simple-description acknowledgements date date-updated
Using a servo with the microbit
text-width-sidebar
Use a servo in python with the microbit.
true
jez
Use a servo in python and PXT.
external
Servo
Servo teaser & diagram by [Upgrade Industries](https://www.upgradeindustries.com/licensing/)
2016-12-23 10:20:00 UTC
2016-12-23 10:20:00 UTC

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.

{:.ui .dividing .header}

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{:.ui .image .centered}

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?{:.ui .image .centered}

We now know how to connect the servo:

{:.ui .very .basic .small .table} | Servo Wire Colour | Data Sheet Label | Connect to Microbit Pin | | Red | VCC (+) | 3V pin | | Brown | Ground (-) | GND pin | | Orange | PWM (Signal) | pin0 |

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

{:.ui .dividing .header}

Code

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

Python MS Blocks

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

Steps

  • Download the Servo class and save it in the /mu_code directory in your home folder.
  • Upload the code below to your microbit.
  • Upload the servo.py file to the microbit within mu.

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

Code

{% highlight python %}

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)

{% endhighlight %}

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:

{% highlight python %} Servo(pin1).write_angle(30) {% endhighlight %}

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

{% highlight python %} sv1 = Servo(pin0) sv2 = Servo(pin1)

sv1.write_angle(180) sv2.write_angle(0) {% endhighlight %}

There is additional detail about the Servo class on github

Experiment