Reed Switch Interrupt Triggers too often

Hey,

I want to make an ISR with a reed switch. So it should trigger, when the reed switch button pin is rising. It is for angular velocity, so in my original code, the reed switch would be triggered about every second, when the magnet passes the reed switch. The problem is, that the ISR usually triggers 4-5 times not just once.

#define reedPin 24     // reed switch
volatile uint8_t angvelFlag = 0;
int p;

void setup() {
  Serial.begin(115200);
   pinMode(reedPin, INPUT);
   attachInterrupt(reedPin, reedtrigger, FALLING);  // reed switch
  }



void loop(){
  static int t1;

    if (angvelFlag) {   
      Serial.println(p);
      angvelFlag = 0;
      }
  }

  void reedtrigger(){
  angvelFlag = 1; 
  p++;
  }

This is just my code, where I am just playing around with the reed switch. When I pass the reed switch I usually get 1,2,3,4 or even more. So I tried this:

#define reedPin 24     // reed switch
volatile uint8_t angvelFlag = 0;
int p;

void setup() {
  Serial.begin(115200);
   pinMode(reedPin, INPUT);
   attachInterrupt(reedPin, reedtrigger, FALLING);  // reed switch
  }



void loop(){
  static int t1;

    if (angvelFlag) {      
     detachInterrupt(reedPin);
      Serial.println(p);
      t1 = millis();
      angvelFlag = 0;
      }

    if (millis()-t1 > 2000){
      attachInterrupt(reedPin, reedtrigger, FALLING);
      
      }
  }

  void reedtrigger(){
  angvelFlag = 1; 
  p++;
  }

In that case I would get 1, but without triggering the reed switch again, the number 2 would show up in the serial monitor after the two seconds. I also tried to put the detachInterrupt into the beginning of the Interrupt routine, so that is should be impossible to run the interrupt again. but it will show the same results as before.

Does anyone have an idea what I am doing wrong?

Reeds are notoriously bouncy

make an ISR with a reed switch

Why?

Edit
if (millis()-t1 > 2000){
try
if (millis()-t1 > 100){

larryd:
Why?

because I also read torque from a crank and do a lot of calculations in the normal code, and since I need the exact time of when the magnet passes the reed switch to calculate the angular velocity I think an interrupt is the best way. In the original code, the reed switch saves the time and triggers a flag.

if (millis()-t1 > 2000){
try
if (millis()-t1 > 100)
or
if (millis()-t1 > 50){

larryd:
Why?

Edit
if (millis()-t1 > 2000){
try
if (millis()-t1 > 100){

I don't get why this would help, but I changed now 2000 to 100 and now it usually shows me 1, 100ms later 2 and 100 milliseconds later 3. Without triggering the reed switch again.

I also tried to put an if statement into the ISR, so that the flag just gets triggered when 100ms are over. But I don't think that this is a good solution for the problem

Not sure how things are wired.
Assume switch is connected between GND and input pin.
You need a pull-up.

pinMode(reedPin, INPUT);
try
pinMode(reedPin, INPUT_PULLUP);

larryd:
Not sure how things are wired.
Assume switch is connected between GND and input pin.
You need a pull-up.

pinMode(reedPin, INPUT);
try
pinMode(reedPin, INPUT_PULLUP);

It is wired between 3.3 volt and the input pin with a pulldown resistor going to ground

"reedPin 24"

What Arduino has pin 24 as an interrupt pin?

It is wired between 3.3 volt and the input pin with a pulldown resistor going to ground

Is the switch 'normally open' 'normally closed' ?

Arduino Due, all digital pins.

It is normally open, but I also tried FALLING but that didn't improve the issue

Assume the pull down resistor is 10K

Edit

Let’s see a good image of the wiring.

larryd:
Assume the pull down resistor is 10K

Edit

Let’s see a good image of the wiring.

I changed it now to what you recommended with the pullup. Meaning the reed swich is bettween PIN 24 and ground.. and also I changed the code to PULLUP. but still, same results

Do you still want to have a picture? It is just ground to pin24 that's it...

Good idea to see one.

Edit
I don’t have a Due to prove your results.

Ok the picture should be attached now

Don’t see a pull down resistor or the switch. :confused:

Not able to tell where the red wire is going.

Do you have the N and S poles of the magnet parallel to the reed switch? It will greatly reduce bounce.

Nope, it is not parallel. I just found some things to clearing the interrupt register, but could not find out how this exactly works. Does anybody know that? If I would detach the interrupt directly in the beginning of the ISR and also clear the register it would not store the data and fire again later.

Debounce in an interrupt context is usually done with a lock out period in the isr. Do not take action on the interrupt if it occurs close in time to the last interrupt. Yes, the unwanted interrupts will trigger the isr, but they will be ignored and not change the count or flag setting.

.

void reed_trigger()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  if (interrupt_time - last_interrupt_time > 200)  //pick some appropriate debounce lock out perioid
  {
    angvelFlag = 1;
    p++;
  }
  last_interrupt_time = interrupt_time;
 
}