Mega : Interrupt on Pin 3

Hi guys,

I'm using a Mega 2560 and on pin 2 I have an encoder which works great with the interrupt.

But on pin 3, the interrupt is always firing. I'm missing something? Aren't interrupt supposed to fire only once until the next trigger, when there's a change in value? If not, what am I missing?

Is there a difference between pin 2 and 3?

the sensor is an O8P204 going throught this relay. the relay is connected to PIN #3 and is Normally Closed.

I know that it's electrically working because I hear the relay when I put something through the IR beam.

int FOAMSENSOR_PIN = 3;

// the setup routine runs once when you press reset:
void setup() {

  Serial.begin(9600);
  pinMode(FOAMSENSOR_PIN, INPUT);
  
  attachInterrupt(digitalPinToInterrupt(FOAMSENSOR_PIN), foamInterrupt, RISING);
}

// Something is wrong. It's always firing when I block the signal with my hand.
void foamInterrupt() {
  Serial.println("Foam sensor interrupt");
}

void loop() {
}

what is the sensor on pin 3 and how is it connected
have you an oscilloscope you could have a look at the signal on pin 3?

No.

Does the encoder work on pin 3? Does the foam sensor work on pin 2?

When asking questions about hardware it is necessary to provide the data sheet(s) for the hardware and a schematic showing how the hardware is connected and powered.

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

Relay is not a good source for an interrupt signal because of contact bounce, you might get a rapid string of interrupts with each contact closure. You could use either a voltage divider or an NPN transistor level converter to drop the 24V output down to 4V for Arduino input. An opto coupler is another (simpler?) option.
A sensor with NPN, open collector output (not PNP) would have been the better choice, no level shifting required.

Since, I’m mainly the software guy and have no real control on the development, I’ll have to find a workaround.

The high signal is stable. It’s when the signal is low that everything goes crazy. When the signal is unstable, I’ll take in account that there’s something blocking the beams. I hate this kind of solution…

Thank guys

Show my reply to the HW guys, put the problem in their playground. :wink:

Do not attempt to do any sort of serial I/O in an interrupt routine. This will hang up or crash most processors.

1 Like

Done! They've managed to pull a pull-up resistor on the pin.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.