Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Mechatronics: Progress Report 2

Course Number - Section : ECOR1044 - B1B


Group Number : 1044-D2-25
Date Performed : March 11 2024
Date Submitted 2024

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.

b). Steps to Adjust LED Brightness Using PWM


To alter the luminosity of an LED, one can utilise PWM. By adjusting the duration of the
on-state within the PWM signal, you can dim or brighten the LED. This is accomplished by
changing the value of duty_cycle in the pin.ChangeDutyCycle(duty_cycle) method, with
duty_cycle being any integer from 0 to 100. This modification of the duty cycle effectively
alters the LED's perceived brightness.

c). Effects of PWM on a Speaker's Sound Output


When applying PWM to a speaker, altering the signal's frequency affects the pitch of the
sound produced, whereas modifying the duty cycle influences the volume. With PWM, not
only can you vary the loudness by shifting the duty cycle between, for example, 25% and
50%, but you must also adjust the frequency to maintain the desired pitch. A decrease in
duty cycle leads to a reduction in volume, whereas an increase results in a louder output.
Therefore, transitioning from a 25% to a 50% duty cycle would amplify the sound.

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.2 BasicPWMLED.py (edited script for Q4, with comments)

from time import sleep


import lcd_i2c#libraries needed: lcd_i2c for LCD display, sleep from time

lcd_i2c.lcd_init() #initialise lcd display


try:
lcd_i2c.printer('Welcome','')#print welcome
sleep(2)
lcd_i2c.printer('Goodbye','')#print goodbye
sleep(2)#2 sec pause in between each print
except KeyboardInterrupt:#stops try if (ctrl + c) is pressed
pass

lcd_i2c.cleanup() #resets the lcd after usage

6.3

6.4 Testing The Effect of PWM Duty Cycle with an LED (with comments)

import RPi.GPIO as GPIO


from time import sleep
import lcd_i2c

# Libraries needed: GPIO for LED, sleep from time, lcd_i2c for LCD display

# General lcd setup


lcd_i2c.lcd_init()
# General GPIO Setup
GPIO.setmode(GPIO.BCM) # sets how we reference GPIO pins
GPIO.setup(23, GPIO.OUT) # sets GPIO pin 23 as an output

# PWM Signal Setup


pin = GPIO.PWM(23, 50) # sets pin 23 as a PWM output, with a frequency of 50Hz

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

except KeyboardInterrupt: # stops try if ctrl + c is pressed


lcd_i2c.printer('Cleaning up...', '')
print("stopped")
sleep(2)

# resetting components
lcd_i2c.cleanup()
pin.stop()
GPIO.cleanup()

6.5 Stepper Motor Demonstration - task 1


import RPi.GPIO as GPIO
import time
import lcd_i2c

# Set warnings to false to avoid unnecessary warnings


GPIO.setwarnings(False)

try:
# Set up Raspberry Pi to use the BCM (Broadcom SOC channel) numbering on the
board
GPIO.setmode(GPIO.BCM)

# Define the control pins for the stepper motor


controlPins = [21, 20, 12, 16]
# Set up the output pins to control the stepper
for pin in controlPins:
GPIO.setup(pin, GPIO.OUT)

# 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)

except KeyboardInterrupt: # If interrupted with ctrl+c it breaks out of the try


statement
pass

# Clean up GPIO and LCD


GPIO.cleanup()
lcd_i2c.cleanup()

6.5 Stepper Motor Demonstration - Task 2


import RPi.GPIO as GPIO
import time
import lcd_i2c

# Set warnings to false to avoid unnecessary warnings


GPIO.setwarnings(False)

try:
# Set up Raspberry Pi to use the BCM (Broadcom SOC channel)
numbering on the board
GPIO.setmode(GPIO.BCM)

# Define the control pins for the stepper motor


controlPins = [21, 20, 12, 16]

# Set up the output pins to control the stepper


for pin in controlPins:
GPIO.setup(pin, GPIO.OUT)

# 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]
]

for x in range(3): # It needs 50 cycles through the halfstepSequence


for a full rotation
for x in range(12):
for halfstep in range(len(halfstepSequence)):
for pin in range(len(controlPins)):
GPIO.output(controlPins[pin-1],
halfstepSequence[halfstep][pin-1])
time.sleep(0.01) # Adjustable for faster or slower speed
time.sleep(1)

# Rotate back in the other direction


for x in range(36): # It needs 50 cycles through the halfstepSequence
for a full rotation
for halfstep in reversed(range(len(halfstepSequence))):
for pin in range(len(controlPins)):
GPIO.output(controlPins[pin- 1], halfstepSequence[halfstep][pin
- 1])
time.sleep(0.02) # Adjustable for faster or slower speed
#lcd display
lcd_i2c.printer('Finished','Enjoy :)')
sleep(2)

except KeyboardInterrupt: # If interrupted with ctrl+c it breaks out of the


try statement
pass

# Clean up GPIO and LCD


GPIO.cleanup()
lcd_i2c.cleanup()
References

1] Michael Feuerherm, ECOR1044 - Mechatronics Laboratory #2,


Carleton University.
[Online]. Available: ECOR1044 - Lab 2 Instructions - Merge
ECOR1044C:ECOR1044D
Mechatronics (LEC) [12148:12161] Winter 2024 (carleton.ca)
[2] Dejan, “How a stepper motor works,” How To Mechatronics. [Online].
Available:
How a Stepper Motor Works - How To Mechatronics
[3] B. Leonov, S. Kristoff, A. Brown, “PWM Sound Synthesis,” PROJECT
HUB. [Online].
Available: PWM Sound Synthesis - Arduino Project Hub

You might also like