Silicon ChipSerial Stepper Motor Controller - July 2005 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Low-cost digital cameras no match for 35mm equivalents
  4. Feature: Getting Into WiFi, Pt.3 by Ross Tester
  5. Feature: CeBIT Sydney 2005 by Ross Tester
  6. Feature: Unleashing Unwired by Ross Tester
  7. Project: Remote-Controlled Automatic Table Lamp Dimmer by John Clarke
  8. Salvage It: Salvaging & using common thermostats by Julian Edgar
  9. Project: Lead-Acid Battery Zapper by Jim Rowe
  10. Project: Serial Stepper Motor Controller by Greg Radion
  11. Feature: PICAXE In Schools, Pt.3 by Clive Seager
  12. Review: Altronics 250W Aussie-Made PA Amplifier by Ross Tester
  13. Project: AVR200 Single Board Computer, Pt.2 by Ed Schoell
  14. Vintage Radio: The 1951 AWA 433MCZ 4-Valve Battery Receiver by Rodney Champness
  15. Book Store
  16. Advertising Index
  17. Outer Back Cover

This is only a preview of the July 2005 issue of Silicon Chip.

You can view 37 of the 104 pages in the full issue, including the advertisments.

For full access, purchase the issue for $10.00 or subscribe for access to the latest issues.

Articles in this series:
  • Getting into Wi-Fi (May 2005)
  • Getting into Wi-Fi (May 2005)
  • Getting Into WiFi, Pt.2 (June 2005)
  • Getting Into WiFi, Pt.2 (June 2005)
  • Getting Into WiFi, Pt.3 (July 2005)
  • Getting Into WiFi, Pt.3 (July 2005)
Items relevant to "Remote-Controlled Automatic Table Lamp Dimmer":
  • PIC16F628A-I/P programmed for the Remote-Controlled Automatic Table Lamp Dimmer [autolamp.hex] (Programmed Microcontroller, AUD $10.00)
  • PIC16F628A firmware and source code for the Remote-Controlled Automatic Table Lamp Dimmer [autolamp.hex] (Software, Free)
  • Remote-Controlled Automatic Table Lamp Dimmer PCB pattern (PDF download) [10107051] (Free)
Items relevant to "Lead-Acid Battery Zapper":
  • Lead-Acid Battery Zapper PCB pattern (PDF download) [14107051] (Free)
  • Lead-Acid Battery Zapper front panel artwork (PDF download) (Free)
Items relevant to "PICAXE In Schools, Pt.3":
  • PICAXE-08M BASIC source code for "PICAXE in Schools", part 3 (Software, Free)
Articles in this series:
  • What’s this? Free PC Boards for Schools? (May 2005)
  • What’s this? Free PC Boards for Schools? (May 2005)
  • PICAXE In Schools, Pt.2 (June 2005)
  • PICAXE In Schools, Pt.2 (June 2005)
  • PICAXE In Schools, Pt.3 (July 2005)
  • PICAXE In Schools, Pt.3 (July 2005)
  • PICAXE In Schools, Pt.4 (September 2005)
  • PICAXE In Schools, Pt.4 (September 2005)
  • PICAXE In Schools; Pt.5 (November 2005)
  • PICAXE In Schools; Pt.5 (November 2005)
Articles in this series:
  • AVR200 Single Board Computer, Pt.1 (June 2005)
  • AVR200 Single Board Computer, Pt.1 (June 2005)
  • AVR200 Single Board Computer, Pt.2 (July 2005)
  • AVR200 Single Board Computer, Pt.2 (July 2005)

Purchase a printed copy of this issue for $10.00.

This easy-to-build unit plugs into the serial port of your PC and can control up to four separate stepper motors via suitable driver boards. Alternatively, you can cascade up to four units to control up to 16 steppers. It’s easy to program too. By GREG RADION Serial Stepper Motor Controller U NTIL NOW, IT HAS BEEN relatively difficult for the experimenter to properly control stepper motors using a computer. That’s because most stepper motor kits sold today interface the step and direction inputs to a parallel port and then require you to write the software to switch these inputs. If you need to incorporate limit switches and acceleration and deceleration of the stepper motor, what started out as a simple job turns out to be complicated and time consuming. What’s more, parallel port designs can generally control only one or two motors and some designs don’t allow multiple boards to be cascaded together. The Serial Stepper Motor Controller (SSMC) described here overcomes these problems. It’s a relatively compact microcontroller-based design that attaches to a PC’s serial port and provides control for up to four stepper motors (via a suitable driver board). What’s more, it does away with the need for special software to control 60  Silicon Chip the acceleration and deceleration of the motors. Instead, you just issue the basic commands and the software inside the microcontroller does all the hard work for you. It’s really very easy to program. There are just nine commands (see Table 2) and these are all entered via a standard serial terminal program (eg, HyperTerminal). We’ll have more on this later. Want to control more than four steppers? No problem – up to four Serial Stepper Motor Controller boards can be cascaded (or “ganged”) together and individually addressed. This allows you to control up to 16 stepper motors, all from the one serial port. You can’t do that with most parallel port designs and, in any case, the parallel port is rapidly disappearing (many laptops no longer include a parallel port, for example). And with the availability of cheap USB-to-serial converters, this controller could easily be adapted for use on any USB port. Fig.1 shows how the SSMC boards are connected. Note that the SSMC board does not directly drive the motors, since it has no on-board driver circuitry. Instead, each stepper motor is driven via a separate driver board. There are several stepper motor driver kits available that can be used with the SSMC board. These include kits K179 (unipolar) and K142B (bipolar) from Oatley Electronics. The unipolar driver board was originally published as the Mini-Stepper Motor Driver in the May 2002 issue of SILICON CHIP. It can control both 5-wire and 6-wire unipolar stepper motors, while the bipolar board controls 4-wire and 6-wire motors. Each of these kits has step and direction signal inputs which allow the user to control the movement of the motor. The Serial Stepper Motor Controller simply connects to these step and direction inputs on the driver cards. Stepper motors Before moving on to the circuit siliconchip.com.au Fig.1: up to four Serial Stepper Motor Controller (SSMC) boards can be cascaded together, so that you can separately control up to 16 stepper motors from the PC. Note that each stepper is driven via a separate driver board (see text). description, let’s take a closer look at the way stepper motors work. Unlike regular DC or AC motors which have commutator brushes to automatically switch the stator coils, stepper motors have no brushes. Instead, the coils inside a stepper motor are individually switched by the stepper motor control circuit. The accompanying panel explains the differences between conventional siliconchip.com.au motors and stepper motors in greater detail. There are two main types of stepper motor: unipolar and bipolar. Let’s take a look at each type in turn. Unipolar stepper motors Most unipolar stepper motors contain two centre-tapped coils and these effectively act as four individual coils – see Fig.2(a). These motors have either five or six wires. Five-wire motors join the two centre taps together, while 6-wire motors bring out the centre tap connections individually. In addition, there are 8-wire unipolar stepper motors and these have four individual coils, with a wire for each end of each of the coils. The four coils of a unipolar stepper motor are individually activated and deactivated sequentially by the July 2005  61 This driver board can be used with both 5-wire and 6-wire unipolar motors. It’s available from Oatley Electronics (Cat. K179). Oatley Electronics also has a board to drive bipolar stepper motors (Cat. K142B). Fig.2(a): unipolar stepper motors generally have either five or six wires. Five-wire motors join the two centre taps together, while 6-wire motors bring the centre tap connections out separately. being that the current flowing though each pair of coils must be reversible. Bipolar stepper motors generally have less steps than unipolar stepper motors of the same size but provide more torque. Note that unipolar stepper motors can also act as bipolar stepper motors if the centre taps are omitted. Circuit details Fig.2(b): bipolar stepper motors have only two coils and four wires. As a result, their drive requirements differ from unipolar stepper motors, the main difference being that the current flowing though each pair of coils must be reversible. controller. Each time this is done, the motor advances one step. Bipolar stepper motors Bipolar stepper motors have only two coils and four wires – see Fig.2(b). That means that the drive requirements for bipolar stepper motors is somewhat different to that of unipolar stepper motors, the main difference The circuit for the Serial Stepper Motor Controller is shown in Fig.3. First, incoming data on the serial port (K2) is converted to 0-5V microcontroller “friendly” levels by a MAX232 chip (IC3). This data is then processed by an Atmel ATMEGA8 microcontroller (IC). DIP switch S1 (2-way) is used to set the address of the board when multiple SSMC boards are plugged together. As shown, the switch lines are run to port lines PD2 & PD3, which are normally pulled high via two 10kW resistors (ie, when both switches are open). Provision is also made on the circuit to accept limit switch inputs. These inputs (LS1-LS4) are fed to port lines PD5-PD7 & PB0 which again are normally pulled high. The 10kW resistors in series with the port pins provide protection for the microcontroller. Where To Buy A Kit The Serial Stepper Motor Controller was developed by Ocean Controls, 4 Ferguson Drive, Balnarring, Vic and all copyright is retained by Ocean Controls. Prices are as follows: (1) Full kit of parts for SSMC (Cat. KT-5190) .......................... $55.00 + GST (2) Fully assembled SSMC unit (Cat. KT-5190A) ................... $65.00 + GST Visit the company’s website at www.oceancontrols.com.au for pricing and ordering details, or phone (03) 5983 1163. Note: prices do not include postage. 62  Silicon Chip The “Step” and “Direction” outputs appear at port lines PB1-PB2 & PC0PC5 and are fed through to their onboard terminals via a 74HC245 buffer chip (IC3). K2 is a 9-pin female D-connector and this is used to connect the circuit to the PC (via an RS232 cable). This is also wired in parallel with K3, a 9-pin male D-connector which allows additional controller boards to be connected. The associated 18kW resistor and diode D2 are necessary to allow multiple devices to be connected to the serial link (see the section below on “multi-dropping”). The terminal connectors provide connection for power at terminal Vs, limit switch inputs (L1-L4), step outputs (S1-S4) and direction outputs (D1-D4). Power for the circuit is derived from a 9-12V DC plugpack supply, with diode D1 providing reverse polarity protection. A 3-terminal regulator (REG1) is then used to provide a +5V rail to power the ICs. Using the Controller The Ocean Controls Serial Stepper Motor Controller is controlled using a serial terminal program, set at a baud rate of 9600, with one start bit, one stop bit and no parity. The accompanying panel shows how to set up HyperTerminal, which comes with Windows. The commands for the controller take the form: <at>AA CMND XXXX where AA is the 2-digit number of the motor being addressed (between 01 and 16 - see Table 1), CMND is the 4-letter command (see Table 3), and XXXX is a numeric value associated with the command (see Table 2). When a valid command is received siliconchip.com.au Fig.3: the complete circuit for the Serial Stepper Motor Controller. The MAX32 chip (IC2) converts the RS232 data to TTL levels and this data is then processed by microcontroller IC1 to derive the step and direction control signals. by the unit, it responds with the address preceded by a hash symbol – ie, #AA – and this is followed by a value if it is requested. Status command detail The status command returns the state of each of the motors attached to a single controller board. Valid “stat” commands have the address of the first motor on the board, eg: <at>01 STAT returns the status of motors 01-04 <at>05 STAT returns the status of motors 05-08 <at>09 STAT returns the status of motors 09-12 <at>13 STAT returns the status of motors 13-16 siliconchip.com.au These are the four valid status commands. The returned value is a 12-bit binary representation indicating whether the motors are moving, their direction and the state of the limit switches. Table 3 shows the general format. Multi-dropping ”Multi-dropping” is the term used when connecting multiple slaves to one master. This is achieved by including a signal diode on the transmit outputs of the slaves, plus a resistor to pull the transmit line to ground when it is not being used. The diode prevents voltages produced by transmitted data from the slaves appearing on the transmit pins of the other slaves. All the slaves receive the same data from the master and decode it if necessary. Fig.4 shows the basic scheme for multi-dropping. Note that the 18kW pull-down resistor and the diode (D2) are included in the circuit, so you don’t have to worry about adding these if you do decide to connect several units together. All you have to do is plug Table 1 – Addressing S2 S1 Off Off Motor Numbers 01-04 Off On 05-08 On On Off On 09-12 13-16 July 2005  63 the SSMC boards together, as shown in Fig.1. Acceleration Fig.4: multiple slaves can be connected to one master controller using a technique called “multi-dropping”. This diagram shows how the scheme is implemented using diodes and pull-down resistors. Each time a command to move a stepper motor is issued, the Serial Stepper Motor Controller calculates the stepping times to give a gradual acceleration and deceleration. In operation, the acceleration and final speed are determined by the ACCN, ACCI and RATE parameters. The default values are 50ms, 2ms and 10ms respectively but these can be changed simply by issuing the appropriate command. When a command is issued to move the motor, it starts stepping at one step every “ACCN ms” and then decreases this by “ACCI ms” every step until the interval is “RATE ms”. Subsequently, as the motor approaches the final position, the step interval then increases by “ACCI ms” from “RATE ms” until the final position is reached, at which point the interval will be back to “ACCN ms” – see Fig.5. Limit switches Fig.5: the microcontroller on the SSMC board calculates the stepping times to determine the acceleration and deceleration – see text. Table 2 – Use These Commands To Control Your Stepper Command Description POSN Set the position that motor AA is currently at to XXXX, where XXXX is between -99,999,999 and 99,999,999. PSTT Returns the position of motor AA. AMOV Move motor AA to absolute position XXXX, where XXXX is between -99,999,999 and 99,999,999. RMOV Move motor AA relatively from the current position by XXXX, where XXXX is between -99,999,999 and 99,999,999. STOP Stop motor AA immediately. STAT Get the status of the motors (see “Status Command Detail” section in text). ACCN Set the maximum stepping rate in milliseconds of motor AA to XXXX, where XXXX is between 0 and 9999 (see “Acceleration”). If the value for ACCN is 0 or less than RATE, then no acceleration or deceleration occurs. ACCI Set the Acceleration interval in milliseconds of motor AA to XXXX, where XXXX is between 1 and 9999 (see “Acceleration”). RATE Set the minimum stepping rate in milliseconds of motor AA to XXXX, where XXXX is between 1 and 9999 (see “Acceleration”). Table 3 – Status Value msb L4 11 L3 10 L2 9 L1 8 D4 7 D3 6 D2 5 D1 4 M4 3 M3 2 M2 Where M, D and L represent the movement, direction and limit switches respectively. For movement: 1 = moving, 0 = stopped. For direction: 1 = forward, 0 = reverse. For limit switches: 1 = closed, 0 = open. 64  Silicon Chip lsb M1 As stated previously, the limit switch inputs are normally pulled high. An input is activated when a limiting switch closes and pulls it to ground. Fig.7 shows how the limit switches are wired. Note that multiple limit switches can be used on each motor, provided they are wired in parallel. Note also that normally open (NO) switches must be used. When a limiting switch closes, the associated motor stops. If the switch remains closed, the motor will then only perform a single step in response to each subsequent command issued. This can be used to back the motor off the limit switch one step at a time, for example. Assembly The assembly is straightforward, with all parts mounted on a doubledsided PC board with plated through holes. Fig.6 shows the parts layout. Start the assembly by installing the resistors and diodes, taking care to ensure that the diodes are correctly oriented. The 9 x 10kW SIL resistor package can also be installed at this stage. It must be oriented with the white dot on the package (ie, pin 1 – the common connection) to the left. That done, install the capacitors and siliconchip.com.au the crystal, followed by the IC sockets, voltage regulator REG1 and the DIP switch. Take care with the orientation of the DIP switch – the “ON” marking goes towards the terminal block. Regulator REG1 must be mounted with its metal tab towards the two 100nF capacitors. Next, add the screw terminal blocks and the two 9-pin “D” connectors (K2 & K3). The DB9F (female) connector is mounted on the left, while the DB9M (male) connector goes to the right. K1 (a 2 x 5-way pin header) is for in-circuit programming but is not supplied as part of the kit. Note also that if you are going to gang two or more controller boards together, you will need to remove the screw posts from the DB9M connectors. This is necessary to allow the male and female connectors to mate correctly when pushed together. Don’t install the ICs yet – that step comes later, after the supply rail has been checked. Fig.6: install the parts on the PC board as shown on this wiring diagram. Note that pin header K1 is for in-circuit programming (ISP) but is not supplied as part of the kit since IC1 is supplied preprogrammed. Testing To test the controller, first connect power (to Vs & COM) and measure the voltage across pins 10 & 20 of IC2’s socket. You should get a reading of 5V. If not, switch off immediately and check that you have installed the regulator (REG1) correctly. If you do get 5V, remove the power and push the ATMEGA-8 microcontroller (IC1), 74HC245 (IC2) and MAX232 ICs into their sockets. Take care to ensure that these ICs are all Fig.7: here’s how the limit switches are wired. Note that normally open (NO) switches must be used. correctly oriented – each device is installed with its notched (pin 1) end towards the right, as shown in Fig.6. Make sure too that all the IC pins go into the sockets and that none are folded underneath the IC or splayed out down the side of the socket. Once all the ICs are in, connect the board to the computer using an RS232 cable and reconnect power. Set the DIP How A Stepper Motor Works Stepper motors are everywhere. For example, every computer contains several (ie, in the floppy and hard disk drives). They’re used because it is easy to achieve very precise positional control – far better than you can achieve with a “normal” motor. Unlike a conventional motor, where you simply connect an appropriate voltage and “away she spins”, stepper motors require considerably more effort to get them to work. First of all, think of a conventional motor. It has two main components – a stator, which sets up the magnetic field, and a rotor, which by magnetic attraction or repulsion turns towards or away from the magnetic field. siliconchip.com.au There’s also a commutator (actually part of the rotor) which keeps switching power from one coil to the next, moving the magnetic field as well, so the rotor has to keep moving, or rotating. Stepper motors are similar in many respects – they have stators and they have rotors – but they don’t have commutators. The magnetic fields which cause attraction/repulsion, and therefore cause the rotor to turn, are set up externally by the motor controller. In a stepper motor, one stator coil is first energised, repelling the rotor. Then that coil is de-energised and the next one energised, again repelling the rotor. Keep this up and the rotor turns continuously. By controlling which field coils are energised and when, the rotation and stopping position of the rotor can be precisely controlled. You will hear stepper motors referred to as 0.9°, 1.8° and 3.6° types, and so on. This refers to the angle of rotation for one “step” of the motor – eg, a 0.9° motor makes 400 individual steps to complete one full rotation of 360°. That’s a lot of steps, especially as each one can be individually accessed. And many stepper motors operate through a gearbox, multiplying that yet again. The speed of rotation is directly related to how fast you can switch current between the coils. This is no problem at low speeds but can cause problems as the switching frequency increases. July 2005  65 How To Configure The Terminal Program Par t s Lis t 2 14-pin DIP sockets (for IC1) 1 16-pin DIP socket 1 20-pin DIP socket 1 2-way DIP switch (S1) 1 DB9F right-angle connector (K2) 1 DB9M right-angle connector (K3) 6 3-way 5.08mm screw terminal blocks 1 2-way 5.08mm terminal block (T2) 1 8MHz crystal (X1) STEP 1: open HyperTerminal & set up a new connection. STEP 2: choose that COM port that the SSMC board is connected to. Semiconductors 1 Atmel ATMEGA-8 programmed microcontroller (IC1) 1 74HC245 octal buffer (IC2) 1 MAX232 RS232-to-TTL level shifter (IC3) 1 7805 5V regulator (REG1) 1 1N4004 silicon diode (D1) 1 1N4148 silicon diode (D2) Capacitors 2 22pF ceramic (C1, C2) 4 0.1mF monolithic (C3-C6) 4 1mF electrolytic (C7-C10) STEP 3: configure the port settings as shown here. STEP 4: click File, Properties, Settings, ASCII Setup and select the “Send line ends with line feeds” and “Echo typed characters locally” functions. STEP 5: entering the <at>01 STAT command should return #01 0 if the board is working correctly and nothing else is connected. switches so they address from 01-04 (ie, both off) and then run a terminal program at 9600 baud and type <at>01 STAT and press the Enter key. 66  Silicon Chip This should return #1 0 if the unit is working properly and nothing else is connected. If you have an oscilloscope, you can Resistors 1 9 x 10kW 10-pin SIL resistor array 4 10kW (R1-R4) 1 18kW (R5) give a move command such as <at>01 RMOV 1000 and view the pulses at terminal S1 to confirm that the unit is working. Alternatively, if you don’t have an oscilloscope, you will have to connect the unit to a driver board and stepper motor. Just connect the driver board (with its stepper) to the S1, D1 and COM terminals and issue the above move command – the motor should immediately move in response. If it does, your SSMC board is working correctly and you can start programming more ambitious control sequences. Example program An example Visual Basic program with source code for controlling four motors is available on the Ocean Controls website at www.oceancontrols. com.au. This program can easily be expanded to control up to 16 motors for virtually any stepper-motor apSC plication. siliconchip.com.au