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
pinMode(power,OUTPUT);
pinMode(gnd,OUTPUT);
digitalWrite(power,HIGH);
digitalWrite(gnd,LOW);
pinMode(motor,OUTPUT);
pinMode(dir,OUTPUT);
digitalWrite(dir,LOW);
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() {
encoder0Pos=0;
delay(1000);
float rpm=encoder0Pos/(400);
Serial.println(rpm);
}
void doEncoder() {
encoder0Pos++;
}
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.