# Need super-basic help with motor encoder

So I have this motor: https://www.servocity.com/23-rpm-hd-premium-planetary-gear-motor-w-encoder

and this motor shield for my arduino uno: https://www.pololu.com/docs/pdf/0J49/dual_vnh5019_motor_driver_shield.pdf

And no background in electronics to speak of. I cannot find a basic enough resource to tell me where to attach the wires, or really what they each do. The ones that are simple enough for me only deal with motors with a couple of wires. Information on motors with six wires go immediately to discussing things that are way beyond me, or at least use terms that I don't know.

So the motor has six leads, and the only info I can find on them in the documentation are this: Black (Motor -), Red (Motor +), Green (Ground), Brown (Channel B), Yellow (Channel A), and Orange (Sensor Voltage +).

I have the black and red hooked up to M1A and M1B on my shield, so I am able to turn the motor on, vary its speed and direction. I also had all the other wires hooked up to various pins mentioned in the Demo sketch associated with the shield.

I have spent a week trying to figure this out on my own, but I am not getting anywhere (except that I know that hooking up orange to almost anything triggers a fault).

So in addition to the four wires that are not connected to anything, I have these pins mentioned in the sketch that are also not connected to wires: D2 M1INA, D4 M1INB, D6 M1EN/DIAG, D9 M1PWM and A0 M1CS.

I hope someone can provide a pointer to a resource or a good, plain-language explanation for what these wires and pin descriptions mean. Thanks in advance!

# servocity

The "Green (Ground), Brown (Channel B), Yellow (Channel A), and Orange (Sensor Voltage +)" wires connect to the encoder on the back of the motor and allow the use of quadrature to detect the number and direction of motor turns.

As noted on servocity's page for the motor, you can count the following numbers of cycles or events per rotation of the motor or of the output shaft:

Encoder: Cycles Per Revolution (Motor Shaft)                12
Encoder: Cycles Per Revolution (Output Shaft)            4,428
Encoder: Countable Events Per Revolution (Motor Shaft)      48
Encoder: Countable Events Per Revolution (Output Shaft) 17,712


An "event" refers to an edge, up or down, on either of the encoder channels. A "cycle" refers to the two channels going through the four phases of one quadrature-encoder cycle.

Given the gear ratio 369:1, the motor probably spins about 8500 RPM to make 23 RPM on the output shaft. A software-based quadrature decoder might have trouble keeping up with 6800 events per second (ie, 48*8500 = 408000 events per minute, or 1/60 as many per second) if the Arduino will be doing anything else besides quadrature decoding. You could combine some of the code shown at an arduino.stackexchange answer with that at another arduino.stackexchange answer to see how many events per second you could count using software on an Arduino. The 147 microseconds available per event (ie, 1000000 us / 6800 events) might or might not be enough; most likely you will need to use a hardware-based quadrature decoder and counter to track shaft location.

[Edit 3:] If the motor parameter you wish to control or measure is speed rather than position, a typical ATmega328-based Arduino board can count enough encoder events to give that information for a single motor. For example, if using an Uno you could connect one encoder line to PD5, the T1 input pin on a '328. With the motor running at full speed, Counter 1 would overflow about twice a second. In its interrupt handler you would read the current value of micros, store it in a volatile variable, and set a change flag. The interrupt handler could also add or subtract the counter value to a position total.

[Edit 2:] Regarding the wiring, it appears that you should do like the following:

Pin# Color   Function             Hook to...
3.   Green   (Ground),               common ground
4.   Brown   (Channel B),            quad. decoder input 2
5.   Yellow  (Channel A),            quad. decoder input 1
6.   Orange  (Sensor Voltage +)      V+ (eg to logic +5 or +12)


See wikipedia's Rotary Encoder article for more discussion of quadrature encoding/decoding, along with signal diagrams, etc.

See pololu's "Using the Driver in Single-Channel Mode" page for an explanation of pins like M1INA, M1INB, M1EN, etc.

• It took me a while, but I think I see what you meant. I corrected the physical connections and used the first code example you suggested to read in milliseconds how long Channels A and B were open. I am (slowly) starting to extract from the second code sample how to count pulses instead of their durations. After I get all that done, I will connect my second motor and get both counting. At that point, I expect I will have totally swamped the arduino and will have to figure out a separate piece of hardware to do the encoder. But thank you so much for the toe-hold on this, I was way off course! – sparse Sep 15 '16 at 18:28
• @sparse, I added a paragraph about using counter/timer 1 to measure motor speed. A typical Arduino can do that without being swamped. – James Waldby - jwpat7 Sep 16 '16 at 5:23
• I corrected events-per-second from 400000 to 6800, per comments/edit by @Michael Snower. Thanks! Also showed the calculation. – James Waldby - jwpat7 Oct 30 '18 at 6:44