# Motor With Encoder, How to Read Input Value From Encoder

30,942

67

3

Credit to : www.pjrc.com

You will need :

Motor with encoder

Arduino board ( I used arduino Uno)

Computer

Battery or DC voltage supply

Encoder library , download from https://www.pjrc.com/teensy/td_libs_Encoder.html or from the zip file attached

Background : https://en.wikipedia.org/wiki/Rotary_encoder

( 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)

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## 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 * http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/#include // 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 attachedvoid setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}long oldPosition  = -999;void loop() {
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.

Enjoy :)

## Recommendations

• ### Internet of Things Class

22,792 Enrolled

## 3 Discussions

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

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