Using an Arduino to read RPM from the Hobbywing RPM sensor
I needed a quick way to get RPM data from my Brushless RC motor as my ESC did not support the feature. After a visit to Ebay and $4 later, I had a Hobbywing RPM sensor for brushless motors.
There wasn’t much documentation that came with it except connection instructions which were great for hooking it up, but not for how to use it with an Arduino. I checked online and found a few sketches, but not one of them worked correctly for some reason. I also hit a few snags writing my own function since I was not familiar with interrupts, but with the help of some good folks at the Arduino forums I was able to get it working without too much hassle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//RPM SENSOR #define INTERRUPT_PIN 0 // Arduino Mega digital pin 2 volatile int interruptCount; float rpm = 0; float numpoles = 18; //Change value to the number of magnets in the motor. void setup() { Serial.begin(9600); // RPM COUNTER INIT pinMode(INTERRUPT_PIN, INPUT); attachInterrupt(INTERRUPT_PIN, interruptFired, CHANGE); } void loop(){ checkRPM(); } // Check RPM Function void checkRPM() { noInterrupts() ; interruptCount = 0; // set variable in critical section interrupts() ; delay (100); noInterrupts() ; int critical_rpm = interruptCount ; // read variable in critical section interrupts() ; rpm = ((critical_rpm)*(60))/(numpoles)*10; Serial.print("Motor RPMs : ");Serial.println(rpm); } void interruptFired() { interruptCount++; } |
The key thing to understand here is that to sync the interruptCount++ variable into the checkRPM() function, it must be guaranteed not to be interrupted or the data can get garbled. In the checkRPM() function you can see how noInterrupts() was called to disable interrupts prior to working with the interruptCount variable, and after the variable was accessed, the interrupts() function was called to re-enable interrupts. The same is to be said of setting the critical_rpm variable to the interruptCount value. Without these critical sections, the code returned strange values depending on usage.
How Accurate is the Hobbywing RPM sensor?
I actually do not know, I haven’t tested the Hobbywing sensor RPM against any other verified method. If however I follow the rule of using RPM = Kv rating of the motor multiplied by # of Volts, I can ballpark the RPM by trial and error using my power meter.
For example, in the sketch above, I have a variable ‘numpoles’ which I initially set to 22 (since my motor has 22 poles). However, when I compared the RPM output to the RPM the motor was rated for, it did not match. I just lowered the number of poles until the KV * Volts = RPM calculation, and RPM sensor values matched.
Does having an accurate RPM measurement really matter?
In this autonomous boat, I don’t feel that having a perfectly accurate RPM reading is necessary. My main goal is to check to see if the motor has stopped. If it has, then the motor monitoring function will restart the motor automatically. In the functions i’m writing, I do not take specific RPMs into account and currently use the sensor as an on off switch (If motor on then x). I may include an RPM measurement in the tracking payload, but its low on the list of priorities at the moment.
hello, nice work for this program, but i have a problem with this sensor, when motor not running, sensor still have value and not zero, i wish we can have some discussion. thanks
This may happen if you are using longer wires and/or using it near other electronics. You will need to write some code to subtract the erroneous number when motor is not running.
where the reference equation of rpm ?
journal or else you get