[go: up one dir, main page]

0% found this document useful (0 votes)
55 views14 pages

Mechatronics Lab 3

Uploaded by

bhagyapatel000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views14 pages

Mechatronics Lab 3

Uploaded by

bhagyapatel000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Mechatronics: Progress Report 3

Course Number - Section : ECOR1044 - B1B


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

2a.

An H-bridge is a circuit design that enables a voltage to be applied in either direction


across a load, such as a motor. This design is named for its resemblance to the
capital letter "H". Comprising four switches that govern the flow of electricity, an
H-bridge alters the polarity of voltage to the motor. By activating two diagonal
switches—one in each pair—it dictates whether the motor spins clockwise or
counterclockwise, thus changing its rotational direction. This versatility is why
H-bridges are favored for managing the operation of DC motors, providing precision
in directional control.

2b.
DC Motor (Equivalent to Heavy Machinery):
Primary Role: Facilitates movement and directionality for large-scale equipment.
Safety Feature: Ensures smooth, reliable action through meticulous motor
management.

Complementary Tool: A rapid-response shutdown mechanism activated during


emergencies.

LED (Status Indicator):


Purpose: Displays operational feedback of the system visually.
Safety Feature: Offers quick recognition of system status and potential issues.

Supplemental Equipment: Indicator lights tailored to specific scenarios, such as


system errors or overheating.

Auditory Signal Device:


Primary Use: Serves as an auditory indicator for events or notifications.
Safety Feature: Delivers critical auditory signals, especially useful in loud
environments.

Complementary Tool: A specialized alarm for extreme or hazardous situations.


Visual Display Interface:
Purpose: Shows real-time status and updates for system operations.
Safety Feature: Allows for the monitoring and acknowledgment of system alerts.

Complementary Tool: An interactive touchscreen for enhanced user interaction and


feedback.

Error Management Mechanism:


Objective: Implements safeguards to handle unexpected scenarios with grace.
Safety Feature: Mitigates severe system failures and facilitates a secure halt or
recovery in case of malfunctions.

Advanced Implementation: A sensor-based monitoring system that detects and


reports unusual operating conditions, like pressure anomalies or temperature
fluctuations.

Incorporating an Error Handling Mechanism:

Let's now integrate a mechanism similar to try/except error handling into our system.

• Mistake Mitigation: Aids in circumventing unintended errors from causing


operational dysfunctions.
• Resilience to Errors: Enhances the system's ability to recover from errors and
proceed with its functions due to resilience mechanisms.

• Diagnostic Simplification: Streamlines the process of diagnosing and rectifying

issues during upkeep.

Additional Protective Measures:


• Proximity Detectors: Deployed to recognize individuals or objects in zones that
pose potential risks.
• Urgent Halt Mechanism: A tailored apparatus structured to immediately halt
electrical power to any kind of machine during a crisis.

• Observation Equipment: Utilised for overseeing and distant observation of the

workplace environment.
2.c

2d.

Laboratory 3 provided an enlightening journey through the components of industrial


systems and their safety considerations. It illuminated the pivotal role of the H-bridge
in dictating the direction of DC motors, which is crucial for practical deployments.
The incorporation of safety mechanisms and the robust try/except error handling
structure, alongside the future potential for further safety device integration and user
interface enhancements like proximity sensors and surveillance cameras,
underscored the need for safety and efficient functionality. Educational takeaways
include the harmonisation of machine control, user engagement, and fault
management. Future development efforts will prioritise enhancing user interaction
and safety device integration. Such experiences are invaluable in curating a skill set
aimed at forging strong and secure industrial automation solutions.

2e.
6.1

The program governs a DC motor to alternate between clockwise and


counterclockwise rotation using a Raspberry Pi, detailed as follows:

● Essential libraries, RPi.GPIO for interacting with the GPIO pins and time for
pauses in the script, are imported.
● The script configures GPIO pins for clockwise (pin1) and counterclockwise
(pin2) motion.
● The Broadcom SOC channel names are set as the reference mode for GPIO
pins.
● Outputs are designated for the motor control pins, initiating them at a low
signal.
● The clockwise motion is handled by setting pin1 to high, and pin2 is similarly
managed for counterclockwise motion.
● A loop orchestrates the motor rotation using the specified pins.
● For RPM adjustment, PWM modulation is implemented via the GPIO.PWM
class.
● Modifications to the time.sleep(1) parameters can alter the motor's
operational duration.
● It is assumed that the motor's connections to the Raspberry Pi are correct; if
not, pin assignments need alterations.

6.2

This code, utilizing a Raspberry Pi and an LCD, facilitates motor and buzzer control
and prompts user input for motor direction. Here's a synopsis:

● Definitions for GPIO setup are established for the motor, LED, buzzer, and LCD
components.
● PWM classes are instantiated to modulate the motor's speed.
● A buzzer PWM object is also created to adjust the buzzer's frequency.
● The try block houses the script's core, initializing the LCD with a greeting and
managing the LED and buzzer based on user commands.
● A loop invites user direction, rotating the motor accordingly and concluding
with a farewell on the LCD upon cessation.
● Invalid inputs trigger a termination message and exit the script.
● After the user sequence, the program deactivates the LED and buzzer, resets
the LCD, and cleans the GPIO setup.
● PWM has been adapted in the provided script for both motor and buzzer,
setting specific duty cycles for each
Appendix A - Experiment Scripts:
6.1 DC MOTOR INTRO

import RPi.GPIO as GPIO


import time

# Pin Mapping
pin1 = 21 # Clockwise pin
pin2 = 20 # Counterclockwise pin

# Setup the GPIO pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(pin1, GPIO.OUT)
GPIO.setup(pin2, GPIO.OUT)

# Initialize the pins


GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)

# Rotate motor clockwise for 1 second


try:
GPIO.output(pin1, GPIO.HIGH)
GPIO.output(pin2, GPIO.LOW)
time.sleep(1)

# Turn off the motor


GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)
time.sleep(3)

# Rotate motor counterclockwise for 1 second


GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin2, GPIO.HIGH)
time.sleep(1)

# Turn off the motor


GPIO.output(pin1, GPIO.LOW)
GPIO.output(pin2, GPIO.LOW)

except KeyboardInterrupt:
# Clean up the GPIO on CTRL+C exit
GPIO.cleanup()

# Clean up the GPIO on normal exit


GPIO.cleanup()

6.2(parts 1 and 2)

import RPi.GPIO as GPIO


import time

# Pin Mapping
in1 = 21

# Setup the GPIO pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)

# PWM Signal Setup


pin = GPIO.PWM(in1, 1000) # set pin 21 as a PWM output, with a
frequency of 1kHz

try:
# Keep the motor running for 5 seconds
pin.start(21)
time.sleep(5)
pin.stop()

except KeyboardInterrupt:
# Stop the PWM and clean up the GPIO pins
GPIO.cleanup()
6.2(3)
import RPi.GPIO as GPIO
import time

# Pin Mapping
in1 = 21
in2 = 20

# Setup the GPIO pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)

# PWM Signal Setup


pin1 = GPIO.PWM(in1, 1000) # set pin 21 as a PWM output, with a
frequency of 1kHz
pin2 = GPIO.PWM(in2, 1000) # set pin 20 as a PWM output, with a
frequency of 1kHz

try:
# Try different duty cycles
for duty_cycle in range(0, 21, 1):
pin1.start(duty_cycle)
time.sleep(0.5)
pin1.stop()

# Turn off the motor


GPIO.output(in1, GPIO.LOW)
time.sleep(3)

except KeyboardInterrupt:
# Stop the PWM and clean up the GPIO pins
GPIO.cleanup()
6.2(4)
import RPi.GPIO as GPIO
import time

# Pin Mapping
in1 = 21
in2 = 20

# Setup the GPIO pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)

# PWM Signal Setup


pin1 = GPIO.PWM(in1, 1000) # set pin 21 as a PWM output, with a
frequency of 1kHz
pin2 = GPIO.PWM(in2, 1000) # set pin 20 as a PWM output, with a
frequency of 1kHz

try:
# Try different duty cycles
for duty_cycle in range(0, 21, 1):
pin1.start(duty_cycle)
time.sleep(0.5)
pin1.stop()

# Turn off the motor


GPIO.output(in1, GPIO.LOW)
time.sleep(3)

for duty_cycle in range(0, 21, 1):


pin2.start(duty_cycle)
time.sleep(0.5)
pin2.stop()

except KeyboardInterrupt:
# Stop the PWM and clean up the GPIO pins
GPIO.cleanup()

6.2(5)
import RPi.GPIO as GPIO
import time
# Pin Mapping
in1 = 23
in2 = 20

# Setup the GPIO pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)

# PWM Signal Setup


pin1 = GPIO.PWM(in1, 1000) # set pin 21 as a PWM output, with a
frequency of 1kHz
pin2 = GPIO.PWM(in2, 1000) # set pin 20 as a PWM output, with a
frequency of 1kHz

try:
# Try different duty cycles
for duty_cycle in range(0, 21, 20):
pin1.start(duty_cycle)
time.sleep(0.5)
pin1.stop()

