# Can I use a motor as a rotary encoder?

Let me preface this by saying that I'm not an expert, nor do I conclusively understand how op-amps work, but I have a strong suspicion that this should be possible, at least crudely.

I have this idea that by using op-amps to measure the voltage difference across the terminals of a DC motor, amplify that difference, then take the integral of that voltage with respect to time, I could represent the net rotation of the motor as a voltage on an output pin, eg. 2.5 V @ 0 turns, 3.0 V @ 1 turn CW, 2.0 V @ 1 turn CCW, etc.

The voltage across a motor's terminals is proportional to its speed, so in theory (in math) I should be able to integrate and get position. However, I would also need to know how an op-amp works well enough to be able to implement it. In my defense, I really thought I did.

Is this even possible? I'm not very concerned about it being practical (I am a mathematician at heart, I'm on the wrong side of stack exchange here), I just want to know if these principles do, in fact, work this way.

Is an op-amp even the right component to use? If comparing, amplifying, and integration is the way I'm going about this, are there different chips I can use instead? I am aware of the broader strokes and concepts, that op-amps compare voltages, multiply, take differences, do integrals, etc. but the finer details of use and implementation are very much so lost on me.

Does anyone have any links to resources to better get a handle on this kind of stuff? I find electronics absolutely fascinating, but it seems to have a very high barrier to entry. Any piece I read about using op-amps is either super dense with an expected background or so vague that it may as well be talking about Alchemy. I took AP Physics in high school, and we had an electricity chapter, so that's a good foundation for first principles, but there are second principles, third principles, fourth, fifth, etc. that I am in desperate need of. I would really love to be able to try out things like this and learn firsthand the difficulties of implementation. Ultimately, that will make any further ideas better conceived and executed.

If, by a long shot, this circuit is possible and practical, please try it yourself. I would love to hear others' opinions on the functionality of such analog devices and how to make them better.

• sounds somewhat possible, but note that analog integration (even digital integration) is always going to drift somewhat. Integrators are often made leaky (drift towards 0) on purpose and I think analog integrators are always going to be leaky just because they're analog. You wouldn't want to rely on it holding a steady value for a long time. May 22 at 21:37
• Interesting... In this way, you can get with a relatively small error a voltage proportional to the number of rotor revolutions (the total traveled "path"). Similarly, counters in cars integrate pulses from the rotation of the wheels to calculate the distance travelled. This digital way is much more accurate than the analog one you offer. May 22 at 21:48
• @Circuitfantasist either digital or analog, integrating an analog value isn't great. Counting digital pulses is much better. May 22 at 21:55
• @Circuitfantasist The digital way is definitely more accurate. Black and white are much easier and cleaner to interpret than shades of gray. I think a big motivation for this design is to have a single output that can determine both amount and direction of rotation, and gives net rotation. Another motivator is having an analog output: digital outputs give some sort of code that must be translated, while analog devices give an analogous measurement. I recently implemented a rotary encoder that uses quadrature encoding, but intepreting "10000111" as clockwise 1/12th of a rotation felt odd. May 22 at 21:57
• Isaac, I like your idea. It has an additional advantage - it will integrate (smooth out) the collector voltage spikes. May 22 at 22:05

In principle it could work but it won't be ideal.

All analog circuits suffer from imperfection. Integrating makes it a lot worse because it stacks up over time. Integrating any non-zero offset will create a permanent drift. If the input is even 0.0001V when the motor is still (are you sure your motor won't pick up 50Hz mains hum and Earth's magnetic field?), the integrator integrates that and the output tends towards infinity (or at least towards the supply voltage). No matter how perfect your circuit is it won't be completely perfect and this will be a problem.

It is advisable to make integrators "leaky" so they reset themselves towards zero, cancelling out drift to a limited extent, but then they can't hold a position for a long time, which is something you want.

Also keep in mind the output is limited to the op-amp's supply voltage. You need to provide both + and - supply; the output can only be in between them. (A lot of circuits use 0V as the - supply and the output can't go negative)

Because of the drift problem it seems a lot more sensible to use a quadrature encoder if you need a cumulative rotation measurement (like a car stereo volume knob) or some type of absolute position sensor (e.g. Gray code encoder, potentiometer, or synchro/resolver/RVDT) if you need an absolute position measurement. None of these techniques have drift because they do not involve integrating analog values.

As for the second part which is how to construct an integrator using an op-amp, it's not that hard. The basic idea of op-amp circuits is that the op-amp magically figures out the right output voltage which causes the input on the + pin to be equal to some feedback circuit voltage on the - pin.

If we construct a feedback circuit that takes the derivative of the output (easy with a capacitor in the feedback), the input will magically equal the derivative of the output (plus imperfections), which means the output is magically integral of the input (plus imperfections). To make it leaky, add a large resistor in parallel with C1 so that it tends to discharge C1 back to zero volts. simulate this circuit – Schematic created using CircuitLab

"Circuit fantasist" pointed out a flaw: the output has to output the voltage for not just the capacitor (the voltage that gets integrated) but also for the resistor (which gets the same voltage as input) therefore the output is the integral plus the input. We can avoid this problem by making an inverting circuit instead. It's quite similar, but instead of getting the op-amp to make the feedback equal to the input, we use it to make the feedback+input equal to 0 volts (i.e. feedback = -input). Note that this means the input is now reversed - the integral will go up when the input is negative and down when it's positive. Really it's the same mathematics as before, but we added -input to both op-amp pins. simulate this circuit

