Difference between revisions of "Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project"

From OpenCircuits
Jump to navigation Jump to search
Line 16: Line 16:
  
  
Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program, or as an alternative running a Java program developed especially to control the PIC ( still under development ). The PIC chip is supplemented with a MAX232 chip and a ULN2003 driver.
+
Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program, or as an alternative running a Java program developed especially to control the PIC ( still under development ). The PIC chip is supplemented with a dual op amp connected as two schmidt triggers, there is of course a stepper motor.
  
  
Example:
 
  
{| class="wikitable"
 
|-valign="top"
 
! Command
 
! Meaning
 
<!-------------------------------
 
|-valign="top"
 
|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.
 
<!-------------------------------
 
|-valign="top"
 
|t10<cr>
 
|Set the step delay to 10 ms
 
<!-------------------------------
 
|-valign="top"
 
|p1<cr>
 
|Select permutation 1
 
<!-------------------------------
 
|-valign="top"
 
|d+<cr>
 
|Set direction forward
 
<!-------------------------------
 
|-valign="top"
 
|g400<cr>
 
|Go for 400 steps – full turn on many motors
 
<!-------------------------------
 
|-valign="top"
 
|
 
|
 
<!-------------------------------
 
|-valign="top"
 
|
 
|
 
  
<!------------------------------->
 
  
|}
+
== Hardware ==
  
 +
The output of the stepper motor can be quite small if the motor is turned slowly, or quite high if turned fast.  To deal with the high output we used 2 diodes one to the positive rail and one to the negative rail.  To avoid two power supplies a voltage divider across a single 5 volt power supply is used and the center of the divider is considered as ground for the input.  To deal with low voltage inputs the full gain of an op amp is used, this will take a small voltage and boost it to either +5 volts ( if the op amp can reach the positive voltage rail ) or 0 volts.  Since the op amp is powered on 5 volts its output cannot get so high as to damage the PIC.  My first circuit worked just like this and produced junk for output.  Apparently a lot of noise crept in near the transition between the voltages, this despite that the signals did not look too bad on an oscilloscope.  To avoid this I added some positive feedback to make the circuit a Schmidt trigger.  The feedback makes the circuit less sensitive, but dramatically improved the success of the device.  Decreasing the the feedback will make the circuit more sensitive but may bring the noise back, increasing the feedback resistor decreases the feedback.
  
== Hardware ==
+
In the schematic only 1 of the two Schmidt triggers is shown.  Note that the stepper motor has 4 coils, only two are used.  It does not matter which the first coil is, but then next one must be one of the three that is 90 degrees out of phase with the first.  If you have scope with xy input, plot the two signals.  When the motor is rotated the plot should rotate around the corners of a square.
 +
 
 +
The PIC part is the circuit is like that of [[PIC based Stepper Motor Dancing Analog Clock]] or [[Stepper Motor Tester]] Use the inputs as specified in the software.
  
I have used a PIC 16F877A for the project, but pretty much any PIC with a uart and another 4 free I/O lines should do. To increase the drive to the motor I used a ULN2803 which is simply an array of Darlington transistors and diodes to be used as a low side switch for each motor winding. There are other similar chips around or discrete devices can be used. I run the pic, on 5 volts, and a larger voltages for the stepper, up to the limit of the driver. ( the UNL2803 is good for 50 v at .5 amp as a switch ) and the rating of the stepper motor. If you have a high power motor you may want a driver with more guts, Just put in some substitute. Coil drive is on or off there is no PWM involved here. Note that the hardware has substantial uncommitted resources. You could easily drive another motor for example. Also some of you may want to put some pull up or down resistors on some of the uncommitted resources. Instead of this board you could use the circuit from the [[PIC based Stepper Motor Dancing Analog Clock]], the main difference is that that circuit does not use the MAX232 chip, it relies on an off board level shifter ( makes it easy to shift the level shifter from project to project while saving some money and board space )
 
  
 
=== Schematic ===
 
=== Schematic ===

Revision as of 16:34, 19 December 2008

Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project

EARLY DRAFT - NOT READY


  • Name: Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project
  • Status: early draft -- do not read
  • Technology: PIC microcontroller with code in BoostC
  • Author: russ_hensel ( where you can find an email address to reach me )
  • Summary: A PIC16F877A project that operates a stepper motor as an encoder Runs with a PC running a terminal program.


Last revision Dec 19 2008 – Early Draft. Archive zip file ( none ) includes: nothing Email me for file, will be posted eventually.


Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program, or as an alternative running a Java program developed especially to control the PIC ( still under development ). The PIC chip is supplemented with a dual op amp connected as two schmidt triggers, there is of course a stepper motor.



Hardware