# Turn off the motor


GPIO.output(in1, GPIO.LOW)
time.sleep(3)

for duty_cycle in range(0, 21, 1):


pin2.start(duty_cycle)
time.sleep(0.5)
pin2.stop()

except KeyboardInterrupt:
# Stop the PWM and clean up the GPIO pins
GPIO.cleanup()

6.2(6)
import RPi.GPIO as GPIO
import time

# Pin assignments for easy identification of components


in1 = 21 # Pin for motor direction 1 (e.g., clockwise)
in2 = 20 # Pin for motor direction 2 (e.g., counterclockwise)
led_pin = 13 # Pin for the LED
buzzer_pin = 13 # Pin for the buzzer, note: it's the same as led_pin,
which is likely an error

# Configure the GPIO pins


GPIO.setmode(GPIO.BCM) # Use Broadcom SOC channel pin
numbers
GPIO.setup(in1, GPIO.OUT) # Configure in1 as an output pin
GPIO.setup(in2, GPIO.OUT) # Configure in2 as an output pin
GPIO.setup(led_pin, GPIO.OUT) # Configure led_pin as an output pin
# Same pin is being set up twice, which is redundant and should be
addressed in the actual setup
GPIO.setup(buzzer_pin, GPIO.OUT) # Configure buzzer_pin as an
output pin

# PWM configuration for motor control and buzzer


pin1 = GPIO.PWM(in1, 1000) # Initialize PWM on in1 at 1 kHz
pin2 = GPIO.PWM(in2, 1000) # Initialize PWM on in2 at 1 kHz
pin_buzzer = GPIO.PWM(buzzer_pin, 600) # Initialize PWM on
buzzer_pin at 600 Hz

try:
# Test buzzer for 1 second at 50% duty cycle
pin_buzzer.start(50) # Start buzzer
time.sleep(1) # Wait for 1 second
pin_buzzer.stop() # Stop buzzer

# Turn on the LED


GPIO.output(led_pin, GPIO.HIGH) # Set led_pin high, turning the
LED on

# Cycle through duty cycles for pin1 (motor direction 1)


for duty_cycle in range(0, 101, 20):
pin1.start(duty_cycle) # Apply the current duty cycle to the motor
time.sleep(0.5) # Wait for 0.5 seconds
pin1.stop() # Stop PWM on pin1

# Turn off the motor and LED


GPIO.output(in1, GPIO.LOW) # Set motor direction 1 pin low
GPIO.output(in2, GPIO.LOW) # Set motor direction 2 pin low
GPIO.output(led_pin, GPIO.LOW) # Set led_pin low, turning the LED
off
time.sleep(3) # Wait for 3 seconds

# Cycle through duty cycles for pin2 (motor direction 2)


for duty_cycle in range(0, 101, 20):
pin2.start(duty_cycle) # Apply the current duty cycle to the motor
time.sleep(0.5) # Wait for 0.5 seconds
pin2.stop() # Stop PWM on pin2

except KeyboardInterrupt:
# Cleanup on keyboard interrupt (Ctrl+C)
GPIO.output(led_pin, GPIO.LOW) # Ensure LED is turned off
GPIO.output(buzzer_pin, GPIO.LOW) # Ensure buzzer is turned off
GPIO.cleanup() # Reset GPIO resources

6.2(7)
import RPi.GPIO as GPIO
import time

# Define the GPIO pins that will be used for the motor, LED, and buzzer
in1 = 21 # Pin for motor direction 1 (clockwise)
in2 = 20 # Pin for motor direction 2 (counterclockwise)
led_pin = 23 # Pin for the LED
buzzer_pin = 13 # Pin for the buzzer

# Initialize the GPIO pins for output


GPIO.setmode(GPIO.BCM) # Use Broadcom pin-numbering scheme
GPIO.setup(in1, GPIO.OUT) # Motor pin 1 set as output
GPIO.setup(in2, GPIO.OUT) # Motor pin 2 set as output
GPIO.setup(led_pin, GPIO.OUT) # LED pin set as output
GPIO.setup(buzzer_pin, GPIO.OUT) # Buzzer pin set as output

# Set up PWM on the pins


pin = GPIO.PWM(in1, 1000) # Initialize PWM on in1 at 1 kHz
pin2 = GPIO.PWM(in2, 1000) # Initialize PWM on in2 at 1 kHz
pin_buzzer = GPIO.PWM(buzzer_pin, 600) # Initialize PWM on
buzzer_pin at 600 Hz

# Begin the control loop


