Driving a 28BYJ-48 Stepper Motor & ULN2003 driver with a Raspberry Pi

These motors are exceptionally affordable and offer remarkable accuracy thanks to their 1/64 gearing. Each step of the motor corresponds to a movement of approximately 0.087890625°. However, it's important to note that the gearing mechanism is made of plastic, which means that it may experience wear and tear over time, particularly when handling heavy objects. Additionally, if the motors are subjected to intense usage, they may generate heat and become slightly warm.



import RPi.GPIO as GPIO
import time

in1 = 17
in2 = 18
in3 = 27
in4 = 22

# careful lowering this, at some point you run into the mechanical limitation of how quick your motor can move
step_sleep = 0.002

step_count = 4096 # 5.625*(1/64) per step, 4096 steps is 360°

direction = False # True for clockwise, False for counter-clockwise

# defining stepper motor sequence (found in documentation http://www.4tronix.co.uk/arduino/Stepper-Motors.php)
step_sequence = [[1,0,0,1],

# setting up
GPIO.setmode( GPIO.BCM )
GPIO.setup( in1, GPIO.OUT )
GPIO.setup( in2, GPIO.OUT )
GPIO.setup( in3, GPIO.OUT )
GPIO.setup( in4, GPIO.OUT )

# initializing
GPIO.output( in1, GPIO.LOW )
GPIO.output( in2, GPIO.LOW )
GPIO.output( in3, GPIO.LOW )
GPIO.output( in4, GPIO.LOW )

motor_pins = [in1,in2,in3,in4]
motor_step_counter = 0 ;

def cleanup():
GPIO.output( in1, GPIO.LOW )
GPIO.output( in2, GPIO.LOW )
GPIO.output( in3, GPIO.LOW )
GPIO.output( in4, GPIO.LOW )

# the meat
i = 0
for i in range(step_count):
for pin in range(0, len(motor_pins)):
GPIO.output( motor_pins[pin], step_sequence[motor_step_counter][pin] )
if direction==True:
motor_step_counter = (motor_step_counter – 1) % 8
elif direction==False:
motor_step_counter = (motor_step_counter + 1) % 8
else: # defensive programming
print( “uh oh… direction should *always* be either True or False” )
exit( 1 )
time.sleep( step_sleep )

except KeyboardInterrupt:
exit( 1 )

exit( 0 )

Stuff you might need for this to run:

sudo apt-get update –fix-missing && sudo apt-get install python3-rpi.gpio


This motor moves at a rate of 5.625*(1/64)° per step, resulting in a total of 2048 steps to complete a 180° rotation.

and 4096 steps for 360°:

One fascinating feature of the driver board is the inclusion of LEDs. Not only do these LEDs add a visually appealing aspect to projects with their blinking lights, but they also serve a functional purpose by providing insights into the internal workings of the stepper motor and aiding in troubleshooting. The LEDs are designed to illuminate in a specific sequence, allowing users to visually identify and address any potential issues.

About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation, and engineering. I have a clear and simple writing style and am skilled in using infographics and diagrams. I am a great researcher and is able to present information in a well-organized and logical manner.

Scroll to Top