I am trying to build a RPM controller for my project. I am using a quadrature encoder (ppr=400) and Arduino Uno for this purpose. I am using the code given below inorder to caluculate the RPM of the DC motor. I get a reading on my serial Monitor. However, this RPM doesn't match with my manual calculation (i.e when I calculate the RPM Manually using a stop watch- for lower speeds).

EDIT-> Eg- Manually I get around 10 RPM, but my program shows 60 and more.

I have used one interrupt pin on my Arduino UNO.

#define encoder0PinA  2
#define encoder0PinB  3
int power=6; // to power up the encoder
int gnd=4;  // to provide ground to the encoder
int motor=11; //motor PWM pin
int dir=10;
volatile unsigned int encoder0Pos = 0;

void setup() {
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH);       // turn on pull-up resistor
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);       // turn on pull-up resistor

  attachInterrupt(0, doEncoder, RISING);  // encoder pin on interrupt 0 - pin 2
  Serial.begin (9600);
  Serial.println("start");    // a personal quirk
  analogWrite(motor,120); // to set a particular test RPM to the motor.

void loop() {
  float rpm=encoder0Pos/(400);

void doEncoder() {


Also I tried the very first code on the following link "https://playground.arduino.cc/Main/RotaryEncoders".. In this case my Arduino stops transmitting completely. I tried the same code on one of my friend's setup.. surprisingly it works there. Can't understand where I am going wrong... Is my coding approach correct?? Please enlighten me... Thank you.

  • 1
    When you say your calculation doesn't match -- please describe the values each produces. There are many ways in which something can "not work" Feb 16, 2018 at 18:54
  • sure sir, I will edit it ASAP.
    – CR7
    Feb 16, 2018 at 18:59
  • Is your quadrature encoder optical or mechanical? Feb 16, 2018 at 19:11
  • It's an optical quadrature encoder
    – CR7
    Feb 16, 2018 at 19:15
  • Encoder link- m.aliexpress.com/item/32816515228.html
    – CR7
    Feb 16, 2018 at 19:18

1 Answer 1


Ensure the encoder outputs 400 pulses on each phase. It could be 200 on each phase for one rotation

Rather then clearing your pulseCount, figure out the time it take to make one rotation (as close as you can tell) and turn the motor on for only that amount of time, and then off again. This will allow you a more controlled environment to test your code in, and it will be the same(almost) every time.

You are calculating revolutions per second with your code, not per min number of pulses/400(if this is correct number)=Revolutions which you are clearing ever 1000ms

Ensure good connections on your wiring as it does not take much to allow the interrupt to fire twice when it should have only fired once.

Use the second phase of the encoder, this is what gives the ability to decipher bad signals and change of dirrection, ie- multiple A pulses in a row(without any B pulse) does not equal movement, it means either the motor is hanging out right on a phase line, or something is causing the interrupt to fire more often then it should.

  • Why is the code from Arduino playground not working?
    – CR7
    Feb 17, 2018 at 3:03
  • Getting direction is not an issue for me... Getting Rpm or RPS is my main concern..
    – CR7
    Feb 17, 2018 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.