Motor With Encoder, How to Read Input Value From Encoder




Introduction: Motor With Encoder, How to Read Input Value From Encoder

Credit to :

You will need :

Motor with encoder

Arduino board ( I used arduino Uno)


Battery or DC voltage supply

Encoder library , download from or from the zip file attached

Background :

( A little experiment I did with multimeter :

power the encoder with 2 AA battery, attach multimeter GND cable to GND of the battery , and multimeter RED cable to the C1 or C2 output of the encoder.

As you turn the motor slowly, you can see that the output voltage of C1 and C2 change from 0V to 3V.

If you have 2 multimeter, attach C1 and C2 each to a multimeter, then turn the multimeter slowly you can see the value of multimeter change from 3V-3V, 0V-3V,0V-0V,3V-3V, which is the pulse according the background theory)

Step 1: Wiring

The DC motor with encoder has 6 wires :

2 wires to power the motor (Often labeled as : M1, M2 , motor power ...)

2 wires to power the encoder (often labeled GND and 3.3V)

2 wires to send signal of position to the microcontroller (Arduino) (labeled as encoder output or C1 & C2)

First you will connect 2 wires to power the encoder to Arduino Gnd And Vcc 3.3V (in my version of encoder motor, the encoder led turn on when I connected the wire power)

Then connect 2 wires signal of the encoder to 2 pins with interrupt of Arduino , (which is pin 2 and 3 of the Arduino Uno)

You can turn the motor by hand or connect 2 motor wires to the battery to rotate the motor.

Note : normal pin will still work at low speed when you turning the motor by hand, however, when you run the motor with the battery and the motor rotates at high speed, the encoder will misread the position of the motor, result in duplication while reading. Using interrupt pin with the library prevent the encoder from making this mistake (in my experiment it was quite accurate when read the motor with interrupt pin, the only time it make a mistake is in the end when I stop the motor and the motor slowing down, that time the reading was duplicate for a few position).

Step 2: Upload the Code

After the installed the library, you can open File/Example/Encoder/Basic to run the code , or copy paste this code in :

/* Encoder Library - Basic Example
* * * This example code is in the public domain. */


// Change these two numbers to the pins connected to your encoder. // Best Performance: both pins have interrupt capability // Good Performance: only the first pin has interrupt capability // Low Performance: neither pin has interrupt capability Encoder myEnc(2,3); // avoid using pins with LEDs attached

void setup() { Serial.begin(9600); Serial.println("Basic Encoder Test:"); }

long oldPosition = -999;

void loop() { long newPosition =; if (newPosition != oldPosition) { oldPosition = newPosition; Serial.println(newPosition); } }

Step 3: Results

After successful with all above step, turn on the serial monitor :

And first turn the motor with your hand to see that the position is slightly change

Then run the motor with battery to see that position reading change rapidly as the motor run fast.

I hope you find this helpful. Thanks for reading.

Enjoy :)

Be the First to Share


    • Tinkercad to Fusion 360 Challenge

      Tinkercad to Fusion 360 Challenge
    • Stone Concrete Cement Contest

      Stone Concrete Cement Contest
    • Go Big Challenge

      Go Big Challenge



    3 years ago

    How do we program it on arduino Due for Dc motor encoders?


    4 years ago on Step 2

    my serial monitor only displays a single number on a line - either a 0 or a 1.


    Reply 3 years ago

    Same problem