• To think of an integrator as an "inverted differentiator" (via negative feedback) is a very sophisticated explanation (in the same way, a non-inverting amplifier can be thought of as an "inverted voltage divider"). Another more conventional approach is to consider the op-amp and resistor as a constant current source charging the capacitor. But this non-inverting configuration has a significant disadvantage compared to the inverting one - the output voltage is the sum of the input voltage and the voltage across the capacitor (Vc is "lifted" by Vin). The inverting integrator does not have it. May 23 at 19:18
• @Circuitfantasist I don't think it's that sophisticated. Good point on the drawback of this circuit. I suppose it is better to make an inverting integrator that balances both inputs at 0 volts and outputs the negative integral. I think that the reason I thought of the non-inverting version first is that it's conceptually simple. May 23 at 19:21
• By the way, the inverting integrator is also an "inverted differentiator". The RC circuit is a differentiating circuit whose input is connected to the op-amp output; the voltage drop across the resistor is its output (but here it is "floating"). It is subtracted from the input voltage in series manner and the resulting voltage is applied to the op-amp inverting input. The op-amp makes it zero ("virtual ground"). In this way, as though the input voltage is applied across the resistor and the voltage across the capacitor appears as a "mirror copy" at the op-amp output as an output voltage. May 23 at 19:33
• Ok, since I see you've also added the inverting integrating circuit, now is a good time to show the general idea in the two op-amp integrator implementations (at least I'm not aware of this being done): In both, the op-amp adds an equivalent voltage to the voltage drop across the capacitor, thus compensating for it. As a result, the current remains constant (I= V/R) and the integration is perfect. This addition is more difficult to see in the inverting circuit where the output voltage is of opposite polarity to the input with respect to ground but in the loop it is of the same polarity. May 23 at 20:31

is this even possible? I'm not very concerned about it being practical

That depends on your relative definitions of "possible" and "practical".

It is so severely impractical as to be practically impossible, but it may be that some 1950's weapons system did it successfully at a cost of millions of dollars.

The problem is that while DC motor theory says that the motor output is $$\v(t) = k_T \omega_a\$$, where $$\k_T\$$ is the motor constant and $$\\omega_a\$$ is the motor armature speed, in reality, $$\v(t) = k_T \omega_a + n(t)\$$, where $$\n(t)\$$ is, at best, Gaussian zero-mean noise, and is, at worst, Gaussian zero-mean noise, some offset voltage, and every imperfection of the motor, rolled into one.

The faults of a DC motor can be somewhat overcome by using a tachometer -- which is basically a DC generator that's specialized for the task of reading out speed. But it still won't be very practical.

Any practical application for this at all would involve needing to know the relative position of the motor for a short period of time -- where "short" is defined by how accurate the estimate needs to be and the project budget.

is an op-amp even the right component to use?

Yes, sort of. But see "project budget", above. Today the best -- or at least the least-worst -- way of doing this would be a high-precision analog to digital converter followed by a microcontroller that does the actual integration.

There are far cheaper solutions to this problem. Use an incremental encoder and an index, or a resolver, or an absolute encoder. The possibilities aren't endless (they end, for instance, short of a tacho-generator and an integrator), but you are definitely spoiled for choice.

• Definitely a microcontroller is the most optimal solution here. At first I had proto'd just the idea of the voltage difference and integration using a microcontroller, and it worked well enough for me to consider it 'responsive' and accurate. While there are cheaper solutions, I don't exactly have a problem, or even an application in mind. The problem is "can I do it this way?", as opposed to "how do I acheive a similar result?". The cheapest solution to that problem is one that says "don't understand how it works, just find something that does and plug it in". May 22 at 22:24
• "It is so severely impractical as to be practically impossible, but it may be that some 1950's weapons system did it successfully at a cost of millions of dollars." Dang... Maybe I can train some pigeons to help me counteract the voltage drift and noise. May 22 at 22:44

The voltage is proportional to the velocity so for small velocities the signal disappears into noise (and noise is inevitable). You can move the motor slowly enough all day and it would not be possible to detect it.

There may be applications where the shaft is always turning at a reasonable speed and you don't care if it misses displacement at low speeds.

This is a similar issue that comes up when you compare a variable reluctance sensor with a Hall sensor. The latter works down to zero frequency.

• What's important is not how slow the velocity, but how much movement there is between velocity changes. A slow but prolonged motion makes a great signal. (My PhD thesis concerned discerning rates when rate changes were sparse but occurred at unknown times -- in this scenario dynamic programming outperformed conventional linear filtering by orders of magnitude) May 23 at 20:18
• @BenVoigt Is your thesis online? It outperforms 'optimal' (eg. Kalman) filtering? May 23 at 21:02
• Perhaps I should clarify that the dynamic programming component partitioned the signal, and within each partition a linear curve fit was used. repository.upenn.edu/edissertations/1158 I guess "covariance resetting" of a KF would be a valid way to think about the partitioning, but Kalman theory leaves it an open question when to perform the covariance resetting -- this is what the dynamic programming search solves. May 23 at 21:17
• pages 56 and 59 will give a rapid visual expression of the problem I was solving May 23 at 21:22