Stepper Motor Demonstration and Tester

From OpenCircuits
Jump to: navigation, search


Stepper Motor Demonstration and Test Projects

This page is draft will superceed much of the info on PIC Stepper Motor Tester which is for the PIC and a new page Stepper Motor Tester for the Arduino which is for the arduino

  • Name: Stepper Motor Demonstration and Test Project
  • Status: still developing, but is working. 2 Versions one for PIC and one for the Arduino. Most of the code is very similar, circuits as well
  • Technology: Both Programmed in C.
  • Author: russ_hensel ( where you can find an email address to reach me )
  • Summary: A project that operates a stepper motor under the control of a PC running a terminal program. Can both test the motor and demonstrate its functions
  • Last revision Dec 20 2010
  • Downloads are available see download section on the sub page for each project

Archive zip file see individual pages for each sub project, PIC Stepper Motor Tester which is for the PIC and Stepper Motor Tester for the Arduino for the Arduino or email me russ_hensel for the most up to date info.

This project has several potential uses:

  • Example code for stepper motor control and for RS232 communications.
  • Determining which drive wire is which on a unipolar stepper motor. This is the type of motor that I have most commonly found surplus or in salvage equipment.
  • Determing angle per step or steps per revolution of a stepper motor.
  • Determing maximum speed of stepper motor.
  • Demonstrating the operation of a stepper motor.

The project is able to drive a stepper motor in a number of ways ( controlled by the RS232 connection ):

  • Set the number of steps
  • Set the direction of the steps
  • Set the time delay between the steps
  • Drive the wires to the stepper in any of the 6 possible connections ( permutations), this allows the user to determine which wire is which.
  • Test the max speed of the motor using an accelerating drive signal.

In general the stepper is driven by issuing several commands to set it up and then a final command to take a series of steps using this setup.


Command Meaning
v<cr> Get the version make sure the command interface is running, usually takes 2 tries to initialize, this is a bug which I have not fixed yet.
t10<cr> Set the step delay to 10 ms
p1<cr> Select permutation 1
d+<cr> Set direction forward
g400<cr> Go for 400 steps – full turn on many motors


See the individual pages, in general the ucontroller does all the logic and the drive is done by low side darlington transistors see: Stepper Motor Tester which is for the PIC and Stepper Motor Tester for the Arduino.

Command Interface

All commands ( except stop should be terminated with a carriage return ) Note that the command interface is not very smart, giving parameters that are out of range my blow the whole program up. If so reboot the microcontroller . Do not send a new command ( except stop ) until earlier commands have been completed ( actually you can get ahead some if you are careful ). Commands may be either upper or lower case. The backspace key usually works.

Command Code Notes, Program Response
Report on all parameters r Begins with version then other parameters something like:

dn -> Dir +1, pn ->Permutation 1, Stepper Pos = 80, Step Delay Us = 300 .....

Report version v Version of the software something like:

Serial Stepper Test ver July 4 2008

Set direction d+


plus for forward, minus for back

Direction set.

Where = Request motor position w Steps taken since power on or reset.

Signed int.

Go for a number of steps gnnn go for a number of steps ( max. about 30,000 ) Direction set with d. use g0 or just g to repeat last number of steps

Responds with "g starting<cr>" when rotation begins, then with "g done<cr>" when done. May be stopped early with stop command.

Set the time delay between steps in ms ( max 255 ) tnnn Reports delay set. nnn = 0 to 255
Micro second delay in addition to to the ms delay. unnnn Reports delay set. Ok to use values nnn = as high as 5000 us.
Set the permutation of the motor wires. pn Set the permutation, find the value that works for your motor. ( n = 0 to 5 ) Responds with the permutation set.
Try the next permutation of the motor wires. n Set the permutation up by one, wraps to 0, and goes for the last number of steps, quick way to find the value that works for your motor. Responds with ??.
Speed test ann Run a speed test gradually accelerating the motor up to speed, then down, and finally reversing -- should end up at the same place it started. The higher the value of nn ( about 30 max ) the faster the test. Use the report command to find the step delay for the max speed. You can also do a speed test setting the step time, but moder has sudden, not gradual acceleration. Responds with ??.
Special command 1, Spin the motor in an interesting way. x1 Motor spins responds with "x special done<cr>" when it is done. May be stopped with the stop cammand.
Special command 2, Vibrate the motor first a lot then less and less to stop x2 responds with "x special done<cr>" when it is done
Stop ! Should almost immediately stop long running commands like Go or x1 or x2. Responds with Stopped<cr> when stopped ( which should be pretty quick ).
Other, not understood commands xxx Responds with "!Bad Command = xxx" if the command is not understood.

