Editing PIC Stepper Motor Tester

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 1: Line 1:
 
= PIC Stepper Motor Demonstration and Test Project =
 
= PIC Stepper Motor Demonstration and Test Project =
 
 
There are multiple versions of this project for general infomation see:  [[Stepper Motor Demonstration and Tester]]
 
Note: we now have a version for the Arduino: [[Stepper Motor Tester for the Arduino]]
 
  
 
*Name: PIC Stepper Motor Demonstration and Test Project
 
*Name: PIC Stepper Motor Demonstration and Test Project
*Status: still developing ( on back burner ), but is working.  Planning a version for the Arduino.
+
*Status: still developing, but is working  
 
*Technology: PIC microcontroller with code in BoostC  
 
*Technology: PIC microcontroller with code in BoostC  
 
*Author: russ_hensel ( where you can find an email address to reach me )
 
*Author: russ_hensel ( where you can find an email address to reach me )
 
*Summary: A PIC16F877A project that operates a stepper motor under the control of a PC running a terminal program.
 
*Summary: A PIC16F877A project that operates a stepper motor under the control of a PC running a terminal program.
  
Last revision July 4 2008 – Draft, near completion except for Java control program. The C code works, but there are lots of some in other parts of the project.
+
 
Archive zip file ( SerialStepperTest.zip ) includes: this doc, eagle schematic, program listing. Posted in download section.
+
Last revision July 4 2008 – Draft, near completion except for Java control program. The C code works, but there are lots of errors in other parts of the project – they should be fixed soon, check back or email me.
 +
Archive zip file ( SerialStepperTest.zip ) includes: this doc, eagle schematic, program listing. Email me for file, will be posted eventually.
  
 
This project has several potential uses:
 
This project has several potential uses:
Line 22: Line 19:
 
*Demonstrating the operation of a stepper motor.
 
*Demonstrating the operation of a stepper motor.
  
Platform: PIC16F877A using BoostC connected via rs232 to a PC running a terminal program. 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 MAX232 chip and a ULN2003 driver.
  
 
The project is able to drive a stepper motor in a number of ways ( controlled by the RS232 connection ):
 
The project is able to drive a stepper motor in a number of ways ( controlled by the RS232 connection ):
Line 153: Line 150:
 
|-valign="top"
 
|-valign="top"
 
|DRIVER  = Low side switch for motor coils.
 
|DRIVER  = Low side switch for motor coils.
|Driver chip for stepper.  ULN2803.  Good for up to .5 amp 35 volts I think.  Could use discrete transistors ( possibly darlingtons ) if you want higher current. Also see note on stepper power.
+
|Driver chip for stepper.  ULN2803.  Good for up to .5 amp 35 volts I think.  Could use discrete transistors ( possibly darlingtons ) if you want higher current. Also see not on stepper power.
 
<!-------------------------------->
 
<!-------------------------------->
 
|-valign="top"
 
|-valign="top"
Line 216: Line 213:
 
== Command Interface ==
 
== 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 ).  In earlier versions the commands had to be in lower case, I think current versions take either upper or lower case - check it out.
+
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 )
 +
 
  
see: [[Stepper Motor Demonstration and Tester]] which covers both the PIC and Arduino Version.
+
{| class="wikitable"
 +
|-valign="top"
 +
! Command
 +
! Code
 +
! Notes, PIC Response
 +
<!------------------------------->
 +
|-valign="top"
 +
|Report version
 +
|v
 +
|Version of the PIC software something like:
 +
Serial Stepper Test ver July 4 2008
 +
<!------------------------------->
 +
|-valign="top"
 +
|Set direction
 +
|d+
 +
d-
 +
|plus for forward, minus for back
 +
Direction set.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Where = Request motor position
 +
|w
 +
|Step taken since power on.
 +
Signed int.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Report on all parameters
 +
|r
 +
|Delimited by commas something like:
 +
Dir +1, Permutation 1, Stepper Pos 80, Step Delay Us = 300 .....
 +
<!------------------------------->
 +
|-valign="top"
 +
|Go for a number of steps
 +
|gnnn
 +
|go for a number of steps ( max. about 30,000 ) Direction set with d.
 +
Responds with "g starting<cr>" when rotation begins, then with "g done<cr>" when done.  May be stopped early with stop command.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Set the time delay between steps in ms ( max 255 )
 +
|tnnn
 +
|Reports delay set.  nnn = 0 to 255
 +
<!------------------------------->
 +
|-valign="top"
 +
|Micro second delay in addition to to the ms delay.
 +
|unnnn
 +
|Reports delay set. Ok to use values nnn = as high as 5000 us.
 +
<!------------------------------->
 +
|-valign="top"
 +
|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.
 +
<!------------------------------->
 +
|-valign="top"
 +
|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.
 +
<!------------------------------->
 +
|-valign="top"
 +
|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
 +
<!------------------------------->
 +
|-valign="top"
 +
|Stop
 +
|!
 +
|Should almost immediately stop long running commands like Go or x1 or x2. Responds with  Stopped<cr> when stoped ( wich should be quick ).
 +
<!------------------------------->
 +
|-valign="top"
 +
|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 ==
 
== 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.
 
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.
see: [[Stepper Motor Demonstration and Tester]] for comments that apply to both the PIC and Arduino versions of the project.
+
 
 +
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 ====
 
==== Compiling ====
Line 236: Line 321:
  
 
== Java Control Program ==
 
== Java Control Program ==
This was once an idea, but I have dropped it for lack of interest ( both mine and others ).  Some thoughts that I had in the past:
 
  
Rather than use a terminal emulation program you can use a Java Program. This makes it easier to issue commands and has a much lower chance of making a mistake. The interface looks like this: ( actually almost written, email me if you have a special interest ).
+
Rather than use a terminal emulation program you can use a Java Program that I will write. This makes it easier to issue commands and has a much lower chance of making a mistake. The interface looks like this: ( actually almost written, email me if you have a special interest ).
  
 
This basically issues the same commands as you can issue with the terminal. There are a couple of enhancements
 
This basically issues the same commands as you can issue with the terminal. There are a couple of enhancements
  
 
<All> sends all the parameters then the go command, thus you can run the whole setup by typing in the parameters and pressing <All>.
 
<All> sends all the parameters then the go command, thus you can run the whole setup by typing in the parameters and pressing <All>.
 +
 +
=== Setting up the java control program. ===
 +
Will have to wait until program is done.
  
 
== Which Wire is Which? ==
 
== Which Wire is Which? ==
  
[[Stepper Motor Demonstration and Tester]] which covers both the PIC and Arduino Version.
+
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 PIC came up all right and the serial connection is working – use the Version command ( v ) and see if the response is reasonable, if not try another couple of times, if it sill does not work reboot the pic. 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 50 ms delay per step. Choose permutation 0. Now try 100 steps ( g100 ) If the motor does not run try another permutation.  
  
== Other Things to Try With the Tester ==
+
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 – russ test some more with more motors ) 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:
  
[[Stepper Motor Demonstration and Tester]] which covers both the PIC and Arduino Version.
+
 
 +
{| class="wikitable"
 +
|-valign="top"
 +
! Wire ID
 +
! Perm.
 +
Pattern 0
 +
! Perm.
 +
Pattern 1
 +
!Perm.
 +
Pattern 2
 +
!Perm.
 +
Pattern 3
 +
!Perm.
 +
Pattern 4
 +
!Perm.
 +
Pattern 5
 +
<!------------------------------->
 +
|-valign="top"
 +
|1
 +
|A
 +
|A
 +
|A
 +
|A
 +
|A
 +
|A
 +
<!------------------------------->
 +
|-valign="top"
 +
|2
 +
|B
 +
|B
 +
|C
 +
|C
 +
|D
 +
|D
 +
<!------------------------------->
 +
|-valign="top"
 +
|3
 +
|C
 +
|D
 +
|B
 +
|D
 +
|B
 +
|C
 +
<!------------------------------->
 +
|-valign="top"
 +
|4
 +
|D
 +
|C
 +
|D
 +
|B
 +
|C
 +
|B
 +
<!------------------------------->
 +
 
 +
 
 +
|}
 +
 
 +
 
 +
Discussion
 +
 
 +
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 ):
 +
 
 +
[[Image:FormHalfSize.png | Stepper Motor Test Form]]
 +
 
 +
In use:
 +
 
 +
{| class="wikitable"
 +
|-valign="top"
 +
! Entry
 +
! Use
 +
 
 +
<!------------------------------->
 +
|-valign="top"
 +
|Test Date
 +
|Test Date.
 +
 
 +
<!------------------------------->
 +
|-valign="top"
 +
|Motor ID
 +
|Something to identify the motor, including anything written on it.
 +
 
 +
<!------------------------------->
 +
|-valign="top"
 +
|Appearance
 +
|Generally what the motor looks like.
 +
<!------------------------------->
 +
|-valign="top"
 +
|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.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Electrical data
 +
|Anything you know or can figure out about the voltage, current, resistance.... of the motor.
 +
<!------------------------------->
 +
|-valign="top"
 +
|PO rot =
 +
|What the motor does when driven by permutation 0.  And so on through P5 rot.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Diagram
 +
|Draw in a schematic of the coils, lable A...D with the wire colors ( this is based on the resistance chart )
 +
<!------------------------------->
 +
|-valign="top"
 +
|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.
 +
<!------------------------------->
 +
|-valign="top"
 +
|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.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Minium Step Time
 +
|Smallest time ( set both the ms and the us ) at which the motor rotates reliably and with enough torque.
 +
<!------------------------------->
 +
|-valign="top"
 +
|Max Rev
 +
|Compute from Minium Step Time
 +
<!-------------------------------
 +
|-valign="top"
 +
|
 +
|
 +
<!-------------------------------
 +
|-valign="top"
 +
|
 +
|
 +
<!------------------------------->
 +
 
 +
|}
 +
 
 +
Example, filled out:  ( comming soon )
 +
 
 +
[[Image:StepFormFilledOutSmall.png | Form Filled Out ]]
 +
 
 +
== Other Things to Try ==
 +
 
 +
* 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.
  
 
== Additions/Changes ==
 
== Additions/Changes ==
  
[[Stepper Motor Demonstration and Tester]] which covers both the PIC and Arduino Version.
+
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.
  
 
== Possibly useful links ==
 
== Possibly useful links ==
  
[[Stepper Motor Demonstration and Tester]] which covers both the PIC and Arduino Version.
+
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]]
 +
 
 +
More information on serial communications with microcontrollers: [[Microcontroller Serial Communications Articles]]
  
== Download ==
+
Info on steppers:
 +
Stepper motors
 +
http://www.allaboutcircuits.com/vol_2/chpt_13/5.html
  
[http://home.comcast.net/~russ_hensel/OC/StepperTest_v1.zip Version 1 zip file: StepperTest_v1.zip]  If you want the most up to date version, email me to see if there have been some unposted revisions. [[russ_hensel]]
+
More info on steppers:
 +
Basic Stepping Motor Control Circuits
 +
http://www.cs.uiowa.edu/~jones/step/circuits.html
  
== Comment, Questions, Contributions? ==
+
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/
 +
 
 +
Some may considered this project for just determining which stepper wire is which overly complicated: there are some simpler methods ( for example: http://www.doc.ic.ac.uk/~ih/doc/stepper/others/ ). However for one reason or another this has not always worked for me, this test is definitive and fun.
 +
 
 +
=== Modifications You May Need to Make ===
 +
 
 +
As they occur to me.
 +
 
 +
=== Download ===
 +
 
 +
[http://home.comcast.net/~russ_hensel/OC/StepperTest_v1.zip 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.
 
Email me [[russ_hensel]], or use the talk page for this topic.  All feedback is welcome.
 
[[category:projects]][[Category:Test Equipment]][[category:Serial Communications]][[category:Microcontroller]][[category:PIC]][[category:BoostC]]
 

Please note that all contributions to OpenCircuits may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see OpenCircuits:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)