0

I've been trying to capture rising edge of an IR sensor output. This sensor's Vcc connected to Arduino 5V and it has a digital output pin. I've observed the digital output pin on an Oscilloscope. It's not noisy.

When I attach a rising edge interrupt on digital pin 2. It somehow captures both rising and falling edges. This response is the same for when it's set for FALLING or CHANGE.

I've tried code at below both on Arduino Uno and

volatile bool caughtAnInterrupt = false;

void setup() {

  Serial.begin (19200);

  pinMode(2, INPUT);

  attachInterrupt(digitalPinToInterrupt(2), test, RISING);
}

void loop() {

  if (caughtAnInterrupt){

    Serial.println("Rising edge detected");

    caughtAnInterrupt = false;
  }
}

void test() {

  caughtAnInterrupt = true;
}
  • How much time between rising and falling edges, and what are you detecting, a square edge? – MichaelT Nov 15 '18 at 8:01
  • @MichaelT IR sensor outputs digital high when it has a object in front of it. So, it might be any time between rising and falling edges. As I specified in the answer, I found the solution. Probably I didn't observed signal properly on Oscilloscope and the signal had debouncing problem. – Şener Nov 15 '18 at 8:06
0

I've solved the issue with the reply by eventHandler to another post. Here it is;

The problem you are having is because the button glitches are producing many interrupts on each button press. You can find a good description and a way of solving it using hardware here.

Let me explain, when you press the button, the mechanical contact will have a transcient state in which it will fluctuate ON-OFF for a short period of time. The same effect might happen when you release the button.

One way of solving this problem is using a capacitor parallel to the load. Another "easier" way would be done by software. The idea is to set a fixed arbitrary time in which you don't allow new interrupts. You could use the millis() or micros() library to set this time. The code would look something like this.

unsigned long lastInterrupt;

void ngt()
{

  if(millis() - lastInterrupt > 10) // we set a 10ms no-interrupts window
    {    

    state = !state;

    lastInterrupt = millis();

    }
}

This way you don't process new interrupts until 10ms have elapsed.

Note: adjust the time to your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.