Notes on terminal program set up.

  • Baud rate should be 19.2K 8N1, but is easily adjustable in the header file for the program.
  • Most terminal programs can be set to treat a carriage return as a carriage return line feed. Do it.

Some terminal programs will not transmit in lower case ( all our commands are lower case ) unless specially set to do so. Set it to allow lower case.

Microcontroller Program Design

I no longer have the patience for assembly language. I have moved on to C in particularly BoostC on the PIC the C in arduino.exe for the Arduino, see link below. Writing in C should makes the program fairly easy to read. Most of the design should be evident by reading the program, however a few notes here may help.

The idea is to put each activation sequence in a table ( or Array if you prefer ) and then step through that table and activate the corresponding port bits and thus stepper coils. The four wires can be activated in a total of 6 different ways, one table corresponds to each permutation. The particular table to use is set using the permutation command ( p ).

The series of wires to energize is specified in the arrays StepperStepsN where N is the number of the wire permutation. Each step just increments its way through the array wrapping around the the beginning and the end.

Commands are received via an serial communications routine, the main loop checks each time around to see if a complete command has been received. Because commands are only interpreted in the main loop all commands are ignored until the program returns to the main loop. The exception to this is the stop command which will terminate a g or x command and return to the main loop quickly. RS232 transmission is not driven by an interrupt and so during transmission from the microcontroller no stepping takes place. Commands which do not result in motion execute very quickly, most of the time is for communications.

Currently drive to the motor is half step drive. This gives twice as many steps per revolution as is labeled on the motor. I plan later to let you select half step, full step or wave drive. See the links below for more information.

Which Wire is Which?

Unipolar motors typically have 6 wires that come in 2 pairs. Typically the “center tap” of each winding is connected to V+ and the individual windings are grounded ( as with the Darlington array described in hardware ) to switch them on. The tester does not determine which of the wires are the center taps, but that is easy to do with an ohm meter. Each center tap will have the same resistance to far end of either of the coils connected to it. There will be twice the resistance between the ends of the coils. Sometimes the two center taps will be connected together so the motor has only 5 wires. Sometimes the center taps will be the only two wires that are the same color. Once the center taps have been determined connect them to V+ ( whatever voltage is appropriate for the motor ) and the other wires in any order to the 4 ports. Name the wires A, B, C, D ( or use the wire colors if available, adjusting the chart below for the colors ) and connect to the drivers on bit 0, 1, 2, 3. ( in the software given I have actually used bits 4, 5, 6, 7 )

Make sure the microcontroller booted up all right and the serial connection is working – use the Version command ( v<cr>) and see if the response is reasonable, if not try the command another couple of times, if it sill does not work reboot the microcontroller. If it still does not work check over the communications parameters on your terminal program. And so on.

Set the motor to run forward, with say 10 ms delay per step ( t10<cr> ) Choose permutation 0 ( p0<cr> ). Now try 100 steps ( g100<cr> ) If the motor does not run try another permutation.

If the motor is good you should find 2 configurations that work, one forward, one back. ( In some cases I have found more than 2, I do not quite understand this, look for the smoothest running -- add a little resistance to turning and you will find the duplicates have almost no torque ) The jerky motion of the shaft can be deceiving, I add a pointer to the shaft, one that will not slip. This chart then tells you the “standard” identification of the wires:

Wire ID Perm.

Pattern 0