The output of the stepper motor can be quite small if the motor is turned slowly, or quite high if turned fast. To deal with the high output we used 2 diodes one to the positive rail and one to the negative rail. To avoid two power supplies a voltage divider across a single 5 volt power supply is used and the center of the divider is considered as ground for the input. To deal with low voltage inputs the full gain of an op amp is used, this will take a small voltage and boost it to either +5 volts ( if the op amp can reach the positive voltage rail ) or 0 volts. Since the op amp is powered on 5 volts its output cannot get so high as to damage the PIC. My first circuit worked just like this and produced junk for output. Apparently a lot of noise crept in near the transition between the voltages, this despite that the signals did not look too bad on an oscilloscope. To avoid this I added some positive feedback to make the circuit a Schmidt trigger. The feedback makes the circuit less sensitive, but dramatically improved the success of the device. Decreasing the the feedback will make the circuit more sensitive but may bring the noise back, increasing the feedback resistor decreases the feedback.

In the schematic only 1 of the two Schmidt triggers is shown. Note that the stepper motor has 4 coils, only two are used. It does not matter which the first coil is, but then next one must be one of the three that is 90 degrees out of phase with the first. If you have scope with xy input, plot the two signals. When the motor is rotated the plot should rotate around the corners of a square.

The PIC part is the circuit is like that of PIC based Stepper Motor Dancing Analog Clock or Stepper Motor Tester Use the inputs as specified in the software.


Schematic

Schematic


Parts -- this is not up to date, working on it

Part Details
Power supply Not shown, you need two dc voltages, one for the PIC at 5 volts, and another with the right voltage for the motors. There is a suitable power supply in the PIC based Stepper Motor Dancing Analog Clock
PIC16F877A My favorate 16 series part, relatively lots of memory and pins. Bigger than you need, but only about 8 bucks. Try with an 18 series part, should not be hard and will leave you more up to date. Let me know.
Pull Up/Down Resistors 10k more or less
CQ1, CQ2 = capicators for the crystal about 20 pf seems to work, see the PIC16F877A manual
Q = crystal 4 meg Hz is what I used. May be quite a bit faster than needed, I have not looked into this. The 4 meg crystal seems to work ok on a proto board. Note that some of the code is dependent on this frequency, but could be fairly easily changed.
C_BP = By Pass Cap. Not on schematic, good idea to add one. A .01 to .1 mfd mica or other by pass cap, good at high frequency seems good.
PUSH_BUTTON_SWITCH = Reset Push to reset the processor. Mine was from salvage.
VPLUS_VDD = Positive power To the 5 volt power.
G1 = Ground connection Not show, add one to connect the power supply.
MOUNT_1...4 Mounting holes

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 PIC. Do not send a new command ( except stop ) until earlier commands have been completed ( actually you can get ahead some if you are careful )


Command Code Notes, PIC Response
Report version v<cr> Version of the PIC software something like:

Serial Stepper Test ver July 4 2008

Report on all parameters r Delimited by commas something like:
Stop ! Should almost immediately stop long running commands of which there are currently none. Response Stopped.
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
  • 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, see link below. I like this compiler it has both a free version with some restrictions and a very reasonably priced full version. Writing in C should make 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 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 interrupt driven 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 pic 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.

Compiling

The zip file contains the entire source bootst project. Unzip into a directory and open in source boost. Set the target to 16F877A. See the comments in the program header for other setup before compiling. After compiling my compiler reports something like:

Memory Usage Report

  • RAM available:368 bytes, used:139 bytes (37.8%), free:229 bytes (62.2%),
  • Heap size:229 bytes, Heap max single alloc:95 bytes
  • ROM available:8192 words, used:2118 words (25.9%), free:6074 words (74.1%)

I think you could contract the program a bit to get under the 2K free compiler limit.



Other Things to Try

Additions/Changes/Modification

  • The 877A is way overkill for this project unless you make it do a bit more. Note that only 2 input pins plus the serial lines are really used.
  • There is nothing magic about the dual op amp I had around, lots of others should work, there are also IC schmidt triggers. Some PIC input ports have schmidt triggers on their inputs. Op amps are good for many other things so nice to have around. You can tweak the parameters of a op amp schmidt trigger in a way you cannot with other circuits. Basic_Circuits_and_Circuit_Building_Blocks#Schmitt_Trigger
  • Optimize the code especially in countEncoder()

Possibly useful links

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

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

A project using a stepper motor as a rotary encoder: Rotary Encoder http://www.webx.dk/oz2cpu/20m/encoder.htm

Using a disk drive motor as encoder: Inexpensive rotary encoder http://users.tkk.fi/~jwagner/electr/rotary-enc/

Info on steppers: Encoder Implementation http://www.seattlerobotics.org/encoder/jun97/encoding.html

Info on steppers: Stepper motors http://www.allaboutcircuits.com/vol_2/chpt_13/5.html

More info on steppers: Basic Stepping Motor Control Circuits http://www.cs.uiowa.edu/~jones/step/circuits.html

A free terminal program, I like this much better than hyperterminal: Welcome to our Free Download/New Products Page! http://www.rs485.com/psoftware.html

BoostC – I think the free version is enough to compile the program: SourceBoost Technologies http://www.sourceboost.com/


Download

pending, not at: Version 1 zip file: StepperTest_v1.zip

Comment, Questions, Contributions?

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