Friday, October 19, 2018


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
( 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
Picture of 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 stepper motor with 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.println("Basic Encoder Test:");
long oldPosition  = -999;
void loop() {
  long newPosition =;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;

Step 3: Results
Picture of 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. If You have any question about stepper motor, you can visit our website for help.

1 comment:

  1. I got an excess of fascinating stuff on your blog. I surmise I am by all account not the only one having all the delight here! Keep doing awesome. test and tag services Adelaide


Introduction to the functions and troubleshooting of closed-loop stepper motor drivers

1.Overview A closed-loop stepper motor driver is a motor driver that integrates multiple functions such as speed regulation, positioning, i...