Controlling a stepper motor with Raspberry Pi Zero W

In my projects with Raspberry PI I always has been fashinated by the infinite fields of application for this fantastic board. One of my preferred is robotics, and this requires the control of arms, wheels and gears by using motors.

Stepper motors are the most common motor type in commerce for these projects, because of their precision.

In this article I’ll show you you to control a simple stepper motor with a Raspberry Pi Zero W.

What we need

This time things are a little more complex than other previous articles, because we need to perform some cabling tasks that require attention.

For this tutorial I use the following:

  • Raspberry Pi Zero W kit – including SD card, power supply and (the most important) a GPIO header
  • Stepper motor – I use Elegoo 28BYJ-48
  • Motor driver – I use Elegoo ULN2003
  • dupont wiring (in my configuration, I will use 4x female-female + 2x female-male)

Before starting (additional HW)…

Before starting, let me expose some considerations on hardware.

Raspberry Pi Zero W kit comes with a GPIO header not connected to the board (why? I really don’t know…). So you have 2 options:

  1. You can use the one coming with the kit, but you will need to solder it. There are many tutorials over internet (you can just google “raspberry pi zero w gpio soldering”) and it is quite simple if you have a little of soldering manuality.
  2. You can buy an alternative solderless holder, which is easier to attach (you can google “raspberry pi zero w solderless gpio hammer header”). In this case you will need to add it in your shopping list

About the choice of Elegoo parts, I choose them because they are cheap and enoght for my very first experiment. I must state that these motors are a little slow for robotics projects, but they appears to be precise. They come in a kit including also some female-male dupont cables, but if you don’t have a breadboard you will need to buy one or to buy other female-female dupont cables.

Regarding the power supply, it is possible to power the motor directly from RPI GPIOs (there are some pins cabled to ground and some other cabled to 5V). The second option is to use an external power supply which can erogate 5V. The second option is mine one. Built this with an old electrical screwdriver, detaching the battery box and identifying positive and negative connectors. I used also 4x rechargable AA battery providing 1,2V each one and connected it with 2 male-female dupont cables (from male side):

RPI motor battery pack

RPI motor battery wiring

Wiring schema

The following picture shows how connections have been cabled in my lab:

RPI stepper motor schema

The stepper motor comes with a pre-installed connector that you can directly connect to ULN2003 controller. All other connection are made with dupont cables.

To identify GPIO in Raspberry Pi Zero, refer to the following picture:

RPI Zero W GPIO pinout

This numbers both GPIO IDs and phisical positions. Using RPI.GPIO python libary requires the use of GPIO ID instead of phisical position. The picture above shows also pins usable to supply power (5V and 3V3) and ground.

We use:

  • GPIO 17 (connected to ULN2003 IN1)
  • GPIO 27 (connected to ULN2003 IN2)
  • GPIO 22 (connected to ULN2003 IN3)
  • GPIO 23 (connected to ULN2003 IN4)

so having the following:

RPI motor GPIO wiring

If you want to power the motor with RPI power PINs, you will connect one of Ground pins to ULN2003 negative pin and one of 5V pins to ULN2003 positive pin.

In the following picture, the ULN2003 wiring:

RPI motor driver wiring

…and the overall picture:

RPI motor lab overview

Preparing the test

We’ll start from a clean raspbian stretch lite installation (see here for steps).

Once connected via ssh to your RPI, update first of all:

sudo apt-get update

Install RPi.GPIO:

sudo apt-get install rpi.gpio

Now we need a Python script to control the stepper motor with our Raspberry Pi. A good hand comes from Matt Hawkins (, who prepared a simple script ready to be used. We can just download and align the script to our wiring configuration.

So, first download:


now edit:


Identify the row where StepPins are defined (row 33). The original one appears as the following (including comments):

# Physical pins 11,15,16,18
StepPins = [17,22,23,24]

Comments are useful to remember phisical wiring and corresponding GPIO IDs that must be declared in StepPins array. If you cabled like me (GPIO 17, 27, 22 and 23), edit these 3 rows having a final result like the following:

# Physical pins 11,13,15,16
StepPins = [17,27,22,23]

Now you can easily test your lab by launching a simple command:

sudo python

and you should see the motor rotating.

Finally, you can find step motor on common e-commerce sites or with the following link for best prices I’ve found around the web: Product – 5pcs New Brand ULN2003 28BYJ-48 5V Reduction Step Motor Gear Stepper Motor 4 Phase Step Motor for arduino 5pcs Motor +5pcs Board

Amazon – ELEGOO 5 Sets 28BYJ-48 ULN2003 5V Stepper Motor + ULN2003 Driver Board for Arduino


Please follow and like us: