Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project
- 1 Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project
Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project
Almost done, needs some detail completed, should be useful as stands, email if you have questions
- Name: Experimenting with Stepper Motors as Rotary Encoders using a PIC running BoostC Project
- Status: works in my lab
- 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
- Archive zip file see Download below.
Rotary encoders are devices that allow electronic measurement of rotation. You can find a lot of information by Googling “rotary encoder”. Many are optical with a partly transparent wheel rotating and being read by 2 pairs of led and photo detectors. Most of the old ball based mice had 2 optical rotary encoders to measure motion in the x and y direction. You can take these old guys apart to get at the encoders. Stepper motors can also be used as rotary encoders. When rotated the motors act as generators and generate the two out of phase waves that are the standard output of encoders (at least after the waves are squared up). If turned very slowly the output signal drops to a very low voltage and you can miss steps; so these guys are not perfect. A nice feature of the motors is that they often have a very nice feel in your hands especially if you put a nice knob on them. Other multi phase motors, like disk drive motors, are reputed to work as well, and often have enough mechanism on them to use as a knob. The output of the encoder can be used as a volume control, tuning device or whatever.
This project describes just enough software and hardware to let you experiment with stepper motors as encoders to use as a jumping off point for a more complete project.
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.
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.
Parts -- this is not up to date, working on it, but mostly correct.
|Power supply||Not shown, 5 volts, you can use a higher voltage with a voltage regulator. 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. Other parts of the PIC circuit not discussed here.|
|Power supply splitting resistors, form a pseodo ground at about 2.5 volts.||100 more or less|
|RIN||about 5 to 10 K|
|RFB||feedback resistor should be about 4 times higher than the input resistor|
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
reset the counters and the log of the encoder. Response something like "Reset Count"
|Log report||l<cr>||Report the log of data. A series of binary numbers, comma delimited.|
|Report on all parameters||r||Delimited by commas something like:|
|Position of the encoder||p<cr>||report the position of the encoder, and other counts of encoder activity.|
|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
There are several different ways the software can work:
- Simple polling when the encoder is checked as often as you can get to it.
- Timer driven interrupt programming where the encoder is checked based on some timer.
- Interrupt driven programming where changes in the encoder output trigger the interrupt ( I have not worked this one out here )
The software remembers the last state of the encoder and compares it to the current state: one of the following must be true:
- There is no change which we take to indicate that there is no rotation ( true if we have not missed any changes ).
- There is a change indicating a positive or negative rotation of one step ( which we add or subtract from our step count )
- There is a change but it does not correspond to a one step rotation, this can happen when we miss a step or when the hardware sends bad data. It can easily happen any time rotation speed drops so low that the hardware cannot detect the rotation, or when the rotation speed starts up and our old rotation state is not accurate. The software counts this as an error.
All variables are unsigned, this keeps speed up. Since the rotation is bi-directional the reset of the count is to a middle value for the range of the variable 32000.
The software also logs the data received any time it changes, this is limited to about 100 changes because a whole byte is use for logging, with a little packing of data 400 changes could be recorded.
Control of the software is via the serial communications library that I have described in other articles. It yields a command driven interface described above.
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:248 bytes (67.4%), free:120 bytes (32.6%),
- Heap size:120 bytes, Heap max single alloc:95 bytes
- ROM available:8192 words, used:1239 words (15.2%), free:6953 words (84.8%)
The program seems to be safely 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()
- Read multiple encoders.
- Optical encoders are easier to read, signals do not depend so much on rotational speed, try using them.
- Use as a control element in a larger project, aming a laser.
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
Rotary Encoders http://www.ubasics.com/adam/electronics/doc/rotryenc.shtml
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/
Comment, Questions, Contributions?
Email me russ_hensel, or use the talk page for this topic. All feedback is welcome.