Another Python tutorial made for the accessory pack.
Today we’ll make a small program for the SG90 servomotor included in the pack. Feel free to re-use this program and enrich it as you see fit.
Before starting : Tools installation
This tutorial is made using the Mu IDE for CircuitPython.
If you haven’t installed those yet please refer yourself to the installation guide on our website.
Plugging things in
In addition to the code we need to plug the servomotor to the console, is possible using the backpack included in the accessory pack.
The power input (VCC, RED wire) goes to the VBAT pin, the ground (GND, BROWN wire) goes to a ground pin and the command entry (ORANGE wire) goes to an analog pin, here the A2 pin.
Step 1 : The program’s basis
Start by opening Mu and creating a new file.
Copy the following code :
from gamebuino_meta import begin, waitForUpdate, display, buttons, color
import board
import pulseio
while True:
waitForUpdate()
display.clear()
This will import the Gamebuino libraries needed for the program, as well as a pulseio library we will use to work with the servomotor.
The part beginning with “while True:” is the main loop of our program, without it nothing would happen.
Step 2 : Configure the servomotor
To be able to use the servomotor as we like we first need to configure it, making it a contextual object within our program. For this we will use the pulseio library we added to our program.
The first step is to create a servomotor object. We declare it as follows, just under our libraries inclusions :
servo = pulseio.PWMOut(board.A2, frequency=50, duty_cycle=0)
this way we “attach” the command entry of the servomotor to our A2 pin, in output mode, with a frequency of 50 and a cycle of 0.
The proper “command” of the servo will be made by modifying the duty_cycle parameter of out servo object.
To simplify this process we will add a function to do all the calculation needed at once, which will make each position change easier.
Beware, the functions looks like this :
def servo_duty_cycle(pulse_ms, frequency=50):
period_ms = 1.0 / frequency * 1000.0
duty_cycle = int(pulse_ms / (period_ms / 65535.0))
return duty_cycle
We simply define a value for duty_cycle that is calibrated on our 50 hertz frequency. this value is expressed in milliseconds and scaled on 65535, the upper limit of a 16 bits integer.
We can now define a few basic parameters we will use for the initial state of out program :
x = 0.4
angle = 0
servo.duty_cycle = servo_duty_cycle(x)
Here the x is the servomotor’s position, 0.4 being equal to a 0° angle, the angle variable is simply the angular value of the servomotor and the servo.duty_cycle is used to reset the position to 0 each time the program starts.
Step 3 : Piloting the servomotor
Here we will enrich our main loop to issue commands to the servomotor using the console’s D-pad.
Let’s begin by the right button, which will increment the servo’s position by changing the angle by +9° :
while True:
waitForUpdate()
display.clear()
if buttons.pressed(buttons.RIGHT) and (x <= 2.5):
x += 0.1
servo.duty_cycle = servo_duty_cycle(x)
angle += 9
We follow this with the left button that decrements the position by -9° :
x = 0.4
angle = 0
servo.duty_cycle = servo_duty_cycle(x) # 7536
while True:
waitForUpdate()
display.clear()
# right button movement
if buttons.pressed(buttons.LEFT) and (x >= 0.4):
x -= 0.1
servo.duty_cycle = servo_duty_cycle(x)
angle -= 9
Then the up and down buttons will jump the position to 0 for the UP button and 180 for the DOWN button :
while True:
waitForUpdate()
display.clear()
# right button movement
# left button movement
if buttons.pressed(buttons.UP):
x = 0.4
servo.duty_cycle = servo_duty_cycle(x) # 1638
angle = 0
if buttons.pressed(buttons.DOWN):
x = 2.5
servo.duty_cycle = servo_duty_cycle(x) # 7536
angle = 180
We finish with a simple displaying of the current angle on the console’s screen :
while True:
waitForUpdate()
display.clear()
# buttons movement
display.setFontSize(2)
display.print(angle)
display.setFontSize(1)
display.print("o")
And voilà ! A simple and quick tutorial to pilot your servomotor using Python. Now it’s yours to play with.