This is a simple class for controlling servos on the microbit in Python.
There are two ways:
Cut and paste the class in servo.py to the top of the program.
See here for an example.
The correct approach is to copy the module to the filesystem. It can then be accessed in the same way the microbit
module is imported at the start of each program.
The are two steps: copying the module to the microbit and importing the module into your program.
-
Save the module to your computer.
-
Copy the downloaded module to the
/mu_code/
directory in the root of your home directory. -
Flash your program to mu.
-
An error message will scroll across the screen about the lack of the servo module.
-
Once it has finished, click the 'files' icon in mu and upload the
servo.py
file to your microbit. -
Press reset on your microbit. When the program runs again it will load the module.
from microbit import *
# from servo.py import the Servo class
from servo import Servo
# this can now be accessed within your program
sv1 = Servo(pin0)
sv1.write_angle(50) # turn servo to 50 degrees
sv1 = Servo(pin0)
sv1.write_angle(50) # turn servo to 50 degrees
note how min_us and max_us changes. These details will be on the servo's datasheet.
sv1 = Servo(pin0, min_us=1000, max_us=2000)
sv1.write_angle(180) # turn servo to 180 degrees
sv1 = Servo(pin1, min_us=750, max_us=2250)
sv.write_angle(10)
Code from The Sheep on Bitbucket and made compatable (and worse) by me. Rescale function by Tom Viner