Mechatronics: Progress Report #3
Ecor1044B: Mechatronics Group: 22
Group Member 1: Kian Dowlatshahi – 101351030
Group Member 2: Rudra Rana - 101339594
Performed on: 21/11/2024
Submitted on: 28/11/2024
Parts Used
Stepper motor – A stepper motor converts electrical pulses into precise mechanical rotation or
motion. The motor’s shaft rotates in steps or angles and the number of steps it takes and the
amount of time between each step can be set.
LCD – An LCD stands for a liquid crystal display. It is a flat panel that uses crystals to display images
(or in the case of our lab) some characters, punctuation or numbers.
Lab Summary
The objective of this lab is to learn how to control a stepper motor and understand the use of an
LCD display to show a robot’s status. We will work with Raspberry Pi GPIO pins to control a status
LED and explore how to display real-time feedback on an LCD screen. The only equipment used in
this lab was the LCD and the stepper motor.
Outline / Process
First me and my partner Rudra Rana entered the lab and turned on the computer. Then we logged
in, turned on the virtual simulation and opened the lab report and began to follow its steps. We
created and saved a new file and copied the code from section 1.1 and pasted it into Thonny. Once
we ran the code I recorded a video of the code successfully running then moved onto section 1.2.
We created a new Thonny file, pasted the code into the new file, saved it and ran it. While running it
we recorded a video to record the code successfully working. Once both of these were achieved we
checked out and logged out of the computer.
Challenges:
During this lab me and my partner really didn’t experience any challenges due to the reduced length
of this lab. It had only ended up taking around 5 minutes and mostly revolved around copying code
from the lab to the Virtual machine.
1.1Stepper Motor
Stepper motor (before and after rotation)
Code:
# Import the necessary libraries
import RPi.GPIO as GPIO # GPIO library to control the pins
import time # Time library for controlling delays between steps
# Define GPIO pins for the stepper motor (each pin controls one coil of the motor)
out1 = 17 # GPIO pin 17 connected to the first coil of the stepper motor
out2 = 22 # GPIO pin 22 connected to the second coil of the stepper motor
out3 = 27 # GPIO pin 27 connected to the third coil of the stepper motor
out4 = 10 # GPIO pin 10 connected to the fourth coil of the stepper motor
# Define the time between each step (in seconds) and the number of steps to rotate
step_sleep = 0.02 # 20 milliseconds delay between each step
step_count = 100 # Number of steps the motor should take
# Setting up the GPIO mode to BCM (Broadcom pin numbering)
GPIO.setmode(GPIO.BCM) # Setting up the GPIO pins as output pins
GPIO.setup(out1, GPIO.OUT)
GPIO.setup(out2, GPIO.OUT)
GPIO.setup(out3, GPIO.OUT)
GPIO.setup(out4, GPIO.OUT)
# Initializing the GPIO pins to a LOW state (turning off all motor coils)
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.LOW)
# Function to clean up and reset the GPIO pins to LOW state before ending the program
def cleanup():
GPIO.output(out1, GPIO.LOW) # Turn off the first coil
GPIO.output(out2, GPIO.LOW) # Turn off the second coil
GPIO.output(out3, GPIO.LOW) # Turn off the third coil
GPIO.output(out4, GPIO.LOW) # Turn off the fourth coil
GPIO.cleanup() # Reset the GPIO configuration
try: # Loop to run the motor for the specified number of steps
for i in range(step_count):
# This pattern energizes the motor coils in sequence to make it step.
# First step (sequence of coil energizing for a single step)
if i % 4 == 0:
GPIO.output(out4, GPIO.HIGH) # Activate coil 4
GPIO.output(out3, GPIO.LOW) # Deactivate coil 3
GPIO.output(out2, GPIO.HIGH) # Activate coil 2
GPIO.output(out1, GPIO.LOW) # Deactivate coil 1
# Second step
elif i % 4 == 1:
GPIO.output(out4, GPIO.LOW) # Deactivate coil 4
GPIO.output(out3, GPIO.LOW) # Deactivate coil 3
GPIO.output(out2, GPIO.HIGH) # Activate coil 2
GPIO.output(out1, GPIO.HIGH) # Activate coil 1
# Third step
elif i % 4 == 2:
GPIO.output(out4, GPIO.LOW) # Deactivate coil 4
GPIO.output(out3, GPIO.HIGH) # Activate coil 3
GPIO.output(out2, GPIO.LOW) # Deactivate coil 2
GPIO.output(out1, GPIO.HIGH) # Activate coil 1
# Fourth step
elif i % 4 == 3:
GPIO.output(out4, GPIO.HIGH) # Activate coil 4
GPIO.output(out3, GPIO.HIGH) # Activate coil 3
GPIO.output(out2, GPIO.LOW) # Deactivate coil 2
GPIO.output(out1, GPIO.LOW) # Deactivate coil 1
# Sleep for a short time to control the speed of the motor
time.sleep(step_sleep)
# Handle a keyboard interrupt (Ctrl+C) gracefully
except KeyboardInterrupt:
GPIO.cleanup() # Clean up GPIO settings before exiting
exit(1) # Exit with a failure code
# Clean up GPIO pins and reset state after the motor stops
cleanup()
exit(0)
# Exit the program successfully
1.2 LCD
Code:
import RPi.GPIO as GPIO
import time
import lcd
try:
#Setup LCD
lcd.lcd_init() # uses imported module to initialize LCD display
time.sleep(1)
#Print Hello to LCD then clear it
lcd.printer("Hello","")
#keeps it on screen for 2 seconds
time.sleep(2)
lcd.printer("","")
except KeyboardInterrupt:
pass
lcd.printer("Goodbye!","")
#keeps it on screen for 2 seconds
time.sleep(2)
lcd.printer("","")
lcd.cleanup()
#cleansup the LCD
GPIO.cleanup()
Conclusion:
During this lab we learned how to use a stepper motor and an LCD display to present the status of
the robot. This gave us practical experience in integrating visual feedback into a mechatronic
system. These exercises reinforced key concepts in motor control and status monitoring, which are
essential for developing real-world robotic systems.
Resources:
[2] H.Chaoui. ECOR1044 – Mechatronics Laboratory #2, Winter 2024. Accessed: Nov. 14, 2024.
[Online]. Available: https://brightspace.carleton.ca/d2l/le/content/273856/viewContent/3656025/View