PC/CP320 Physical Computing
Raspberry Pi Servo Motors
Overview
Servo motors are common.
Objectives
- To introduce control of servo motors with the Raspberry Pi
Background
The Raspberry Pi has a PWM output built-in which can help with
servo motor control.
Preparation
Introduction to Servos
Equipment
- Raspberry Pi
- TTL-serial cable
- Oscilloscope
- Servo motor
(SG-90)
Procedure
-
Datasheet Examination
Look at the
datasheet
to determine the required supply voltage.
Note: The Pi power pin may not provide enough power for the
motor. If that is the case then use the bench supply, but make
sure your bench ground and the Pi ground are connected.
Since the control signal is going from the Pi
to the motor, is there any danger to the Pi if the motor
supply voltage is above 3.3V?
Note that the servo motor internal electronics protect whatever
is controlling it from EMF caused by the motor.
Given that and the
answer to the previous question,
will an optoisolator be required to control this from the Pi?
-
Identify which wire has which function:
- power
- ground
- control signal
-
To drive the motors, a pulse width modulated signal must be
sent periodically. The width of the pulse will indicate
direction and speed:
- pulse width of 0, i.e. no pulse -- the motor will stop
- pulse width < some value -- the motor will rotate in one
direction
- pulse width = some value --
the motor is in the neutral position
- pulse width > some value -- the motor will rotate in the
other direction
- the drive signal is proportional,
so the farther it is from the neutral
position, the greater the rotation
- NOTE: specified neutral is approximate, you must calibrate
to get precise control
From the data sheet, what is the required period for
the pulses?
What frequency does that correspond to for your PWM
signal?
From the data sheet, what is the approximate pulse width
for the neutral position?
What duty cycle does that correspond to for your PWM
signal?
From the data sheet, what is the approximate pulse width for
the limit in one direction?
What duty cycle does that correspond to for your PWM signal?
From the data sheet, what is the approximate pulse width for
the limit in the other direction?
What duty cycle does that correspond to for your PWM signal?
-
Pulse Width Creation and Testing
Start with the pwm_test_2018.py program that you
used previously. Identify which PWM GPIO pin it uses.
Connect the oscilloscope to the PWM GPIO
pin.
Don't connect the PWM pin to the servo
motor yet.
Modify the program to create a PWM signal of the required
frequency and duty cycle for the neutral position as
determined above.
Now find the required duty cycles to give you the upper and
lower limits as determined above.
Demonstrate the program giving the correct signals
on the oscilloscope.
-
Motor Calibration
The values just determined will be approximate; each motor
will vary slightly.
Now connect the PWM pin to the motor.
If you have done the previous part
correctly, you should need no change to the frequency, and
only small changes to the duty cycles.
Now, use the program to find the duty cycle which
keeps the servo motor in the neutral position;
do not assume that the value in the
datasheet is precise.
If the motor goes to one end and
buzzes, it means you are overdriving it. Stop the program
and figure out what is wrong before procedding!
Was it close to the expected value?
Use the program to find the
duty cycle which rotates the motor to the limit in one
direction.
Was it close to the expected value?
Use the program to find the
duty cycle which rotates the motor to the limit in the other
direction.
Was it close to the expected value?
Determine the angular resolution limit of your program. In
other words, how small a change in angle can you
consistlently produce?
Demonstrate the program putting the motor in each of
the neutral, left, and right limits.
-
Program Refinement
Modify your program so that you input the
desired angle
of rotation in degrees.
Modify your program so that, when you quit, the motor
will be returned to the neutral position.
-
Demonstrate your circuit and program to the lab supervisor.
Wilfrid Laurier University
© 2019 Wilfrid Laurier University