create new tag
view all tags

Stepping Motor Exercises


A stepping motor is a device which is often used when a precise amount of movement is required: an exact angle of a an exact position on a linear scale. While DC motors move while they a powered a stepping motor requires a sequence of impulses to move step by step as its name suggests.

In this exercise we will learn how to program different step sequences for full step of half step movement and how to move the motor forward and backward.

This is a photo of the motor itself and its driver module featuring an ULN2803 driver chip.


Exercise 1: Make sure the connections are correct

The stepping motor is has 5 connections which are provided by the cable from the driver module to the motor itself. The 2 coils must be energized in the right order.


To do this we have 6 connections from the driver board to the Raspberry Pi:

  • 5V (check for “5V +” and “-” on the board)

  • Gnd

  • 4 phases denominated In0..In3

If you want to be compatible with my solutions then you should hook up the phases as follows:
  • phase 1 on pin 27
  • phase 2 on pin 22
  • phase 3 on pin 18
  • phase 4 on pin 17

The state of each of the In lines is shown on its corresponding LED. We will have to provide a sequence of pulses

to make the motor move by 4 full steps. This is what we call single phase stepping where only 1 coil is energized at a time. In this case we have

We can also drive the motor by energizing both coils as follows:

This way of driving the motor is called double phase stepping. It needs more power since we always have both windings energized but it will also provide a higher torque.

Last not least we can use a combination of single and double phase stepping to produce half steps. In this case the angular resolution is better by a factor of 2. The polarization table looks like this:

Connect the stepping motor to the driver board and the driver board to the Raspberry Pi. The In0 .. In3 lines must be connected to GPIO pins (e.g. 17,18,27,22). Now set only the GPIO line, to which In0 is connected, to high, all the others to low and make sure that the corresponding LED lights up. Do the same thing for all the connected GPIO pins.

Exercise 2: Write a program to move the stepping motor by 2048 steps.

Use the single phase sequence to make the stepping motor move. One sequence corresponds to 4 full steps. Make sure to wait at least 25 ms in between each setting:

  • set 1 0 0 0, wait 25ms

  • set 0 1 0 0, wait 25 ms …

Write a subroutine which moves the motor by 4 steps. Then call this subroutine 512 times. Use C arrays to define which In line corresponds to which GPIO line and a 2 dimensional array to mimic the above table.

How far does the motor move with 2048 steps? How can you lower the speed of the movement?

Exercise 3: Modify the stepping tables

Modify the tables with the values for In0..In3 to use single phase stepping, double phase stepping and half step mode. How do you have to modify these tables if you want the motor to turn the other direction? Try all 6 combinations:

  • single phase forward
  • single phase backward
  • double phase forward
  • double phase backward
  • half step forward
  • half step backward

Exercise 4: Improve your program to give user control over its parameters

Modify your program in such a way that the user can define its behavior through command line parameters:

  • -f or –forward: move forward (this is the default)

  • -b or –backward: move backward

  • -n or –numberOfSteps: number of steps (default 512)

  • -v or –velocity: velocity of movement

  • -s or –fullStep: full step (this is the default)

  • -h or –halfStep: half step

Additional Exercises:

The "inverse device" to the stepping motor is a rotary encoder. Here we do not send the pulse sequence to move a motor but we receive the sequence to be able to tell by how far the motor has moved. The encoder is usually fixed to the motor shaft and it tells us the angle or position of the motor.

Reading out an encoder is not really part of this exercise but since we have such an encoder in our sensor kit I played on it and I give you some code to read out the encoder in the solutions section to this exercise.

The following signal shapes show the encoder signals when moving clockwise


and counter clockwise


-- Uli Raich - 2017-05-11


-- Uli Raich - 2017-05-11


Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng clockwise.png r1 manage 2.9 K 2017-10-26 - 12:04 UnknownUser  
PNGpng counterClockwise.png r1 manage 2.9 K 2017-10-26 - 12:04 UnknownUser  
PDFpdf stepping.pdf r1 manage 310.0 K 2017-11-27 - 10:34 UnknownUser  
PNGpng stepping.png r1 manage 182.9 K 2017-05-11 - 12:22 UnknownUser  
Unknown file formatodt steppingMotor.odt r1 manage 231.1 K 2017-10-25 - 18:55 UnknownUser  
PNGpng windings.png r1 manage 28.7 K 2017-05-11 - 12:22 UnknownUser  
Edit | Attach | Watch | Print version | History: r8 < r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r8 - 2017-11-27 - uli
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback