I recently purchased a "Arduino Magnetic switch Hall-speed counting detector Sensor module" from Amazon for what is essentially a speedometer project. When I connect the sensor into a digital pin and read/display the state I get what seems to be the appropriate string of 1s and 0s based on the presence or absence of a magnet. However, when I use either interrupt pin on my Arduino Uno R3 with an interrupt function that does nothing but increment a counter and create a time stamp, I get a counting pattern that doesn't correspond exactly to the number of magnet passes.
When I use FALLING it seems like the interrupt function triggers twice at the exact same time. With RISING or CHANGE I get three responses. Is this a programming issue or a hardware issue?
More information about the specific hall sensor used would be useful here; some sense just N or S and some either. If the hall sensor has any numbers on it you should try googling them up.
When the magnet is passing under the sensor it sees the polarity of the face of the magnet but can also see the magnetic flux from the other pole of the magnet when the sensor is near the sides of the magnet. You might be able to counter this simply by moving the sensor further away.
You get the same counts for RISING and CHANGE? CHANGE should give you the sum of RISING and FALLING. I guess you have to apply a low pass filter to make some "debouncing". Do you have the chance to analyse the signal on an oscilloscope?
Yes, RISING and CHANGE both give three counts per magnet pass. One count occurs when the magnet is placed in front of the sensor and the other two occur when the magnet is removed. To me it seems like this is almost correct for CHANGE except that the FALLING count occurs twice per pass. But I guess I don't have the experience to understand why this same behavior occurs for RISING.
I don't have easy access to an oscilloscope. How important would it be?
An oscilloscope gives you a picture how the signal curve looks. The interrupt of the ATmega is reacting on changes of that curve, on edges. There must be a state change (LOW->HIGH or HIGH->LOW) for the CHANGE mode to trigger. RISING triggers only on LOW->HIGH, FALLING triggers only on HIGH->LOW.
From the scope output you can tell, what is triggering the interrupts and how you may be able to filter some of the events. Without that pictures it's harder to find the correct values but it's not impossible.
Your post sounds like these numbers (3 counts for RISING or CHANGE, 2 for FALLING) are reproducable. Is this the case?
Please post your code, maybe we find some problem in there.
The LM393 has an open-collector output. Does the module need an external pullup resistor between output and +5v, or is there one on the module already? If it needs an external pullup, what value are you using?
Have you tried adjusting the helical pot on the sensor board?
Please post your schematic and code.
btw for a speedo you don't normally need a module as complicated as that one, just a US1881 Hall sensor and 2 magnets. Also a 1K pullup resistor if the wires to the Arduino are long.
The error in the count was very reproducible, it happened 99.9% of the time. However, I was able to "fix" the problem buy adding code to the ISR that prevented the counter from advancing more than one time every 25 milliseconds. This works in my case because I am dealing with periods greater than 100 milliseconds.
I know this is an ancient post, but I had this problem too with an LM393 on an infrared sensor that pulsed 3, 4, 5 or even more times on each single activation - Maybe this helps others in the future.
Turns out that although the whole sensor (and/or LM393?) is rated 3.3V-5V, it sends out messy pulses at 5V. I connected it now to the 3,3V output of the Arduino and now the pulses are perfect! Only one at at time...
When using a LM393 or other comparator to convert an analog input signal to a digital one, you almost always need to add hysteresis (positive feedback) to get clean digital transitions on the output. Digital Hall sensors don't need to be used with a comparator, and they have hysteresis built-in.