I'm trying to get a RPM reading from a Hall effect sensor triggering an arduino input.
So the sensor is interfaced correctly and I'm getting a reading (for example when I'm trying with the line put under comments//, I'm getting a correct reading of period).
The problem is when I'm trying to convert from a period (in µS) to a RPM value, at this point, it will only give me wrong readings or -1???
So, since there is a division involved, is there something I should know about?
Below are the code parts you're interested in, to sum up : if I put the first line under comment and only use the second one (commented right now), I get the period reading and it works, if I try to get the RPM, it won't....
long int RPMN1
long int RPMN1count
attachInterrupt(0, N1, FALLING);
void N1()
{
RPMN1=(60000000/(micros()-RPMN1count));
//RPMN1=RPMN1count-micros();
RPMN1count=micros();
}
Problem is :
I tried it with a tone(pin, frequency) instruction and it worked all the way up to 18000Hz, it gave the right 540 000 RPM reading.
BUT... when I plug it to the real hall sensor on my machine, it reads ok up to around 200RPM and then it gives 37 constantly until I lower the RPM again... Go figure...
I guess that'd be a sensor problem or else but did anyone ever encountered this kind of problem? What's my best guess? Taking out the scope and trying to visualize the signal from the hall sensor?
I think it will be easier to diagnose the problem if you look at the interval between pulses (in usecs) rather than looking at the RPM figure.
It sounds like you may be getting noise or "bounces" .
Why not define all the long values as unsigned long - they should never have negative values and using long will cause problems if the count rolls over.
If it was my project I would reduce my ISR to
void N1()
{
RPMN1count=micros();
flag=1;
}
and do the subtraction in the main code
and rather than do a calculation while the interrupts are off, I would just take a copy of the value in RPM1count.
I'm not suggesting either of these has a bearing on your 37 problem.
Can you provide a data sheet for the Hall sensor that you are using and how it connected. There are different types of these sensors, and the pull up or pull down required varies.
Your scope will tell you if you are getting a clean signal from the sensor or not, but you already know that with good signals from tone your code works well.
How often do you really need to update a tachometer? I suppose that if you're using PWM to adjust the speed of the motor, but even then, it seems excessive to update the tachometer count every revolution.
how about instead...
unsigned long pulseCount=0;
const unsigned long updateInterval=250000; // 4 updates per second
int RPM=0;
void N1() {
pulseCount++
}
void loop() {
if (startTime == 0) {
startTime=micros();
}
if (millis() - startTime > updateInterval) {
RPM=((pulseCount/2)/((millis() - startTime)/60000000));
startTime=0;
}
//display or otherwise use RPM value
}
Here is the Hall sensor I'm using (chinese crap but cheap!
It is wired to a +12V and the NPN sink (am I right?) wire is wired to the Arduino pin. I'm just using the Internal Pullup, there is no external pullup (maybe I should use one?).
I've Googled pretty hard on your sensor, and can not find a proper data sheet. What I did find indicates that the maximum frequency of response is 150hz and 4mm sensing distance. I'm still not certain of the connections and pullup/down requirements. What are you trying to sense in your application, the mechanical configuration, and the rpm requirements. This sensor may not be appropriate.
Amico LJ12A3-4-Z/AX DC 3 Wire Inductive Type Proximity Sensor
Description
Metal shell, DC type, 4mm detecting distance, 3 wire system.
Power supply inverted connecting protection, short circuit protection, non-contact detecting device.When proximity switch is close to some target object, it will send out control signal.
Used widely in the machine tool industry, frequency counters and other automatic control system.
Wire Type: Cylindrical DC 3 Wire Type (Brown, Black, Blue);
It does seem like the exact same one that I'm using.
I'm sensing two small magnets at about 2-3mm distance.
Thing is : it seems to be working perfectly when I turn the propeller by hand. There is a small LED on the back that lights up when it passes the magnet so it does work perfectly!
Maybe I'll try to do the interrupts on the RISING and not the falling edge...
I'd be very concerned about the 150hz response frequency. It's a low cost inductive proximity sensor.
Since you have magnets attached in your application you should take a look at the Hall effect magnetic field sensors. The magnetic polarity is important.. If you require the screw probe type mount, take a look at Hall effect gear tooth sensors.