Arduino + 2A Motor Shield + Encoder Motor

Arduino + 2A Motor Shield + Encoder Motor




This tutorial is to verify or count the output pulses from the quadrature encoder type of motor by using Arduino.


Hardware requirements

CT UNO                                                             –Cytron Screw Terminal Shield

             CT-UNO(V2)-800x800   DSC08916


-2Amp Motor Driver Shield                                       – LCD Keypad Shield

DSC08910      7004-800x800

-1K Resistor                                                                 -Male to Male Jumper Wire

resistor-0-25w-5-1k-246-800x800         4684-800x800













Software Needed

-Arduino IDE 1.6.7


Learn More About Encoder ?

Rotary encoders measure rotation of a shaft, while linear encoders measure distance traveled. For both types of encoder, the position measurement can be either incremental or absolute. An incremental encoder measures change in position, but does not keep track of actual position. When power is interrupted, incremental encoders lose their position reference and must start over via a re-homing sequence to a reference point. Absolute encoders, on the other hand, keep track of absolute position, whether rotation of a shaft or linear travel, by assigning a unique digital value to each position. So even if power is lost, an absolute encoder will know the exact position of the shaft or the linear drive.

Incremental encoders work by producing a specific number of equally spaced pulses per revolution (PPR) or per distance (PPM—pulses per millimeter, or PPI—pulses per inch). When one set of pulses, or output channel, is used, the encoder can determine position only.  But most incremental encoders use quadrature output, which consists of two channels, typically referred to as channel A and channel B, that are out of phase by 90 degrees. Quadrature output allows the encoder to also sense direction, by determining which channel is leading and which is following. Some incremental encoders also produce a third channel with a single pulse, commonly referred to as channel Z or channel I. This channel serves as the index or reference position for homing.

With quadrature output, three types of encoding can be used: X1, X2, or X4. The difference between these encoding types is simply which edges of which channel are counted during movement, but their influence on encoder resolution is significant.

With X1 encoding, either the rising (aka leading) or the falling (aka following) edge of channel A is counted. If channel A leads channel B, the rising edge is counted, and the movement is forward, or clockwise. Conversely, if channel B leads channel A, the falling edge is counted, and the movement is backwards, or counterclockwise.

How to apply into Arduino UNO board?

Step 1

Connect the wire to the Arduino board follow the diagram.

Black = Motor GND-

Red = Motor  VCC+

Brown = Sensor VCC+

Green = Sensor GND-

Blue = Sensor Channel A

Purple = Sensor Channel B

**Note: Put the 1K resistor for each Channel.



Step 3 

Download and upload the source code of single channel to observe the channel A output, encoderA_B to observe the pulses in 2x to the CT UNO mainboard. The source code can be download at the link below.

Note:**The pulses of motor is depend on the motor ratio.

Example:Motor ratio 35:1, 7 pulses per rotation,35*7=245(For single channel only)

Cilck on source code to download.

Step 2

Mark a point at the motor to confirm that the mark match after the pulses sent.



Step 4

Press any button at the LCD when it is ready.


Step 5 

Observe the pulses at LCD and motor that is it return to begin point or not.Below show the example of the result.

For IG32E-35K, it is 7 Pulse per rotation. The result:

Type    Gear RatioPulse Per Rotation (Single Channel)
Pulse Per Rotation (A and B) X2
 IG32E-35K  35:1 245 490


Code Overview

For single channel or x1 we only need to display the output of channel A,but we also want to know that the rotation of motor in  the clockwise\counter clockwise.So in this case,we need to read the pulses of channel B whether is lagging or leading the channel A.

a                    clip_image008

In this function,we can see that if encoder B/channel B is 0 and the encoder A also 0.The pulses will count in negative, that’s mean the motor is moving counter clockwise.The encoder B is lagging the encoder A. To conclude, when the channel B is leading the channel A means the motor/encoder is moving counter clockwise.When the channel A is leading the channel B means the motor/encoder is moving clockwise.And the pulses will count when channel A is trigger to 1.

For 2x,this pulses will change whether when channel A trigger no matter 1 / 0. 

b clip_image010

Same with the preview,read the channel B to know the direction of the motor/encoder moving.

The results for 4x got little bit different because the pulses will change whether channel A or channel B trigger so we have to add a Interrupt for channel B same like channel A.




In the diagram,has display two of interrupt function A_CHANGE and B_CHANGE.