LAB 03
Name of the Experiment: Introducing servo motor using along with Ultrasonic Sensor
with Raspberry Pi
CSE461, section 02
Group 1:
Rikth Humayun
22101895
Mohammad Latif Mozammel
21201139
Rayian Rafiz Anoy
24141230
Amlan Dutta
20301033
Nijaf Md. Ahanaf Rivan
21301339
Objective:
The primary aim of this lab was to design and implement an automated sorting system
for a conveyor belt in a smart warehouse. The system combined an ultrasonic sensor
to measure distances and detect the proximity of objects and a servo motor to adjust
the position of a deflector arm based on the detected distance, thus enabling sorting
operations. This experiment focused on integrating previous knowledge of ultrasonic
sensors with servo motor control, using Raspberry Pi to program GPIO pins and
generate precise PWM signals.
Equipment:
The experiment required the following hardware components:
●
●
●
●
●
●
Raspberry Pi 4
Servo Motor (SG90)
Ultrasonic Sensor (HC-SR04)
Breadboard
Jumper Wires
220-ohm Resistors
● microSD card, keyboard, mouse etc.
Experimental Setup:
1. Ultrasonic Sensor Configuration:
● Firstly, we connected The Trigger pin of the HC-SR04 sensor to GPIO21 (Pin 40)
of the Raspberry Pi, responsible for initiating the ultrasonic pulse.
● The Echo pin was connected to GPIO20 (Pin 38) via a voltage divider circuit to
reduce the signal to 3.3V, ensuring compatibility with Raspberry Pi's GPIO pins
and preventing damage.
● The VCC and Ground pins of the sensor were connected to Pin 04 (5V) and Pin
06 (Ground), respectively.
● The sensor was programmed to calculate distances by timing the duration of the
ultrasonic wave's round trip.
2. Servo Motor Configuration:
● The red power wire of the SG90 servo motor was connected to the 5V power pin
of the Raspberry Pi.
● The brown ground wire was connected to a ground pin.
● The orange signal wire was connected to GPIO17, where PWM signals were
generated to control the motor's angle of rotation.
Code:
import RPi.GPIO as GPIO
import time
from gpiozero.pins.pigpio import PiGPIOFactory
from gpiozero import Device, Servo, AngularServo
Device.pin_factory = PiGPIOFactory()
GPIO.setmode(GPIO.BCM)
TRIG = 21
ECHO = 20
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
servo = AngularServo(17,min_angle = 0, max_angle = 180,
min_pulse_width=0.5/1000, max_pulse_width=25/10000)
def distance():
count = 0
GPIO.output(TRIG, False)
time.sleep(0.5)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
pulse_start = time.time()
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
count+=1
if count>100:
break
count = 0
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
count+=1
if count>100:
break
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance,2)
return distance
while True:
d = distance()
print(d)
if d<10:
servo.angle = 0
elif d>10 and d<20:
servo.angle = 45
elif d>20 and d<30:
servo.angle = 90
elif d>30 and d<40:
servo.angle = 135
else:
servo.angle = 180
time.sleep(0.5)
GPIO.cleanup()
Results:
The ultrasonic sensor successfully measured distances in real-time and
displayed the values on the console. The servo motor adjusted its deflector arm
based on the measured distances, followed by:
●
●
●
●
●
< 10 cm: Servo moved to 0°.
10–20 cm: Servo moved to 45°.
20–30 cm: Servo moved to 90°.
30–40 cm: Servo moved to 135°.
> 40 cm: Servo moved to 180°.
This demonstrated a fully functioning automated sorting system.
Discussions:
1. Pulse Width Modulation (PWM) is a technique that modifies the length of the "on"
time of a periodic signal to regulate the angular position of a servo motor. Several
cycles with a constant frequency make up the PWM signal. Two sections make
up each cycle:
On time: During a strong signal.
Off time: When there is less signal.
The "on" time's pulse width establishes the servo motor's angular location. Servo
position can be precisely controlled by varying the "on" time (pulse width). The
resulting proportional angles range from 0° to 180° for intermediate pulse widths.
The smooth operation of the servo depends on generating PWM signals with
minimal jitter, achieved by using reliable libraries like PiGPIOFactory.
2. The relationship between the servo motor's desired angle and the required duty
cycle is given by the formula:
Duty Cycle = { (1/18) * Angle} + 2
For a desired angle of 80°, the duty cycle is calculated as:
Duty Cycle = { (1/18) * 80} + 2
Duty Cycle = 6.44%
The servo arm is positioned at 80° with the computed duty cycle of 6.44%, which
translates to a pulse width of 1.288 ms. This demonstrates that there is a direct
and linear relationship between the duty cycle and the angle. The servo's location
can be proportionally adjusted by varying the duty cycle.
The duty cycle is a measure of how much of the PWM cycle the signal stays
"on." A longer "on" period and, hence, a greater angle for the servo motor are
associated with a higher duty cycle. The servo may reach any position between
0° and 180° by adjusting the duty cycle between 2% and 12%.