Pattern 1


Pattern 2


Pattern 3


Pattern 4


Pattern 5

1 A A A A A A
2 B B C C D D
3 C D B D B C
4 D C D B C B


Which wire is which depends in part what you consider a standard coil activation sequence. From my reading I believe that this corresponds to the table Zero_StepperSteps in the code. Your standard may differ, if so you can modify the code. The code uses a so-called “half step” drive. You can contact me russ_hensel if you are having trouble figuring this out. Google will link you to a huge amount of information on steppers.

Stepper Test Form

I have developed a form for testing motors that I find useful. It looks something like this ( and is included in the file download package ):

Stepper Motor Test Form

In use:

Entry Use
Test Date Test Date.
Motor ID Something to identify the motor, including anything written on it.
Appearance Generally what the motor looks like.
Table Enter the wire colors in 1...6 then measure the resistance between all pairs. The table is symetric so you need to measure only one side of the diagonal.
Electrical data Anything you know or can figure out about the voltage, current, resistance.... of the motor.
PO rot = What the motor does when driven by permutation 0. And so on through P5 rot.
Diagram Draw in a schematic of the coils, lable A...D with the wire colors ( this is based on the resistance chart )
cw on P0 List the wire colors for 1...4 and common to drive the motor clockwise on permutation 0. ( to be sure about this I change the connections and test that permutation 0 actually works that way. The chart above for the identification of the wires helps with this.
Half steps per rotation Make sure by rotating the motor at least 10 whole turns and verify that it returns to the starting position. Remember that the tester uses half steps not whole steps.
Minium Step Time Smallest time ( set both the ms and the us ) at which the motor rotates reliably and with enough torque.
Max Rev Compute from Minium Step Time

Example, filled out:

Form Filled Out

Other Things to Try With the Tester

  • How fast can your stepper go?

Run it faster and faster ( shorter time delay ) until it fails. You may want to turn it 400 turns to clockwise fast, then 400 counter clockwise slow. If it does not end up where it started then it skipped a step or more. If you know how many steps per rev then you need not reverse it just ask it to go a round number of revolutions.

  • How many steps per revolution for your motor?

Guess, try to make it turn 10 revolutions fairly slowly or at least slowly enough so that it is not skipping steps.. If you number is right it will end up at the same angle it started. If not try a new guess.

  • Demo the motor with Special Commands x1 and x2:
    • x1: spins the motor one way faster and faster, then reverses and returns to original position.
    • x2: motor vibrates faster and faster through smaller and smaller arc, then stops where it started.


These are changes I may or may not make: ( or you may take them on )

  • Add servo testing to the project ( actually fairly easy as I already have the servo tester, standalone, done. Just need to merge in the code. ).
  • Add selection for half step, full step or wave drive.
  • Use low side switches ( normally darlington connected transistors, or logic level mosfets ) with higher current ( and perhaps voltage ) rating. The current circuit is limited to about 500 ma.
  • Move the rs232 level shifter into the cable and drop it from the circuit, then the level shifter can be used for many projects.

Possibly useful links

This program uses my: Serial Communications Library -- BoostC and 16F877A

A closely related project with a lot of similar code: PIC based Stepper Motor Dancing Analog Clock

[RS232] Discusses the sort of interface that is used by this project.

More information on serial communications with microcontrollers: Microcontroller Serial Communications Articles

Info on steppers: Stepper motors

More info on steppers: Basic Stepping Motor Control Circuits

BoostC – I think the free version is enough to compile the program: SourceBoost Technologies

Some may considered this project for just determining which stepper wire is which overly complicated: there are some simpler methods ( for example: ). However for one reason or another this has not always worked for me, this test is definitive and fun.


Archive zip file see individual pages for each sub project, PIC Stepper Motor Tester which is for the PIC and Stepper Motor Tester for the Arduino for the Arduino or email me russ_hensel for the most up to date info.

Comment, Questions, Contributions?

Email me russ_hensel, or use the talk page for this topic. All feedback is welcome.

Personal tools