Understanding pulseIn

I am trying to measure the frequency of rotation of a motor. I have a led and detector on either side of the motor. The motor has 3 arms so I get a signal 3 x the rotation. I have measured this waveform with a scope and its 20.89Hz BUT my code shows 22.95Hz. Multiplying this by 0.91 gives the the correct answer.

Any ideas why it won't read the correct frequency ?

Thanks

void setup()
{
pinMode(2,INPUT);digitalWrite(2, HIGH);
Serial.begin(115200);

}
void loop()
{
float freq = 0;
float ontime1 = 0;
float offtime1 = 0;
float ontime2 = 0;
float offtime2 = 0;
float ontime3 = 0;
float offtime3 = 0;
float period = 0;

ontime1 = pulseIn(2,HIGH);
ontime2 = pulseIn(2,HIGH);
ontime3 = pulseIn(2,HIGH);
offtime1 = pulseIn(2,LOW);
offtime2 = pulseIn(2,LOW);
offtime3 = pulseIn(2,LOW);
period = ontime1+offtime1+ontime2+offtime2+ontime3+offtime3;

freq = 1000000.0/period;
Serial.print (char(82));Serial.print (char(65));Serial.print (char(58));Serial.print (freq);Serial.print (char(10));

delay(100);
}

Please follow the advice on posting code given in posting code

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

pulseIn() returns an unsigned long not a float. But why not print the values you're getting to see where the error is coming from?

Steve

Serial.print (char(82));Serial.print (char(65));Serial.print (char(58));Serial.print (freq);Serial.print (char(10)); Is this for a code obfuscation contest?

   float freq     = 0;
   float ontime1  = 0;
   float offtime1 = 0;
   float ontime2  = 0;
   float offtime2 = 0;
   float ontime3  = 0;
   float offtime3 = 0;
   float period   = 0;
   
   ontime1  = pulseIn(2,HIGH);
   ontime2  = pulseIn(2,HIGH);
   ontime3  = pulseIn(2,HIGH);
   offtime1 = pulseIn(2,LOW);
   offtime2 = pulseIn(2,LOW);
   offtime3 = pulseIn(2,LOW);
   period   = ontime1+offtime1+ontime2+offtime2+ontime3+offtime3;

Or do you have to write a minimum number of LOCs?

Please remember to use code tags when posting code

Thanks for the replies. I have changed to unsigned longs and printed the on and off times. They all add up to the reported frequency BUT I still believe it is incorrect.

Reported freq ontime1 offtime1 ontime2 offtime2 ontime3 offtime3 sum calc freq
23.14 6602 7730 6802 7559 6730 7795 43218 23.1385071035217
23.13 6802 7572 6722 7792 6601 7748 43237 23.1283391539654
23.13 6727 7790 6595 7747 6815 7563 43237 23.1283391539654
23.12 6598 7738 6822 7566 6738 7789 43251 23.1208526970475
23.14 6803 7566 6728 7780 6602 7731 43210 23.1427910205971
23.13 6735 7784 6592 7738 6820 7574 43243 23.1251300788567
23.12 6595 7729 6816 7575 6735 7801 43251 23.1208526970475

I couldn't solve this but the following code gives the correct answer...

unsigned long time_start = 0;
unsigned long time_end = 0;
float freq;

void setup()
   {
   pinMode(2,INPUT);
   digitalWrite(2, HIGH);
   Serial.begin(115200); 
   }
void loop()
{
   while(digitalRead(2) == 0){ }
   while(digitalRead(2) == 1){ }
   time_start = micros();
   while(digitalRead(2) == 0){ }
   while(digitalRead(2) == 1){ }
   while(digitalRead(2) == 0){ }
   while(digitalRead(2) == 1){ }
   while(digitalRead(2) == 0){ }
   while(digitalRead(2) == 1){ }
   time_end = micros();
   freq = 1000000.0/((time_end - time_start));
   Serial.print (freq);
   Serial.print (char(10));
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.