program to find rpm

  • arduino uno
  • input digital - proximity sensor would this program work to find the rpm of a wheel with a proximity sensor

int sensorpin=1; //sensor at pin 1 long int previoutime=0; int value,count,rp,rpm; long int interval=500; //calculating number of rotates every 500ms

void setup() { Serial.begin(9600); pinMode(sensorpin,INPUT); }

void loop() { value=digitalRead(sensorpin); long int currenttime = millis(); if(value==1) count++; //count gives the number of highs ergo giving number of rotations if((currenttime-previoustime)>interval) //if time elapsed is more than 500ms { if(value==0) //if there are no rotations { count=0; rpm=0; Serial.print(rpm); } rp=count/interval; //rotates per 500ms rpm=rp*120; //rotates per minutes previoustime=currrenttime; Serial.println(rpm); rpm=0; count=0; } delay(10); }

Not being funny, but why ask us? Why don't you test it?

In any case it's difficult to understand someone else's code with not on single comment in it, and we don't know what a "proximity sensor" actually is.

One thing I do see though, is that with spin = 1, a digitalRead(spin) will be reading pin 1 which is a no-no when you use Serial, since it also uses that pin.

I don't have the hardware now , i can't check.

proximity sensor is an inductive sensor which gives high when a metal is at its close proximity

Well you can at least try to compile it: you don't need the hardware for that.

Right now it doesn't compile: you have a typo in one of your variable names.....

Make life easier for everyone and use the # button above the :sweat_smile: to put your code in tags

so that
{
it looks like
}
this...

And as I said earlier, if you use Serial, don't use pin 1. In fact don't use pin 1 any way.

Also bad practice to change your code in one post since then the comments after it make no sense to other readers. Right now my previous comment on "spin" now makes no sense since you changed the name to "sensorpin"

thank you for helping a newbie

program will compile .. i just need to know whether the logic is correct

I think I see a potential problem in your code.... I haven't had a long look (I'm at work, 15h20 here), but I think you'll get multiple counts per pass of the sensor. The pin may be read more quickly than the effect of the passing metal and you'll get many count++'s per pass.

I think you may need to look at interrupts, and look for the edge of the signal, rather than its presence.

What is the delay for?

Any variables that record millis() or micros() or do any calculations with them must of type unsigned long. Yours are just long.

You can calculate RPM one of two ways. You can see how long it takes for 1 or 5 or 10 revolutions or you can see how many revolutions happen in a fixed time. On the Arduino it is probably easier to see how long it takes for N revolutions because it can measure time in millisecs or microsecs but, at the end of a fixed time period the shaft might be halfway through the next revolution and you would not have measured that half.

If, for example, you want to measure the time per revolution averaged over 5 revolutions I think this code will work. The value in millisPerRev will be updated every 5 revolutions. This is not a complete sketch.

unsigned long millisPerRev;

unsigned long startTime;
unsigned long endTime;
byte revCount = 0;
boolean readingRevs = false;

void setup() {
  // usual stuff
}


void loop() {
   updateMillisPerRev();
   
   
}

void updateMillisPerRev() {
   if (! readingRevs) {
      revCount = 0;
      timeForRevs = 0;
      readingRevs = true;
   }
   if (digitalRead(sensorPin == HIGH)) {
     revCount ++;
     if (revCount == 6) {
        endTime = millis();
     }
     if (revCount == 0) {
        startTime = millis();
     }
   }
   if (revCount == 6) {
      millisPerRev = (endTime - startTime) / 5;
      readingRevs = false;
   }
}

...R

JimboZA: I think I see a potential problem in your code.... I haven't had a long look (I'm at work, 15h20 here), but I think you'll get multiple counts per pass of the sensor. The pin may be read more quickly than the effect of the passing metal and you'll get many count++'s per pass.

I think you may need to look at interrupts, and look for the edge of the signal, rather than its presence.

rather than test for HIGH like this:

  value=digitalRead(sensorpin);
  long int currenttime = millis();  //not relevant in this example
  if(value==1)
    count++;                                                                         // NOW count actually gives the number of highs

try to test for a state change like this:

value=digitalRead(sensorpin);
if(value==1)
{
  if (lastVal == 0)
  {
    count++;  // this will count the state change
  }
}
lastVal = value;

but then again, it may be so fast you need to use an interrupt...

BulldogLowell: but then again, it may be so fast you need to use an interrupt...

Note that interrupts have overhead too.

Threads on close timing in the archives here use 'timing loops' and sequences of nop commands to achieve under 4 microsecond to sub-microsecond measures.

But how many rpm's would that take? Kind of scary, no?

For really, really fast I would try an external CTC (counter timer chip) to generate an interrupt every so many revs. At least I think that there are CTC's cable of operation at many times 16 MHz.

well, thanks for the help .

Thanks for answering questions. I wouldn't know what to do without my mind reading cap. Oh wait! Where did my cap go?

new to arduino need to know how to use interrupts i want to find the number of HIGHs from IR sensor

thnx for the help

Take a look here http://playground.arduino.cc/Code/Interrupts
What ir sensor?

Take a look at the attachInterrupt() function. You'd attach an interrupt handler to RISING mode. Then your code would get called each time the pin you are monitoring switches from low to high.

Remember to keep your interrupt handlers short so they don't interfere with the functioning of the rest of your program.

At the simplest, your interrupt handler could just increment a global counter, which you could then read from the loop function.

Yes, see interrupts in the Reference tab.

thank you for the help !! got it done

its infrared sensor

can you help me find number of rotates per minute of a wheel attached with IR sensor.

fiosnetwork27:
thank you for the help !!
got it done

its infrared sensor

can you help me find number of rotates per minute of a wheel attached with IR sensor.

I guess that if you count how many times the encoder gets either high or low, then you will get the number of rotations… Of course you will use a counter which increases +1 every time…

This Nick Gammon link is also worth reading.

...R

need a program to find the rpm of a wheel using a proximity sensor...