Stepper Motor Indexer & Decoder
ECE 476: Spring 2005

Daniel Beer, dbb28
Tony Lloyd, aml54

Introduction | High Level | Hardware Design | Software Design
Results | Pictures | Conclusion | Appendices


2. High Level Design

In scientific laboratories stepper motors are used to mechanically position just about everything: monochrometers, diffractometers, translation stages, slits, optical tables, etc. Historically, stepper motors have been used because they stand up well to radiation, can in many instances be used open loop, are relatively simple to interface, and are cheap. Unfortunately, for critical applications, any slippage of the motor shaft ends up corrupting data, hence our interest in knowing the angular position of the motor shaft.


The picture on the left above is of two stepper motors with optical encoders on a four circle diffractometer. On the right is a hand drawn ‘dial’ indicator which uses a paper clip as a reference marker; in the lower right hand corner are two stepper motors.

Scientific labs use two motor coordinate systems called “user” and “dial” coordinates. These coordinates are physical units, either millimeters or degrees of arc. The scientist places his sample in the beam, a laser beam or x-ray beam, and defines this as the zero position in user coordinates. At the zero position is user coordinates the scientist next enters the dial indicator reading on his apparatus into the software. It is important to note that sense (the direction) of the dial coordinates can be opposite the sense of user coordinates, but in all cases both coordinate systems are in the same physical units. The scientist then defines the scan or data gathering direction as positive while the rewind direction is negative. Backlash is used in the rewind direction to minimize errors in any gearing.

Stepper motors in scientific labs usually use a 'tweak' or ‘jog’ motion function as well as incremental and absolute motion functions in user coordinates. Also, stepper motors usually have software and hardware limits which are supported in both coordinate systems.

Our high level design was to mimic as closely as possible how stepper motors are normally used in a scientific lab but rather than operate open loop, to also install an optical encoder on a double shafted stepper motor and use the optical encoder signals as feedback to our controller. To decode the optical encoder we used an LS7266R1 dual 24-bit quadrature counter to decode and count the encoder signals from the optical encoder.

3. Design Specification

We take the basic definitions of user and dial coordinates as well as the definitions of motion and rates of motion which follow, as the specification for our controller system. From this basic specification, a communications protocol is then established, as well as the basic control language used to control our ATmega32 motor controller. The hardware interfaces which we have implement naturally fall out of the specification which follows.

The two basic stepper motor velocity profiles for scan (forward or positive movement) and reverse (rewind or negative movement) are first defined. The basic equations of motion are given and then user and dial units are rationalized as they relate to our controller.

We used a US Digital MD1 10 MicroStep controller in our design for finer angular resolution of the motor shaft. Most stepper motors have 200 steps per revolution. With a microstepper there are 2000 microsteps per revolution. The optical encoder has 2000 lines per revolution. With a 4 times decode this yields 8000 counts per revolution; a well matched resolution for the MD1 microstepper.

3.1 Definition of Rates and Units

3.1.1 Base rate

Base rate is also called start rate, velocity base, base frequency or start frequency. Usually the base rate has units of Hertz but it is useful to think in units of steps per second. By abuse of language, steps per second is really microsteps per second and a step pulse is a microstep pulse.

The base rate is the rate from which a stepper motor can reliably start from rest. Similarly for a motor moving at its base rate, once step pulses to the motor stop, the motor reliably come to rest at its last position, i.e., last step.

Said another way, a stopped stepper motor can be instantaneously accelerated to its base rate and instantaneously decelerated from its base rate to a stopped position. Implied in the above is that a stepper motor at base rate Vb can be instantaneously decelerated to a stopped position and then instantaneously accelerated to –Vb. For a stepper motor a typical base rate is 500 Hertz (microsteps per second).

The drawing above shows a stepper motor stepped at its base rate. If Vb is clockwise (cw) then the motor is stepped cw at t1 and t2, then counterclockwise (ccw) at t5 and t6, then cw at t7 and finally ccw at t8; after the foregoing movement the motor should be in exactly to same position as when it started.

3.1.2 Steps per degree/mm

The user is interested in the final movements of the stepper motor, such as one millimeter or one degree. As such there is a conversion factor defined for a given motor which relates steps, or in our case microsteps to the user units of degrees or millimeters. If you take the output shaft of a stepper motor as the movement you are interested in: the stepper motor has 200 steps/rev. and the MD1 Microstep Drive has 10 microsteps/step for 2000 microsteps per revolution. Given 360 degrees per revolution the conversion factor is 50/9 microsteps per degree or inversely 18/100 degrees per microstep; so each microstep is = 0.18 degrees of arc movement. The foregoing is for the output shaft of the stepper motor. Accordingly, if you affix a 10:1 gear reducer on the stepper motor then the output shaft will move 0.018 degrees of arc per microstep.

