Motor encoder counting with IC

Hello All,

I'm trying to rig up my own PID motion control using a DC gear motor. So far I've got a PWM controller so I can control the speed and direction of the motor. I was able to very crudely count pulses and determine position. I was not using interrupts so I doubt it is very accurate. Instead of dealing with all of this in software, I'd like to outsource the counting to an IC. I've looked at the 74HC4020, but that's parallel output. I think I'd like to use I2C or some other serial interface to poll the counter. Any reason not to? Would the speed of serial communication be a limiting issue at all? I'm not really sure what frequency I'd need to calculate and adjust the pwm to get accurate motion control.

Here's my motor: and my driver: (I think? It does work though.) I have a Arduino Uno or BBB available for the microcontroller

200 rpm max * 3200 pulses/rev = 10,666 Hz.

Help me pick out a counter IC! I'll probably ditch this project soon and replace everything with Mesa boards and run LinuxCNC :grin:


One thing you have to be very careful of working with chips like the CD4020 - they are "ripple counters" which means that when one flip-flop flips, it triggers the next one which triggers the next etc. That means you can get a funny state where the first one or two stages have changed, but it has not made it all the way through. With your good luck, you will read the output of the counter right in the middle of a "ripple" and get a wrong number. To work with those, you have to basically read on the opposite phase of the clock that increments them (yes, ask me how I learned about that :( ) Just something to file away in your mind if you are thinking of using ripple counters.

That sounds tricky. What kind of counter is the absolute easiest and fool proof? I'd like to make this the least effort needed.

That sounds tricky. What kind of counter is the absolute easiest and fool proof? I'd like to make this the least effort needed.

:D :D

Have tried something like this ?

Have you looked for any motor encoder arduino examples ?

As the old saying goes “it’s impossible to make things foolproof - fools are so ingenious” :slight_smile: I have managed to do things that were supposedly “foolproof” myself. The 4020 is not bad you just have to be aware of that issue that applies to all ripple counters (sort of along the same line as another recent thread where someone was trying to check 4 bytes of data with interrupts running that also had access to changing the data. You can get strange results like that. What you really would want would be an up-down counter and dual pickups (quadrature basically) so if it gets 1-2, it is going one way and if it gets 2-1, then it is going the other way. You would also need to be able to reset it. It might be possible to handle 10Khz with your Uno, but it might be easier to have something like a dedicated pro mini or something handling the counting. It is not clear in your description why you need 3200 pulses per revolution. You will have to evaluate that to determine what resolution you need - if you are going to turn the motor some multiple of say 10 revolutions, then even a couple of pulses per revolution should be adequate - on the other hand, if you want to be able to turn the motor part of a revolution, then the higher count / revolution is needed. The advantage to a much lower count is much lower processor overhead (more time to do things) so you could let the processor handle the count up/down as well.

I think really what I’m looking for is a quadrature decoder, not a counter. Something that will take care of all of the timing and intensive stuff and give me a nice clean serial output when I ask for it. Anyone have experience with one of these?