Interruption always activated

Hi everyone !

I try to get the speed of q spinning disc with a light blocking sensor and an encoder.
I use an arduino nano and the sensor KY-010 connected on the pin D2 (interrupt 0).

Everything was working last week, but now, when I start the program, the interruption is always activated, even if i don't activate the sensor.

I use a different led with the sensor a bit modified but that's not the problem, it is working :
When I watch the signal from the sensor with the oscilloscope, everything is fine. 5V when nothing, 0V when the LED is in front of it. When the Signal pin of the sensor is connected to the arduino, there is a lot of noise but the direct component of the signal is high when no LED and LOW with LED

So, do you know why the counter is incremented all the time ?

Here is the code I use and you can find a picture attached.
Thanks :slight_smile:

int cpt = 0;
void setup() {

  • Serial.begin(9600);*
  • pinMode(2, INPUT);*
  • attachInterrupt(0,interrupt,RISING);*
  • Serial.println("Let's go !");*
    }
    void loop() {
  • Serial.println("____________________");*
  • Serial.println(cpt);*
    }
    void interrupt(){
  • cpt++;*
    }

Hello there!

It may help to disable interrupts while the ISR is running. the following code is how this can be achieved.

void interrupt()
{
  noInterrupts();
  cpt++;
  interrupts();
}

This just prevents more ISR calls while the ISR is currently running. Try it and let me know.

If we are talking about ATmega (UNO, Mega, Nano, etc.), interrupts are disabled by default while MCU is in the ISR and enabled after finish. There is internal flag for another one interrupt of each kind to prevent missing any while the ISR is running. Any extra interrupt is missed.

@Quentin91
As you are writing, if there is a noise on Arduino pin, it can produce false interrupts. Check your HW first and try to improve it somehow.
BTW: In the loop, should be better to test the cpt and print only if it was changed.

Hi, thanks for your replies

That's right, I will only print the new cpt, thanks

I really think that the hardware is okay. When I don't connect anything to the arduino, the signal is perfectly stable. +5V when nothing detected, 0V when the light is detected. But when I plug the signal pin on the Arduino (pin D2), the signal isn't stable anymore so I am worried that the Arduino has some issues.

Anyway, to check it, is there a way to check the value from the sensor by plugging it on an analog input and incrementing cpt only if the value from the sensor is lower than a certain value ?

Or have you got any idea about the cause of this problem ?last week it was okay I think.

Maybe I plugged the USB while supplying the Arduino from Vin by mistake and it damaged it ?

It's my first project with arduino so I am a bit lost when the error doesn't come from the code...

Try with the pinMode of D2 set for INPUT_PULLUP.

What happens if you move the interrupt to pin3? (interrupt 1)

Maybe I plugged the USB while supplying the Arduino from Vin by mistake and it damaged it ?

That should not be a problem.

When I don't connect anything to the arduino, the signal is perfectly stable. +5V when nothing detected, 0V when the light is detected.

How are you measuring this?

I use a different led with the sensor a bit modified but that's not the problem,

What did you change? Can you go back to test with the old led?

You cannot really think that the hardware is okay. No doubts? Can you post the schematics?

Okay, you were right. I tried with another sensor and it is working. The first one was dead. Thank you very much for your help :smiley: