3

I have a DC geared motor rated at 12V and 60 rpm . It has a quadrature encoder with 700 PPR. I connected it directly to a 9V battery (almost new) and wished to measure the rpm of that motor. To find the nature of the pulses generated by the encoder, I connected "A" channel of the encoder to pin 8 of arduino Mega and tried to plot a graph on Matlab by opening Serial port on Matlab. I sampled data every 50 micros and saved data on an array on arduino IDE, then sent it later so as to avoid the buffer delay and dependance on baud rate. From graph, the time difference between 2 pulses was found to be almost 1.6 millis from which, the speed of motor was determined to be about 53 rpm. Then, I wrote a code using interrupts to measure time between 2 pulses on arduino and print it on serial monitor. I found it close to 3 millis, which is about 30 rpm. What should be approx speed of a 12 motor at 9V? I know it is linear with voltage but practically what it should be? Here is my arduino code for finding the time between two pulses on arduino.

#define ENCODEROUTPUT 700
#define threshold 40
volatile long counter = 0; //This variable will increase or decrease depending on the rotation of encoder
int rpm = 0;
volatile long prevCounter=0;
int arr[threshold];
bool flag=1;
bool flag2=0;
int i=0;
long previousMicros=0;
  
void setup()
{
 Serial.begin (9600);
 pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
 attachInterrupt(0, ai0, RISING);
 prevCounter=counter;
 previousMicros = micros();
}
 
 void loop() 
 {
  if ((prevCounter!=counter)&&flag)
  {
   arr[i] = micros()-previousMicros;
   prevCounter=counter;
   i++;
   if(i==threshold)
   {
    flag=0;
    flag2=1;
   }
   previousMicros=micros();
   if(flag2)
   {
    for(int j=0;j<threshold;j++)
    {
      Serial.println(arr[j]);
      flag2=0;
    }
   }
   }
   
  
 }
 
 void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
 if(digitalRead(3)==LOW) {
 counter++;
 }else{
 counter--;
 }
 }
Helix xxx
  • 71
  • 4
  • 1
    60 RPM is 1 rotation in second, so 700 pulses per second. It might require reading Arduino library source code to find out if you use the functions correctly, but your code reacting to each and every pulse may be too slow, and it takes 1.04 milliseconds to transmit each character over UART at 9600 BPS. Either only count how many milliseconds it takes to read 700 pulses, or count how many pulses you get in 1 second, for least overhead of tracking pulse count or time. – Justme Jan 20 '22 at 07:16
  • @Justme thanks for answering. But in my code, i have first sampled all my data considering a delay of 50 micros and then transmitted it seperately over time. At the end i have plotted data vs sampling time which is every 50 micros. So basically the 9600 bps criteria shouldn't matter. – Helix xxx Jan 20 '22 at 11:36
  • I still think you are measuring in a too complex way. And overwriting your variables and other parts of the SRAM by never resetting i back to 0. – Justme Jan 20 '22 at 12:09
  • You have to know the motor current, and allow for the 9V battery sagging under that current. If you measured 30rpm, (from a 12V 60rpm motor) that suggests 6V which is probably all you can expect from those tiny 9V batteries (unless it's a VERY small motor) –  Jan 20 '22 at 14:56
  • Got it. Thank you ! – Helix xxx Jan 20 '22 at 15:10

0 Answers0