science tinker

Stepper motor in Python: Raspberry Pi 4 project with wiring and code

Breadboard wiring for Stepper motor in Python, as drawn in the simulator.

A 28BYJ-48 stepper motor is driven by a ULN2003 board: IN1 on GPIO 17, IN2 on GPIO 18, IN3 on GPIO 27 and IN4 on GPIO 22. Python has no stepper library, so switch the coils yourself with the eight half-step patterns. Turn the shaft a quarter turn forward (1024 half steps), wait 1 second, then turn it a quarter turn back to where it started. Wait 2 ms after each pattern. Print done at the end.

  • Raspberry Pi 4
  • Advanced level
  • 2 parts
  • Free, in your browser

You will learn: How a stepper motor moves: energising the coils one pattern at a time pulls the rotor round in fixed steps. Going through the patterns backwards turns it the other way, and the waiting time sets the speed.

Project 81 of 85 in the project gallery. Lesson 10 of 11 in the course Raspberry Pi with Python.

The idea behind this page: how a microcontroller project works

A microcontroller project has three sides that must all be right: the parts, the wiring and the program. A microcontroller is a small computer on one chip. Its pins can read inputs, such as a button or a sensor, and switch outputs, such as an LED, a buzzer or a motor. The program, called a sketch on an Arduino, is the list of instructions that says what the outputs should do when the inputs change. Almost every project follows the same pattern: read an input, decide, then set an output, and repeat this many times a second.

A guided project gives you a goal, the parts, the wiring and starter code with gaps for you to complete. The simulator then checks your work by running it, the way a teacher would test a real circuit. The solution is not shown on this page, because working it out is how the ideas are learned.

This page is about one guided project: Stepper motor in Python. Read it with these ideas in mind.

How to study this page

  1. Read the goal at the top, then the parts table to see what each part is for.
  2. Study the wiring table and the picture: which leg of each part goes to which pin.
  3. Read the starter code line by line before you change it. Find the place where your own lines must go.
  4. Open the project in the simulator, complete the code and press Run. Read any message the check gives you: it says what is wrong and what to write.
  5. When it passes, change one thing (a pin, a delay, a limit) and predict what will happen before you run it again.

Mistakes students make

  • Using a different pin in the code from the one in the wiring.The pin number in the sketch must match the pin the wire really goes to.
  • Leaving out pinMode() in setup().A pin must be set as an input or an output before it is used, or it will not behave as expected.
  • Changing many things at once.Change one thing, run, and see what happens. Then you know which change caused the result.
  • Ignoring the check message.The message names the fault. Read it fully before trying again.

Key words

Microcontroller
A small, complete computer on one chip, with a processor, memory and input and output pins.
Sketch
The Arduino name for a program.
Pin (of a microcontroller)
A metal contact on a chip or board through which it reads an input or drives an output.
Input
A signal going into the microcontroller, for example from a button or a sensor.
Output
A signal coming out of the microcontroller, for example to an LED or a motor.
Breadboard
A board with rows of joined holes for building a circuit without soldering.

See every key word in the glossary

What to learn first and next

Learn this first
Learn this next

Parts you need

PartHow manyWhat it does in this project
Raspberry Pi 41Runs your Python program and powers the circuit.
Stepper motor (28BYJ-48 with ULN2003 driver)1A motor that turns in exact small steps, 2048 for one full turn, so a sketch can set its angle without any position sensor.
Breadboard and jumper wires1Hold the parts and join them without soldering.

How to wire it

Put the parts on the breadboard, join the board's 5V and GND pins to the breadboard's power rails, then make these connections:

PartConnection
Stepper motorIN1–IN4 → GPIO 17 (pin 11), GPIO 18 (pin 12), GPIO 27 (pin 13), GPIO 22 (pin 15) (in code: Stepper motor(2048, IN1, IN3, IN2, IN4))

In the Studio, the 💡 guide rings show where each wire goes, and the ✔ Check button tells you which connection is missing.

Starter code

Start from this Python program and write the missing lines. The comments say what each part has to do.

from gpiozero import OutputDevice
from time import sleep

coils = [OutputDevice(p) for p in (17, 18, 27, 22)]   # IN1 to IN4
HALF = [[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], [1, 0, 0, 1]]

def step(pattern):
    for coil, level in zip(coils, pattern):
        coil.value = level
    sleep(0.002)

# a quarter turn forward: 1024 half steps
for i in range(1024):
    step(HALF[i % 8])

sleep(1)

# now a quarter turn back: go through the patterns in the other order
# hint: HALF[(1023 - i) % 8]

print("done")

Build it step by step

  1. Open the project. Press Build it in the Studio. The parts are already in the tray.
  2. Place and wire the parts. Follow the wiring table above, or switch on the guide rings.
  3. Write the code. Complete the starter program in the Code panel. It is Python, written as text.
  4. Run it. Press ▶ Run and use the part's slider, button or switch to test it. The terminal panel shows what your program prints, and Stop works like Ctrl+C.
  5. Check it. Press ✔ Check: the simulator tests the circuit and the code and tells you exactly what to fix.

Questions students ask

What does the Stepper motor in Python project do?

A 28BYJ-48 stepper motor is driven by a ULN2003 board: IN1 on GPIO 17, IN2 on GPIO 18, IN3 on GPIO 27 and IN4 on GPIO 22. Python has no stepper library, so switch the coils yourself with the eight half-step patterns. Turn the shaft a quarter turn forward (1024 half steps), wait 1 second, then turn it a quarter turn back to where it started. Wait 2 ms after each pattern. Print done at the end.

What parts do I need for the Stepper motor in Python project?

An Raspberry Pi 4, stepper motor, a breadboard and jumper wires.

How do I wire the Stepper motor in Python project?

Stepper motor: IN1–IN4 → GPIO 17 (pin 11), GPIO 18 (pin 12), GPIO 27 (pin 13), GPIO 22 (pin 15) (in code: Stepper motor(2048, IN1, IN3, IN2, IN4)). Power comes from the board's 5V (or 3.3V) and GND pins along the breadboard rails.

What will I learn from the Stepper motor in Python project?

How a stepper motor moves: energising the coils one pattern at a time pulls the rotor round in fixed steps. Going through the patterns backwards turns it the other way, and the waiting time sets the speed.

Can I build this without real hardware?

Yes. The project runs in the free Science Tinker microcontroller simulator in your browser: place the parts, wire them, write the sketch and press Run. The ✔ Check button tests your circuit and code and tells you what to fix. Nothing to install and no account needed.

Where is the finished code?

The page gives the starter sketch so that you write the important lines yourself. If you are stuck, the 💡 Solution button in the Studio shows a complete, working sketch that you can run and study.

More advanced projects

Components in this project

Know someone who would use this?

It’s free for every learner. Send it to your class, team or club.

Updated · Free, no sign-up ·