Raspberry Pi - 28BYJ-48 Stepper Motor ULN2003 Driver

This tutorial instructs you how to use Raspberry Pi to control 28BYJ-48 Stepper Motor using ULN2003 Driver. In detail, we will learn:

Raspberry Pi ULN2003 28BYJ-48 stepper motor

Hardware Preparation

1×Raspberry Pi 4 Model B
1×28BYJ-48 stepper motor + ULN2003 Driver Module
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 28BYJ-48 Stepper Motor

Stepper motors are ideal for position control. They break a full revolution into a series of equal "steps". These motors are frequently found in printers, 3D printers, CNC machines, and industrial automation applications.

One of the cost-effective methods to gain knowledge about stepper motors is to utilize 28BYJ-48 stepper motors. These usually come with a ULN2003 based driver board, making them incredibly easy to use.

As per the data sheet, when the 28BYJ-48 motor is in full-step mode, each step is equivalent to an 11.25° rotation. Therefore, there are 32 steps for a single revolution (360°/11.25° = 32).

Furthermore, the motor has a 1/64 reduction gear set. This translates to 32 x 64 = 2048 steps. Each step is equal to 360°/2048 = 0.1758°.

Conclusion: If the motor is set to 2048 steps in full-step mode, it will rotate one revolution.

The 28BYJ-48 Stepper Motor using ULN2003 Driver Pinout

The 28BYJ-48 stepper motor has 5 pins. We do not need to be concerned with the specifics of these pins. All we need to do is plug it into the ULN2003 motor driver connector.

28BYJ-48 stepper motor

Overview of ULN2003 Stepper Motor Driver Module

The ULN2003 is a widely used motor driver Module for stepper motor.

  • It has four LEDs that indicate the activity of the four control input lines, giving a great visual effect when stepping.
  • Additionally, it comes with an ON/OFF jumper to cut off power supply to the stepper motor.

ULN2003 Pinout

ULN2003 Stepper Motor Driver pinout

The ULN2003 module has 6 pins and a female connector:

  • IN1 pin: this is used to drive the motor and should be connected to an output pin on Raspberry Pi.
  • IN2 pin: this is used to drive the motor and should be connected to an output pin on Raspberry Pi.
  • IN3 pin: this is used to drive the motor and should be connected to an output pin on Raspberry Pi.
  • IN4 pin: this is used to drive the motor and should be connected to an output pin on Raspberry Pi.
  • GND pin: this is a common ground pin and must be connected to both GNDs of Raspberry Pi and the external power supply.
  • VDD pin: this supplies power for the motor and should be connected to the external power supply.
  • Motor Connector: this is where the motor plugs into.


  • The voltage of the external power supply must match the voltage of the stepper motor. For instance, if a stepper motor needs 12V DC, we must use a 12V power supply. If the stepper motor is a 28BYJ-48, it requires 5V DC, so we will use a 5V power supply.
  • However, even if the stepper motor requires 5V power supply, do NOT connect the VDD pin to the 5V pin on the Raspberry Pi. Instead, connect it to an external 5V power supply, as the stepper motor draws too much power.

Wiring Diagram

The wiring diagram between Raspberry Pi and stepper motor ULN2003 driver

This image is created using Fritzing. Click to enlarge image

It is not necessary to pay attention to the color of the wires on the stepper motor. All that needs to be done is to connect the male connector on the 28BYJ-48 stepper motor to the female connector on the ULN2003 driver.

How To Program to control a stepper motor

There are three ways to regulate a stepper motor:

  • Full-step
  • Half-step
  • Micro-step

For basic applications, the full-step method can be used. The details of the three methods will be discussed in the final section of this tutorial.

Raspberry Pi Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file stepper_uln2003.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-28byj-48-stepper-motor-uln2003-driver import RPi.GPIO as GPIO import time # Define GPIO pins for ULN2003 driver IN1 = 23 IN2 = 24 IN3 = 25 IN4 = 8 # Set GPIO mode and configure pins GPIO.setmode(GPIO.BCM) GPIO.setup(IN1, GPIO.OUT) GPIO.setup(IN2, GPIO.OUT) GPIO.setup(IN3, GPIO.OUT) GPIO.setup(IN4, GPIO.OUT) # Define constants DEG_PER_STEP = 1.8 STEPS_PER_REVOLUTION = int(360 / DEG_PER_STEP) # Define sequence for 28BYJ-48 stepper motor seq = [ [1, 0, 0, 1], [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] ] # Function to rotate the stepper motor one step def step(delay, step_sequence): for i in range(4): GPIO.output(IN1, step_sequence[i][0]) GPIO.output(IN2, step_sequence[i][1]) GPIO.output(IN3, step_sequence[i][2]) GPIO.output(IN4, step_sequence[i][3]) time.sleep(delay) # Function to move the stepper motor one step forward def step_forward(delay, steps): for _ in range(steps): step(delay, seq[0]) step(delay, seq[1]) step(delay, seq[2]) step(delay, seq[3]) # Function to move the stepper motor one step backward def step_backward(delay, steps): for _ in range(steps): step(delay, seq[3]) step(delay, seq[2]) step(delay, seq[1]) step(delay, seq[0]) try: # Set the delay between steps delay = 0.005 while True: # Rotate one revolution forward (clockwise) step_forward(delay, STEPS_PER_REVOLUTION) # Pause for 2 seconds time.sleep(2) # Rotate one revolution backward (anticlockwise) step_backward(delay, STEPS_PER_REVOLUTION) # Pause for 2 seconds time.sleep(2) except KeyboardInterrupt: print("\nExiting the script.") finally: # Clean up GPIO settings GPIO.cleanup()
  • Save the file and run the Python script by executing the following command in the Terminal:
python3 stepper_uln2003.py
  • Check out the motor rotating.
  • It should rotate one revolution in a clockwise direction, followed by two revolutions in an anti-clockwise direction, and then two revolutions in a clockwise direction.

By changing the value of delay variable in the code, you can change the speed of the stepper motor.

The script runs in an infinite loop continuously until you press Ctrl + C in the Terminal.

Additional Knowledge

Stepper motor vibrates while moving

Do not be concerned if the stepper motor shakes while in motion. This is a trait of the stepper motor. We can diminish the vibration by utilizing the micro-stepping control technique.

Additionally, due to its feature, if managed correctly, the stepper motor can create musical notes like a musical instrument. An example of this can be found here on Hackster.io.

Method of controlling stepper motors

  • Full-step: The unit of movement is one step, which is equivalent to the value of degree indicated in the stepper motor's datasheet or manual.
  • Half-step: Each full step is divided into two smaller steps. The unit of movement is half of the full step. This method allows the motor to move with double resolution.
  • Micro-step: Each full step is divided into many smaller steps. The unit of movement is a fraction of the full step. The fraction can be 1/4, 1/8, 1/16, 1/32 or even more. This method allows the motor to move with higher resolution and smoother motion at low speeds. The larger the dividend, the higher the resolution and smoother the motion.

If the motor's datasheet specifies 1.8 degree/step:

  • Full-step: The motor can move in increments of 1.8 degrees per step, resulting in 200 steps per revolution.
  • Half-step: The motor can move in increments of 0.9 degrees per step, resulting in 400 steps per revolution.
  • Micro-step: The motor can move in increments of 0.45, 0.225, 1125‬, 0.05625 degrees per step, resulting in 800, 1600, 3200, 6400... steps per revolution.

The code above used the full-step control technique.

Resonance Issue

This is for the more experienced users. Beginners do not need to worry about it. It occurs in a range of speeds, where the step rate is equal to the motor's natural frequency. There could be a noticeable change in the sound the motor makes, as well as an increase in vibration. In actual applications, developers should take this into account.

Video Tutorial


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!