3.1.3 Acceleration time

Our motor controller uses a constant acceleration or linear velocity ramp to accelerate to its steady state velocity, Vss, followed by a period of constant velocity and ending with a similar period of constant deceleration to a stop. The time of acceleration is made equal to the time of deceleration.

The drawing below shows the acceleration velocity profile of a stepper motor going from its base rate, Vb, to its steady state rate, Vss, over an acceleration time of tA. The basic equations of motion for acceleration are also given.

A reasonable set of units would be steady state rate Vss = 5000 Hz, base rate Vb = 500 Hz., and acceleration time tA = .25 seconds. With a conversion rate of 2000 steps / revolution (2*phi), this gives an acceleration of 18*phi radians/ sec2.

3.1.4 Forward movement

Shown below is the velocity profile of a complete forward movement of a stepper motor. By convention for scientific instruments forward movement is positive movement and is the direction in which data are gathered. Forward movement is the scan direction.

Forward movement is defined by three periods: (1) starting with a period of constant acceleration - accelerated from Vb to Vss - followed by (2) a period of constant velocity - steady state rate of Vss for tD – tA seconds -, and finally (3) a period of constant deceleration where the stepper motor is decelerated in the same amount of time that it was accelerated. The motor finally stops when its velocity equals the base rate. Equations of deceleration are given for completeness.. The last equation of total distance traveled is used in the software: the total distance traveled s, equals sA, acceleration distance, plus sss, steady state distance, plus sD, the deceleration distance.

3.1.5 Reverse (rewind) motion with backlash

Shown below is the velocity profile of a complete reverse movement of a stepper motor. This movement ends with backlash compensation so the motor always works into the load in the same direction thereby minimizing errors. The units of backlash are steps. By convention, for scanning instruments the reverse direction is negative and is often called the rewind direction. In a typical experiment scan movements are small amounts of movement after which data is gathered, then the cycle is repeated; small scan step, gather data ….repeated scores of times. Finally, the instrument is rewound to its initial position, called a rewind motion, and another scan is initiated. It is at the end of a rewind motion that backlash is applied.

The equations for total distance traveled are also given for reverse motion. . The math confirms that there are basically two equations needed to calculate total distance traveled, (1) an acceleration/deceleration equation and (2) a steady state equations. A basic rule of thumb is that backlash should be between 1/10th and 1/20th of a degree or millimeter; therefore backlash should have a value 1/10th to 1/20th of the conversion factor.

3.1.6 User Units

Our motor controller uses units of degrees or millimeter and we support the use of a dial indicator. Rotating instruments come with ruled marks on the rotating part of the instrument while on the non-rotating part of the instrument there is an indicator mark. The same holds true for translating instruments. The measuring rulers are referred to as dial indicators and measure physical units. When the instrument is installed and the scan direction is defined it often turns out that the dial runs counter to the user units. The utility of the dial comes when the instrument ‘loses position.’ The dial is then used to ‘recover position.’

The user first defines the positive sense of scan and defines zero as the center of a beam. The user then enters what the dial indicator reads at the zero position of user units into the computer. Shown below is an instrument with a positive scan direction downward and dial indicator which reads upward. The dial reads 90 when the user units reads zero. User units are related to dial units by:

user_units = (sign * dial_units) + offset (1)

Dial units are taken as directly proportional to the hardware value in the motor accumulation. Note that the conversion factor can be negative, and this is exactly what is done when the user wants to change the sense of his user units. If you have a case where the dial units run counter to the user units then sign = -1 and if the conversion factor is also negative then as the motor accumulator increases the user units will also increase.

If the user wants to change his reference position then the value of offset is changed by the software to reflect the change relative to dial units. In no case is the value of offset directly changed as this would destroy the relation between dial and user units. The value of the limits are in both dial and user units and if a dial limit is changed then the corresponding user limit is changed according to equation (1).

3.1.7 Relative and Absolute Movement

Our motor controller has the ability to move to any valid relative (within software and hardware limits) user unit. It also has the ability to move from its current position to any absolute position providing the end position is within its valid limits.

3.2 Command Protocol

The personal computer communicates via half duplex RS-485 communications with the ATmega32 UART. The motor controller is always a slave and the personal computer is always the master in our system.

3.2.1 Packet Structure

The packet structure for data being sent to the MID consists of six bytes:

MID address ! Command R/W ! Data[0..7] ! Data[8..15] ! Data[16..23] ! Checksum

