RPM Measurement  - Page 1
Forum section: electronics / beginners

author: yashrk
author_rank: Frequent Contributor
timestamp: December 16, 2015, 06:39:32 pm
content: Hey Guys,I am working on RMP measurement program on arduino, most of the rpm measurement reference code runs for ever and I don't want that I want to implement it as function which I can call when ever RPM value is necessary. So I wrote the code and it did complied but I am waiting on my hall effect sensor to arrive so I cannot test it right now so I just want to show you guys the code and see if the general implementation is correct or not? Code certainly is not the finished as you will see I don't make any use of rpm value in the loop but right now I just want to figure out the rpm part is right.Here is the codeboolean pool = 0;unsigned long lastmillis = 0;int rpm = 0;volatile int rpmcount = 0;void get_rpm(){lastmillis = millis(); //as its a funtion which can be called at any time we upadate current timeattachInterrupt(0, rpm_fan, FALLING); //attach interrupwhile(pool==0){ //wait for 1 sec to complete in the ISR//do nothing}pool =0; //reset poolrpm = rpmcount *60; //convert to rpmrpmcount = 0; //reset rpmcount}void rpm_fan(){ //things to do when there is an interruptif (millis() - lastmillis == 1000){ //after one seconddetachInterrupt(0); //Disable interruptpool=1;}else rpmcount++;}void setup() {}void loop() {get_rpm();}

author: AndreasF
author_rank: Frequent Contributor
timestamp: December 16, 2015, 07:10:59 pm
content: Quote from: yashrk on December 16, 2015, 06:39:32 pmA few issues: since you only check if the the 1 second has elapsed in the interrupt, you might end up waiting forever for "pool" to turn to 1. Also since you test against equality between lastmillis and millis() in the interrupt, what if one interrupt triggers at millis()-lastmillis = 999 and the next one doesn't trigger until 1001?Another problem you might encounter is that your interrupts take too long to process and thereby miss pulses from your hall effect sensor. If all you want to count such pulses there are specific modules to do that, not surprisingly called counters (often part of a timer peripheral). However, I don't know how well the Arduino environment supports it.PS: use the [ code ] tags to make code easier to read.

author: yashrk
author_rank: Frequent Contributor
timestamp: December 16, 2015, 07:38:24 pm
content: For my application RPM is not that critical so I didn't bother trying to make counter work.I guess the both problem can be avoided by using ">=" instead of "==" for comparing the millis with 1 secThe max pulses estimated is around 90 pulses / secWill it still miss out on any pules while its executing if loop in ISR?

author: AndreasF
author_rank: Frequent Contributor
timestamp: December 16, 2015, 09:21:37 pm
content: Yes, I would definitely use ">=" or simply ">999"With only about 90 pulses per second the interrupt routine probably works just fine. You may even consider continuously updating a pulse-to-pulse interval (PPI) in the ISR, which you can then use to calculate RPM on demand whenever you need it without having to wait for 1 second:Code:...volatile long PPI = 0; //the pulse-to-pulse interval in msunsigned int RPM;...//whenever you need it:if PPI>0 {RPM = 60000/PPI; //need to avoid division by 0} else {RPM = 60000; //or choose any other maximum value if pulses come in in less than 1ms intervals}void rpm_fan(){ //things to do when there is an interruptstatic long lastmillis = 0; //because of the static keyword this will only be initialized once, and will otherwise keep its last valuelong currentmillis;currentmillis = millis(); //you only want to call millis()PPI = currentmillis-lastmillis;lastmillis = currentmillis;}For higher RPMs this may not be as accurate as counting pulses per second, for very low RPMs it might be more accurate.

author: yashrk
author_rank: Frequent Contributor
timestamp: December 17, 2015, 04:12:59 pm
content: Yeah thanx

author: tggzzz
author_rank: Super Contributor
timestamp: December 17, 2015, 11:52:00 pm
content: Quote from: yashrk on December 16, 2015, 06:39:32 pmThat is a very common situation.QuoteYou need to think about what you mean by "it".Clearly you cannot test the hall effect sensor.You can test the algorithm if you simulate the sensor in software.You can test the code if you use a signal generator where its signal is similar to that from the sensor.