Hall Effect Sensor Reports Multiple "FALLING" per magnet passed

Hi, I am using a hall effect sensor and a disk with four magnets on it to measure the RPM of a spinning engine shaft.

When I ran my code, I noticed that the RPM values reported were way off (engine was spinning at ~2000 RPM, arduino would tell me ~3300 RPM). To troubleshoot, I wrote very simple code that would fire an interrupt every time the sensor was "falling". The interrupt would then print on the serial monitor "FALLING".

However, when I spun the shaft by hand, I noticed that the interrupt was firing multiple times per magnet, when it should only fire once. It usually fires twice.

How do I fix this issue in which the signal seems to be "bouncing" and not accurate?

The sensor I am using is http://www.digikey.com/product-detail/en/SS451A/480-3587-ND/2505488?itemSeq=142873507&uq=635256434494687772 . The data sheet is on the website.

Thanks, here's the simple code that I wrote to test for falling.

void setup()
{
  pinMode(2, INPUT);
  digitalWrite(2, HIGH); //internal pull-up resistor
  
  attachInterrupt(0, test, FALLING);

  Serial.begin(9600);

}

void loop() {
  
}

void test() {
  Serial.println("FALLING");
}

Using a serial function inside an ISR (which disables all interrupts) may be problematic to your troubleshooting.

I noticed that the interrupt was firing multiple times per magnet, when it should only fire once. It usually fires twice.

That is due to the proximity of the magnet to the sensor. The sensor is too close, try separating them a bit.

And yes don’t print inside an ISR.

@G_M: explain the too close thing please, if you will?

I would also like to know why I should move the magnets further from the sensor. Shouldn't the magnets/sensor be as close as possible?

Here is the complete program that I wrote to calculate the RPM. There are 4 magnets on each disk (1 revolution = 4 magnets passed). I delay for 1 second to take readings via interrupt, then I detach the interrupt, calculate the RPM, print the RPM, then re-attach the interrupt and start the delay timer over again.

Is this a sensor problem or is this a code problem? It would be great if you guys could take a look at my code.

//RPM variables (all INT)
unsigned int rpm1 = 0; //the actual "RPM" value that gets displayed 
volatile unsigned int rpmcount1 = 0; //the counter used in the interrupt function

//time variables
volatile unsigned long lastmillis1 = 0UL;
volatile unsigned long now = 0UL;

void setup()
{
  pinMode(2, INPUT);
  digitalWrite(2, HIGH); //internal pull-up resistor
  
  attachInterrupt(0, rpm_Sensor1, FALLING);

  Serial.begin(9600);

}

void loop () {
  
    delay (1000); //the reading period
    
    detachInterrupt(0);
    
    rpm1 = rpmcount1 * 15;  //using 4 magnets on wheel
                            //reading period: 250, then *60
                            //reading period: 500, then *30
                            //reading period: 1000, then *15
                            //****Don't forget to change the value of delay()
                                          
   Serial.println(rpm1);
   
   rpmcount1 = 0; //restart the RPM counter
  
   attachInterrupt(0, rpm_Sensor1, FALLING);

}

void rpm_Sensor1()
{
    rpmcount1++;
}

I would also like to know why I should move the magnets further from the sensor. Shouldn't the magnets/sensor be as close as possible?

You have a problem. Last week I had the same problem.

I solved it by moving the sensor further way from the magnet so this is no theoretical solution. Seeing that the magnet and mechanism was over 40 ft off the ground I didn't do too much investigation. However, I can speculate that the magnet was too strong for the sensor and it dropped out at the closest approach and registered either side of the minimum distance position. If you look at the spec of a hall effect switch there is a maximum value for magnetic flux.

Ah ok Mike, thanks…

In a way similar to the graph below which is for an ultrasonic sensor, where in the one shown, closer than about 8cm it gives the same values as for further away?

sharp_ir_voltage_graph.png