Control a Stepper Motor using Python and a Raspberry PI

Daniel Wilczak
3 min readApr 25, 2021

--

As of right now there is not that many good tutorials teaching those new to python / raspberry pi how to control a stepper motor. In this tutorial your going to learn how to control a stepper motor using a Raspberry PI, TB6600 stepper motor controller and Python.

I love to show examples first for people like me who don't want to read too much. Here is the wiring diagram and code used in this tutorial.

Wiring Diagram:

Wiring diagram to control a Nema 23 stepper motor using python,TB6600 and Raspberry PI.

Code:

All the code can be found here or at the bottom of the article.

Breakdown: Hardware Setup and Code:

This tutorial can be broken down into two sections. First we’ll start with the setup of the hardware. Followed by how the code is going to control the stepper motor steps.

Hardware Setup:

Here you’ll find all the things necessary to get your stepper motor setup.

Which stepper motor wires go together:

To find which winding of the stepper motor wires go together we’ll simply use a multi meter on continuity mode. Whichever two wires beep than you’ll know those go together.

Multi meters show continuity with the symbol: )))

Setting up the controller amp’s and micro steps:

To setup the motor controller. You’ll need to know what your stepper motor amp rating is and what steps setting you would like. To put steps in simple terms. Stepping is used when higher accuracy is needed. If your setting is on 1 micro step than 200 steppers are needed to do one full revolution. To quote https://reprap.org.

A stepper motor always has a fixed number of steps. Microstepping is a way of increasing the number of steps by sending a sine/cosine waveform to the coils inside the stepper motor. In most cases, micro stepping allows stepper motors to run smoother and more accurately.

For our Nema 23 example were going to keep the 1 micro step option. Which is ON, ON, OFF. We’ll talk about where we will use this later in the article.

TB6600 Motor driver specifications.

Your stepper motor amp setting can be found on your models specification sheet. For our example, the Nema 23 motor required 1.5 amps or a value of ON, ON, OFF.

Switch view of the TB6600 Stepper motor controller

The first three switches reference your micro steps and the following three switches are to your stepper motors amp requirement. Note the direction on the switch module that ON is in the down direction.

Code:

The python code to run a stepper motor is rather easy. It requires a setup of the pins and than a way of transmitting a PWM signal to the controller.

PIN Setup.

PWM Control

Now to control the motor using PWM signals. I’ve added a lot of comments in the code to hopefully make it clear how this happens.

Now your ready to go with your stepper motor and raspberry pi. Please let me know if this helps you or there is anything I can do to clarify.

Thank you for reading,
Daniel Wilczak

Links to items used:

Nema 23 Motor

TB6600 Stepper Motor Controller

Raspberry Pi

--

--

Daniel Wilczak
Daniel Wilczak

Written by Daniel Wilczak

Data engineer with a passion for machine learning.

Responses (1)