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

Quick points about this motor & driver

They are wonderfully cheap and extremely accurate due to 1/64 gearing. They move by 0.087890625° per step! However, the gearing is made of plastic and will wear out overtime, especially if moving heavy objects. Lastly the motors can become a little toasty if you work them hard.




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 takes 5.625*(1/64)° per step, this means 2048 steps for 180°:

and 4096 steps for 360°:

One thing that is super cool about the driver board are the LEDs. Projects are always cooler with blinky LEDs, but these guys also help show what is actually going on inside the stepper, and can help you find issues. They are supposed to light up in sequence.

5 Replies to “Driving a 28BYJ-48 Stepper Motor & ULN2003 driver with a Raspberry Pi”

  1. dear friends,
    I tried to rebuild the circuit diagramm as shown above.
    I miss the ground connection between circuit (ULN2008) and Raspi. As a result of this my engine doesn’t work. Secondly I connected the external power supply-ground with Raspi-ground and external (+) with (+) of the ULN2008 circuit, but the result was the same. Zhen I used the +5V and Ground from Raspi for connecting with ULN2008 circuit. This configuration works.
    I want to supply the ULN2008 with an external power supply. How does it works? Can you help me please

    1. Rolf,

      I’m having a hard time understanding what wire was where when things were working or not. Are you saying that you actually powered the motor from the Pi? Even if it works it’s pretty “dangerous” to do so because motors generator a lot of noise on circuits and do you don’t want them hooked up directly to your GPIO pins (expect for the control pins of course). But hey, if it works it works :).

      In any case, are you saying that you have a 5V external power supply, and that you’d like to power both the Pi and the motor with it?

  2. It’s one of the first time I read a tutorial, really “ready to use”.
    I just followed your diagram, copy/past your script, and it works !

    Congrats and thank you 🙂

    1. C’est tellement la lutte pour trouver des informations succinctes et précises sur ce genre de sujet, j’essaye vraiment de poster des infos qui sont les deux. Merci de laisser un commentaire pour me faire savoir que c’est réussi :).

  3. Thank you. This article was clear and concise. It explained very well how the motor works with Raspberry pi. It is exactly what I was looking for.

Leave a Reply

Your email address will not be published. Required fields are marked *