Double counts with magnetic hall effect sensor--desperate for help.

Status
Not open for further replies.

KrisKasprzak

Well-known member
I have one of these hall effect sensors connected to my Teensy 3.2 with a very short wire and the sensor works fine, by passing a magnet over the sensor i get a single count on my interrupt pin (I'm using pin 2) , when I remove the magnet no additional pulse. Code below. All is fine.

Now I have added a 2 foot shielded cable between the sensor and my Teensy, and I'm getting very wrong results. The cable is actually a short USB cable, and my sensor and Teensy are soldered to some PCB's all connections are good

When I pass the magnet over the sensor, the sensors light goes out and I get a pulse (Count =1), but when I remove the magnet I get another pulse, Count now = 2).

I have a feeling there is some capacitance issue with the wire--but I'm real stuck.

https://www.amazon.com/Wrisky-Ardui...6&sr=8-39&keywords=hall+effect+sensor+arduino

Anyone have an idea?

Code:
volatile unsigned long Count; 


void setup() {

  Serial.begin(115200);
  

  pinMode(2, INPUT_PULLUP);
 
  attachInterrupt(2, inc_rpm, FALLING);

 
}

void loop() {

  

}


void inc_rpm() {


    Click++;
    Serial.println(Click);



}
 
Correction. Passing the magnet near the sensor does not cause a Count, but removing the sensor causes 2 Counts.
 
Do you have an oscilloscope? That would let you see the issue. My guess is you are getting a bounce in the signal due to capacitance. I would add a 100pf capacitor from the signal to ground and see if that fixes the issue. If not, try a large one until it does. I had a similar issue with an optical sensor and a capacitor fixed it. Good luck.
 
Your code isn't doing anything to supress bounce. You'd either need to go for a hardware solution (at the most basic the mentioned cap), or add something to the interrupt checking if there has already been a count in the last x microseconds. The most basic way of doing that is actually using the micros() command, but you do need to do something about roll over.
 
tuttle9er and GremlinWrangler,

Thanks for the feedback. I do have a cheap scope and I can see the signal but I didn't know how to fix it. Around an hour after I made this post, i tried the same thing, only i put a 0.1uF across the signal pin and Vcc--it seemed to do the trick. I'm guessing at a certain level the cap will start to affect the pulse rate. My rates are generally 1-15 pulses per second so i should be good. I'll keep testing and maybe try the cap to ground as well, but for now I feel it's working.

I've also looked at putting a software bounce checker in the ISR but I figured I ask first--actually my code doesn't count pulses it measures time between pulses using millis(), that way i get much much better accuracy--but the bouncing issue was apparent with any measurement scheme. My implementation is measuring wheel RPM and with the time measurement, I get very good accuracy--and now with the cap, it's reliable. I'll look at a software debouncer.

Thanks again, if you are ever in northern Alabama, let me know I'll buy you a beer!

Kris
 
Here is code I used for an optical encoder, running as fast as 3 ms per tooth. The cap should not effect your rate, plus more of an issue if reading the rising edge, which the cap with slow the rise time. I feel you need to see more of the code let me know and will send you a pm.

Code:
//Trigger on Falling, or leading edge of tooth
void counterDeg()// Degree Time
{
  DegreeTimeShared = micros() - DegreeStart;
  if (DegreeTimeShared >= 2000) { //to prevent false firing, 3000 = 278rpm 2000=417
    DegreeStart = micros();
    DT = DegreeTimeShared; //fixed incorrect DegreeTime when no capacitor in place
    PositionDegShared += 5;
    bUpdateFlagsShared |= DEGREE_FLAG;
  }
}
 
turtle9er

thanks, i'll keep the code in mind, so far it looks like the cap did the trick. I have the cap across vcc and signal pin, I may try gnd and signal or even both.
 
Status
Not open for further replies.
Back
Top