Professional Documents
Culture Documents
Mechatronics Lab 2
Mechatronics Lab 2
2.
a)Explanation of Pulse Width Modulation (PWM) and Duty Cycle
Pulse Width Modulation, commonly known as PWM, is a technique for controlling power to
devices by rapidly switching them on and off. This creates a series of electrical pulses of
variable duration. The duty cycle of these pulses is the proportion of time they are on
compared to the total period of the PWM signal, expressed as a percentage. This cycle can
vary from 0%, where the signal is never on, to 100%, where it's always on.
d). Comparison of Full Step Drive and Wave Drive in Stepper Motors
Full Step Drive and Wave Drive are two strategies for operating stepper motors. Full
Step Drive energises two coils simultaneously, providing maximum torque but possibly
leading to a less smooth motion. In contrast, Wave Drive energises just one coil at a time.
While this method may produce a smoother movement, it does so at the expense of torque.
Each approach has its applications depending on whether smoother motion or higher torque
is the priority.
e). Please include a labelled hardware diagram of the laboratory setup in
your report. This diagram will be a reference point when discussing the various
components or systems, whether you're using block diagrams or labelled photos.
The list of components for this diagram can be found on the Brightspace platform.
f.) put question here The lab provided a practical understanding of Pulse
Width Modulation (PWM) and Duty Cycle, illustrating how altering the PWM's duty
cycle can control an LED's brightness and modulate sound output from speakers. It
also gave hands-on experience with different components such as the LCD screen,
servo motor, DC motor, and stepper motor, utilizing specific GPIO functions and
methods to operate them. Moreover, the lab session offered a comparative analysis
of various motor driving methods, detailing their operational benefits and limitations.
Appendix A - Experiment Scripts
6.3
6.4 Testing The Effect of PWM Duty Cycle with an LED (with comments)
# Libraries needed: GPIO for LED, sleep from time, lcd_i2c for LCD display
pin.start(0) # sets the starting duty cycle of the PWM signal to 0% and initialises the signal
# main
try:
# repeats until ctrl+c is inputted
while True:
for i in range(0, 101, 20): # 0 to 100%, with 20% increments
pin.ChangeDutyCycle(i) # changes duty cycle to current i value
lcd_i2c.printer('Duty Cycle:', f'{i}%') # display on lcd
sleep(2) # pause for 2 seconds
# resetting components
lcd_i2c.cleanup()
pin.stop()
GPIO.cleanup()
try:
# Set up Raspberry Pi to use the BCM (Broadcom SOC channel) numbering on the
board
GPIO.setmode(GPIO.BCM)
# Define the half step sequence for the stepper motor Go one full rotation
halfstepSequence = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1],
[1, 0, 0, 0]
]
for x in range(50):
for halfstep in range(len(halfstepSequence)):
for pin in range(len(controlPins)):
GPIO.output(controlPins[pin-1], halfstepSequence[halfstep][pin-1])
time.sleep(0.005) # Adjustable for faster or slower speed
time.sleep(0.02)
try:
# Set up Raspberry Pi to use the BCM (Broadcom SOC channel)
numbering on the board
GPIO.setmode(GPIO.BCM)
# Define the half step sequence for the stepper motor Go one full
rotation
halfstepSequence = [
[1, 0, 0, 0],
[1, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 1, 0],
[0, 0, 1, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 0, 0, 1]
]