The ! symbol means concatenation. No spaces are allowed in the string. The motor controller has an address with the value 1 to 127 (bit D0 to D6) and the MSB (D7) is 1. A read or write command (1 for write, 0 for a read query) is determined by the MSB of the Command instruction. A parameter of up to 24-bits can follow in the next three bytes. The range of possible parameter values is given in the next section. The checksum is an 8-bit value which is the sum of all five bytes in the packet preceding the checksum.

Upon receipt of a packet the MID will immediately respond to the master with a packet of seven bytes structured as follows:

MID address ! Command ! Return code ! Return value[0..7] !
Return value[8..15] ! Return Value[16..23] ! Checksum

The MID address is the address of the MID sending the packet to the master controller with the MSB = 0x80. The Returned value shall be specific to certain commands. The Return codes shall are defined below. The checksum shall be the 8-bit sum of all preceding six bytes.

3.2.2 Command Set

Shown below are the commands which our motor controller supports.






0 ACCEL_TIME 128 to 2,048 msec Velocity Ramp Acceleration Time
1 BASE_RATE 200 to 2,048 steps/sec Base Velocity Rate
2 CONV_UNITS 0 to 50,000 steps/mm Conversion to User Units
3 STEADY_RATE 200 to 2,048 steps/sec Steady State Velocity
4 BACKLASH 0 to 5000 steps Backlash
5 SLIP_WAIT 16-bit int msec Slip Check Delay Time
6 ENABLE_DRIVE 0 or 1   Enable/Disable Drive
7 ASSOC_LIMITS 0 or 1   Limit Association Reversal
8 DIAL_INDICATOR 16-bit int mm Read/Write Dial Indicator
9 LCD_CONTROL 0 or 1   Enable/Disable LCD
10 MID_STATUS 8-bit int   Read MID Status (0xff = okay)
11 USER_POSITION 24-bit int mm Read/Write User Position
12 MULTIPLEXER_SET 2-bit   Read/Write Multiplexer Select
13 LOWER_SLIMIT 16-bit int mm Read/Write Lower Soft Limit
14 UPPER_SLIMIT 16-bit int mm Read/Write Upper Soft Limit
15 DECODER_A_POS 24-bit signed int steps Read/Write Decoder A Position
16 DECODER_B_POS 24-bit signed int steps Read/Write Decoder B Position
17 LOWER_HLIMIT 0 or 1   Read Lower Hard Limit
18 UPPER_HLIMIT 0 or 1   Read Upper Hard Limit
19 unused      
20 TWEEK_MOTOR 16-bit int mm Set Current Motor Offset
21 MOVE_INCREMENTAL 16-bit signed int mm Move Motor Incrementally
22 MOVE_ABSOLUTE 16-bit int mm Move Motor to Position


24-bit int steps Move Motor Microsteps

3.2.3 Returned Codes

Shown below is a list of return codes which out motor controller sends back to the personal computer.

Value Returned



0 EVERYTHING OK Proceeding with command
1 COMMAND NOT VALID Invalid command
2 MOVE STOPPED Massive slippage, move stopped
3 MD1 NOT ENABLED Motor driver not enabled
4 MOTOR SLIP COMP. Compensating for slippage
5 INTERRUPT Hardware limit triggered
6 SOFTWARE LIMIT Software limit triggered
7 CHECKSUM ERROR Checksum error on incoming command

3.3 Interface Specification

3.3.1 RS-485 interface

Our motor controller uses a MAX487 for RS-485 communications. Spike suppression is provided as well a high frequency termination of the RS-422 signals.

3.3.2 SPI interface

The MID can be FLASH programmed using the Serial Peripheral Interface on pins PB5, PB6, and PB7.

3.3.3 MD1 interface

The MD1 interfaces with our motor controller via 4 signals: (1) + 5 volts DC, (2) Step command, (3) Direction command, and (4) an enable/disable control signal. Even though the MD1 is optically isolated a ferrit bead inductance will be used to reject noise from the MD1. Open collector buffers are used on the motor controller as Step and Direction buffers. An opto-isolator is used to isolate the MD1 enable/disable signal. When the motor controller first powers up the enable/disable signal is LOW, that is, in a disable state and Step and Direction are HIGH.

3.3.4 Encoder interface

The +5 volts DC to the optical encoder is isolated with a broadband ferrit bead. The differential RS-422 signals from the optical encoder are physically interfaced with a 10 pin head which has the same layout as the optical encoder.

3.3.4 LCD interface

The LCD interface identical to the one used in lab and is shown on page 4 of the schematic.

3.3.5 JTAG interface

Our motor controller has a JTAG interface to support future testability and maintainability requirements of our design