Rotary encoder sabotaged by evil drive motor

Hey All-
Thanks to everyone who helped me get my rotary encoder up and running a bit ago. I switched from mechanical to optical like most of you suggested (Aligent Technologies HRPG series). It's working now...part of the time.

When I spin the shaft by hand it registers correctly. I've tried spinning both directions by hand at about .5 RPM and up to about 300 RPM. It registers correctly, registering approximately 500 “clicks” per rotation and counting properly on my readout.

But when I mechanically attach the encoder to a drive shaft on my machine and then run the machine, the encoder gives very erroneous measurements. The machine's shaft spins the encoder quite slowly, ranging from .25 RPM to about 10 RPM in either direction. With the machine driving, the readout I have hooked up shows unpredictable counting, sometimes jumping back when the count should be going up, and generally counting orders of magnitude too fast.

While the machine is stopped the encoder readout is static.

The encoder has five leads, which i've wired as follows:

Data Line 1 with external 10k pull down
Date Line 2 with external 10k pull down
+5 V coming from the arduino
Ground
“Body” ground

The cable between the encoder to the control box is about 50' of 20 GA unshielded wires.

Any thoughts about how to chase down/correct the problem would be appreciated. I'm not very experienced with electronics so please keep that in mind with your explanations.

Thanks!

"The cable between the encoder to the control box is about 50' of 20 GA unshielded wires."

I would suspect you're having high-frequency noise induced in your encoder lines. You definitely should be using shielded wire. Otherwise, you have a nice 50' antenna. Try to reduce that length as much as possible.

If you are going that long of distance I would use differential drivers at the encoder, use shielded twisted pair cable, and differential receivers at the Arduino.

Avoid running the encoder and motor cables together.

You need to add some decoupling:-
http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Thanks! We have some shielded cable that I'll try, and will read about and add decoupling.

Much appreciated advice. I'll let you know what happens.