try:
pin_buzzer.start(50) # Start the buzzer at 50% duty cycle
time.sleep(1) # Keep the buzzer on for 1 second
pin_buzzer.stop() # Stop the buzzer

GPIO.output(led_pin, GPIO.HIGH) # Turn on the LED

# Main loop for user input


while True:
# Get user input to control the direction of the motor
userinput1 = input("Enter a direction (Clockwise, Counterclockwise,
Stop): ")

if userinput1 == "clockwise": # If user chooses clockwise rotation


pin.start(21) # Start PWM for clockwise rotation
time.sleep(5) # Keep it running for 5 seconds
pin.stop() # Stop the motor

elif userinput1 == "counterclockwise": # If user chooses


counterclockwise
pin2.start(21) # Start PWM for counterclockwise rotation
time.sleep(5) # Keep it running for 5 seconds
pin2.stop() # Stop the motor

elif userinput1 == "stop": # If user wants to stop the motor


break # Exit the loop

else: # If user enters an invalid command


print("Invalid Input, please try again.") # Notify the user

# If loop is exited, turn off all components


GPIO.output(in1, GPIO.LOW) # Make sure motor pin 1 is off
GPIO.output(in2, GPIO.LOW) # Make sure motor pin 2 is off
GPIO.output(led_pin, GPIO.LOW) # Make sure the LED is off

except KeyboardInterrupt: # Allow user to exit the script with CTRL+C


GPIO.output(led_pin, GPIO.LOW) # Turn off the LED
GPIO.output(buzzer_pin, GPIO.LOW) # Turn off the buzzer

finally:
GPIO.cleanup() # Clean up the GPIO pins when done

6.2(8)

import time
import RPi.GPIO as GPIO
import lcd_i2c as lcd

#General GPIO Setup


GPIO.setmode(GPIO.BCM) #sets how we reference GPIO pins
GPIO.setup(21, GPIO.OUT) #sets GPIO pin 21 as an output
GPIO.setup(20, GPIO.OUT) #sets GPIO pin 20 as an output.
GPIO.setup(23, GPIO.OUT) #sets GPIO pin 23 as an output.
GPIO.setup(13, GPIO.OUT) #sets GPIO pin 13 as an output.

#PWM Signal Setup


pin1 = GPIO.PWM(21,1000) #set pin 21 as a PWM output, with a
frequency of 1000 Hz
pin2 = GPIO.PWM(20,1000) #set pin 20 as a PWM output, with a
frequency of 1000 Hz
pin3 = GPIO.PWM(23,500) #set pin 23 as a PWM output, with a
frequency of 500 Hz
pin4 = GPIO.PWM(13,600) #set pin 13 as a PWM output, with a
frequency of 600 Hz
pins = [pin1, pin2, pin3, pin4]
for pin in pins:#sets the starting duty cycle of the PWM signal to 0% and
initializes the signal
pin.start(0)
time.sleep(1) #sleep for a second to ensure signal is initialized properly

lcd.printer("Hello", "") #prints hello


time.sleep(2)
lcd.printer("","")

try:

while True:
userinput = input("Enter clockwise/counterclockwise/stop:") #take
user input

if userinput == "clockwise": #if clockwise:


lcd.printer("CW", "") #print cw
pin4.ChangeDutyCycle(50) #turn on buzzer
time.sleep(1)
pin4.ChangeDutyCycle(0) #turn off buzzer
pin3.ChangeDutyCycle(50) #turn on led
for i in range(22): #ramp speed
pin1.ChangeDutyCycle(i)
time.sleep(0.5)
pin3.ChangeDutyCycle(0) #turn off led
pin1.ChangeDutyCycle(0) #turn off dc motor
lcd.printer("Goodbye", "") #print goodbye
time.sleep(2)
lcd.printer("","")
time.sleep(1)
elif userinput == "counterclockwise":
lcd.printer("CCW", "") #print cw
pin4.ChangeDutyCycle(50) #turn on buzzer
time.sleep(1)
pin4.ChangeDutyCycle(0) #turn off buzzer
pin3.ChangeDutyCycle(50) #turn on led
for i in range(22): #ramp speed
pin2.ChangeDutyCycle(i)
time.sleep(0.5)
pin3.ChangeDutyCycle(0) #turn off led
pin2.ChangeDutyCycle(0) #turn off dc motor
lcd.printer("Goodbye", "") #print goodbye
time.sleep(2)
lcd.printer("","")
time.sleep(1)

elif userinput == "stop":


break
else:
print("Invalid input, try again later")
break

except KeyboardInterrupt:
pass

pin1.stop()
pin2.stop()
GPIO.cleanup()
lcd.cleanup()

You might also like