CSE 461 LAB Sheet
CSE 461 LAB Sheet
Lab 2
Introducing servo motor using along with push buttons and LEDs with
Raspberry Pi
I. Topic Overview:
The lab is designed to introduce the students to get the basic idea on motor about how
they function. Students will be given an introduction on servo motors, duty cycle and
working mechanism. Also they will incorporate the last lab’s task which was control
LED with a push button.
Here, you will be able to learn the functioning of each pin, which helps you to do things on your
Raspberry Pi 4 easily. There are 40 pins in this model and among them 26 are GPIO pins.
Page 2 of 9
Servo Motor :
Wire Configuration
After selecting the right Servo motor for the project, comes the question of how to use it. As we
know there are three wires coming out of this motor. The description of the same is given in the
previous section. To make this motor rotate, we have to power the motor by connecting the Red
Page 3 of 9
wire to +5V DC power supply, Brown wire to the Ground and sending the PWM signals to the
Orange colored wire. Hence we need something that could generate PWM signals to make this
motor work, this something could be anything like a 555 Timer or other Microcontroller
platforms like Arduino, PIC, ARM or even a microprocessor like Raspberry Pi. Now, how to
control the direction of the motor? To understand that let us look at the picture :
From the picture we can understand that the PWM signal produced should have a frequency of
50Hz, that is the PWM period should be 20ms.
The position of the servo depends on the “ON” part of the cycle. The “ON” time can vary from
1ms to 2ms. So when the “ON” time is 1ms the motor will be at 0° (fully right position) and
when 1.5ms the motor will be 90° (center position), similarly when it is 2ms it will be 180°(fully
left position). So, by varying the “ON” time from 1ms to 2ms the motor can be controlled from
0° to 180°.
The “ON” time can also be represented in terms of Duty Cycle. The Higher the Duty Cycle of a
signal, the greater the “ON” time of that signal.
DutyCycle = PulseWidth*frequency
To sum up, to move the servo full left we need a Duty Cycle of 10%, for the center position a
Duty Cycle of 7.5%, and for the full right position a Duty Cycle of 5%. This is all based on a 50
Hz signal. Intermediate values would linearly scale between these values.
Page 4 of 9
The values of the Duty Cycles stated above may vary slightly from one servo to another. Thus, in
the experimentation to test whether these values of duty cycles are the same for our servo or not,
we found out that for our servo:
● A Duty Cycle of 12% yielded the full left position (180 degrees)
● A Duty Cycle of 7% yielded the center position (90 degrees)
● A Duty Cycle of 2% yielded the full left position ( 0 degree)
Now, how do we find the Duty Cycle for our desired angle? For example, what will be the Duty
Cycle if we want the servo to rotate 60 degrees?
For that, we can linearly calibrate our duty cycle from 2% ( 0 degree, full left position) to 12% (
180 degree, full right position), so these 2 extreme points can be written in the coordinate system
form like this : (0,2) and (180,12)
with these two points we can calculate the slope of the line:
Next, we will be representing the relationship between duty cycle and angle in the form of an
equation, so finding the equation:
y - y1 = m (x - x1)
y-2 = 1/18 (x - 0)
so the relation between duty cycle and the angle can be represented in the following way:
y = 1/18x + 2
Page 5 of 9
Therefore, for rotating the servo to 60 degrees, we can calculate the duty cycle in the following
way:
= 5.3 %
● First, connect the power wire of the servo (red for us), to a 5v power pin of the raspberry
pi (Pin 2 or 4).
● Then connect the ground wire of the servo (brown), to any ground pin of the raspberry pi.
● Lastly, connect the pwm wire of the servo (orange), to any GPIO pin of the raspberry pi.
We will use the GPIO pin number in the code below.
*(First go to the Terminal and type: sudo pigpiod, then open the python IDE and write the code.)
Device.pin_factory = PiGPIOFactory()
s = AngularServo(18,min_angle = 0, max_angle =
180,min_pulse_width=0.5/1000,max_pulse_width = 25/10000)
while True:
sleep(1)
#right
sleep(1)
Page 6 of 9
Part II: Controlling an LED with the help of a push button
For controlling the LED with a push button on the Raspberry Pi 4, we need the following
electronic components:
● Raspberry Pi 4
● Servo Motor
● LED
● A resistor of 220 ohms
● Push-button
● Connecting wires (female to male)
Page 7 of 9
To start the pigpio daemon
sudo pigpiod
To stop the pigpio daemon
sudo killall pigpiod
The code:
Device.pin_factory = PiGPIOFactory()
s = AngularServo(17,min_angle = 0, max_angle =
180,min_pulse_width=0.5/1000,max_pulse_width = 25/10000)
while True:
button1.wait_for_press()
s.angle=120# (120 degree to the left)
led.on()
sleep(1)
led.off()
#right
button2.wait_for_press()
s.angle=60 # 60 degree to the right
led.on()
sleep(1)
led.off()
Code with duty cycle control:
import RPi.GPIO as GPIO
from time import sleep
Page 8 of 9
from gpiozero import LED
button1= Button(27)
button2= Button(22)
led1=LED(4)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
pwm=GPIO.PWM(17,50)
pwm.start(7) #center(90 degrees)
while True:
button1.wait_for_press()
pwm.ChangeDutyCycle(5.3) # (left)
led1.on()
sleep(1)
led1.off()
button2.wait_for_press()
pwm.ChangeDutyCycle(8.7) # right
led1.on()
sleep(1)
led1.off()
Lab Task:
Now your task is to update the circuit and modify the code in such a way that you can implement
following task:
● Implement 2 LED
● Press Button_A and one LED will turn on and servo motor will rotate for a specific angle
● Press Button_B and another LED will turn on and servo motor will rotate for a specific
angle
Page 9 of 9