top of page

STM Stepper Motor Tutorial

This tutorial will detail both how to choose a stepper motor as well as how to use a stepper motor with an ST discovery board/uC.

Stepper Motor Selection

Stepper motors are DC motors which work by dividing a full rotation into several steps. This allows the placement of the motor to be known with much higher accuracy than a conventional DC motor. By having the motor step through a full rotation we can keep track of exactly what step the motor is on and in doing so we can know the location of the Stepper Motor to high accuracy. The drawback of a Stepper motor is that it typically consumes significantly more power than a typical DC motor. Additionally, to hold specific position stepper motors require significant amounts of power even while stationary.

Still, for the third HAB I need a motor that can move a significant amount of weight and accurately hold its position. Before purchasing Stepper motors note that typically there are three components required to operate a stepper motor. The first is the stepper motor itself. Second most stepper motors also require an external driver to be able to turn. Finally, the driver/stepper motor needs to be powered through an AC to DC power converter. Below I have included pictures of each component from my setup.

IMG_6681.jpeg

Before deciding on a power supply and driver for the stepper motor the motor itself must be determined. Most Stepper motors specify the N*m of torque they can produce. For this reason to calculate the size of motor required you need to calculate how much torque you will need your motor to apply. To be safe I buy motors with twice the calculated torque. I do this to ensure that my setup is redundant and flexible. If you find out that you need a lot of torque than there are times you may need to use gearing to allow a smaller the Stepper Motor to apply more torque. These are all considerations which you will need to determine independently. In my case after calculating the weight and position of my antenna array, I decided to purchase a 3.1 Newton meter (N*m) stepper motor. The motor I bought was purchased from Stepper Online and was a Nema 23 Stepper Motor. NEMA 23 specifies the size of the inner coil of a motor. Typically the larger more powerful stepper motors have higher NEMA numbers. Having chosen the motor I needed I could look at its datasheet to find its operating amperage based on this I was able to choose a stepper driver capable of supplying the necessary currents. I also purchased the driver from Stepper Online. Finally, I selected a power supply which would be able to handle both the current and voltage requirements of my stepper motor/driver.
​
Below are links to all the components I will use in this tutorial

​

​

Note that the stepper motor has an input amperage of 4.2 amps (A). This is what determined the stepper driver which can supply from 1-4.2A. Also, note that the stepper driver can take anywhere from 20 to 50 VDC. Note that at higher voltages it is possible to turn the motor faster. In my case, since I do not need the motor to turn exceptionally fast, I will not need to supply anywhere near 50 volts. But even if I were to use 4.2 Amps at 40 volts I would only require 168 watts of power. Since I am only powering two stepper motors the power supplies rated power of 350 watts should suffice for my application.

Stepper Motor Wiring

Now that all of the components to operate our stepper motor have been found, it is time to put together all the components. The first thing to note is that most stepper motors will have two loops within them, this is why many have four wires, two wires for each loop. Without going too much into how stepper motors are turned, note that it matters which to wires your wire into a loop. To better understand what I am trying to say look at the image of the stepper driver below

Stepper_Edited.png

Note that in the image below I have labeled loop 1 and loop 2. All four wires must be plugged into these ports, but we need to make sure that the wires plugged into the same loop on the driver are in the same loop within the motor. To do this you can either look at the datasheet for the stepper motor. This sheet will always tell you which two wires correspond to the same loop. Alternatively, without plugging the stepper motor into anything, at random choose two wires from the stepper motor. Now hold these two wires so they make an electrical contact. Finally, while still holding the wires together try to turn the shaft of the stepper motor. If the two wires are part of the same loop the shaft will be more difficult to turn then if the wires are not part of the same loop. In this way, you can determine which wires go to the same loop and you can use this information to properly wire the Stepper Driver.

Now that the stepper motor has been wired to the driver we need to wire the driver to the uC and the power supply. To simplify this I chose to use a benchtop power supply to power the driver for now. I did not feel comfortable wiring the power supply before consulting with an electrician before hand. If we were to power the power supply though and connect it to the stepper driver it would look something like the images below

Stepper_Edited_PS_Connections.png
Power_Supply.png

To wire the power supply to the driver we just need to connect the -V from the driver to the GND of the driver and the +V from the power supply to the +V of the driver. Once this is done all that is left to do is to wire the driver to the ST uC or an Arduino. To do we will need to use two GPIO pins from the Arduino or ST uC. To learn how to configure these pins view my tutorial on ST GPIOs.

Stepper_Edited_ST_Connections.png

Note that GPIO<x> and GPIO<x1> are two different GPIOs. But the uC ground (GND) is the same pin. Now we are almost ready to begin coding the ST uC to turn the stepper motor. First, though we need to configure the switches 1-8 on the stepper board. Note that SW1-3 control how many amps the stepper motor requires. For my project, I turned all the switches to the off position indicating a current of 4.2A. Note that a higher current will give more torque but will also lead to more heat being generated in the motor and driver. I did not move Switch 4 from the position it was in when I received the driver. Finally, we have to configure switches 5-8. Note that these switches control how many times we have to pulse our GPIO to complete a full revolution. A single pulse is the act of turning the GPIO on (5v) and then turning it back off (0v) At a minimum for this driver the pulse width which is the time the GPIO switch should be on must be at least 2.5 uS.

Lowering the number of pulses/revolution is useful for turning the motor faster but is not as accurate as higher pulses per revolution. For testing, I chose to use 1000 pulses per revolution. Note before we begin programming that whatever pin is connected to the DIR+ port controls the direction of the stepper motor. High (5v) will spin the motor clockwise and low (0v) will spin the motor counterclockwise. At this point in the tutorial, we are ready to begin programming.

Stepper Motor Programming

To begin programming start by generating the scaffolding code to control the GPIOs. Next, it is necessary to download the script found here. Add the .c file to the src directory and the .h file to the include directory. Also be sure to upadte the makefile so that the .c file is compiled. After this is done you will use the timer introduced in the linked tutorial. This tutorial allows you to create uS delays which is what we need in order to create pulses with a 2.5 uS width. Now, all that we have to do is to use a for loop to create a set number of pulses and turn the Stepper as needed. Below I have linked a repository containing the code to achieve this with the STM32F334C8. Note that the only changes to the code scaffolding are as follows.
​
In the include directory I added the file "dwt_stm32_delay.h" and in the src directory, I added the file "dwt_stm32_delay.c", both of these files came from the tutorial linked above. Next, I added the dwt_stm_delay.c to the C_SOURCES variable in the make file. This tells the compiler to compile this file along with the others. Finally in the main.c file I included "dwt_stm32_delay.h". Within the main.c file's main function I added the command to initialize the uC timer.
​
DWT_Delay_Init();
​
Last in the while loop of the main.c file in the main function I added the following code snippet to turn the stepper motor

​

Screen Shot 2019-09-15 at 6.20.15 PM.png
bottom of page