Mega 2560 ISR bug?

Hello everyone,

I have configured two external interrupts on my Mega2560, specifically on pins 2 and 3, both set to trigger on a falling edge. Pin 3 has an external pull-up resistor with a voltage of 5V and should maintain a high output level constantly. Pin 2 is continuously outputting high and low levels at a frequency of 10Hz through another microcontroller. For some unknown reason, the interrupt on pin 3 is triggered roughly once every 1 to 10 hours. I have clearly pulled it up to 5V with a pull-up resistor, so why is the interrupt still being triggered?

Perhaps you may ask why I don't simply disable the interrupt on pin 3 since my program only uses the interrupt on pin 2. In response to this, I want to clarify that I have simplified the issue encountered in my actual project into this format. Initially, I suspected it could be due to other problems, but even in this simplified form, the issue persists. In my actual project, pin 3 is also being triggered inexplicably.

I have already tried replacing the Mega2560 development board and the wires, but the issue remains. I hope you can provide some advice. Thank you very much for your help. I
will post my code for everyone to review.

Here is my wiring diagram, which includes a common ground connection. MCU2 sends high and low level signals at a frequency of 10HZ. The blue one on the left is MCU2. MCU1 is the green one on the right. When a falling edge occurs, it triggers an external interrupt on pin 2 every 100ms. Pin 3 should not be triggered, as I have configured it with an internal pullup resistor.
Wiring:MCU1-GND—MCU2-GND MCU1-PIN 2—MCU2-PIN 2


The later update
2024-12-2

  1. I tried replacing the ESP32 to test this scenario.
  2. I changed the Arduino version from 2.3 ->1.8.
  3. I used twisted pair cables, shortened and replaced the wires.
  4. I switched from external pullup to internal pullup.
  5. I stopped using Serial.print inside interrupts.
    After trying these five methods, the issue still persists.

The latest update
2024-12-3
The problem has been solved. It was an issue with the USB port power supply. Thank you all very much for your help.

are all the GND joined ?
are you using Dupont wires or everything is soldered?

//MCU1Receive.ino
//MCU1 RECEIVE
//Please note that these are two different files.
//Receive Receive Receive
//-----------------------------------------
unsigned long loopIng = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("begin");
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), stop1, FALLING);
  attachInterrupt(digitalPinToInterrupt(3), stop2, FALLING);
}
void loop() {
  loopIng++;
  if (loopIng >= 32000000) {

    Serial.println("Running");  //Run?
    loopIng = 0;
  }
  if (errorIsr == 1) {
  Serial.println("error");
    errorIsr = 0;
  }
}
void stop1() {
 //Serial.println("ok");
}
void stop2() {
   errorIsr = 1;
}  
 //MCU2Send.ino
 //MCU2Send 10HZ LOW
//Please note that these are two different files.
//Send Send Send
//-----------------------------------------
void setup() {
  Serial.begin(9600);
  Serial.println("begin");
  pinMode(2, OUTPUT);
}
void loop() {
  //10HZ 
  digitalWrite(2, HIGH);
  delay(50);
  digitalWrite(2, LOW);
  delay(50);
}

Thank you for your reply. I have handled the common ground connection, and the wires I used are Dupont wires. However, I have already ruled out the possibility of issues with the wires.

are you going through a breadboard ? cross talks?
how stable is the 5V for the pullup ? how large is the resistance ?

(please use code tags for code)

What happens if you use the internal pull-up resistor instead of (or in combination with) the external one?

What is the value of the external pull-up resistor?

Serial.print inside an Interruptroutine is No, No. It brings trouble,.

Sounds like a wiring problem, with cross talk between inputs. Post a photo of the setup.

Do not attempt to do serial I/O in an interrupt routine.

In general yes, that’s not a good practice.

But, for this demo code, it won’t create the cross talk and print is supported in an ISR nowadays.

1 Like

Yes, there is a workaround for some MCUs, but it will fail on others. It is a BAD IDEA to attempt serial I/O in an interrupt.

2 Likes

I apologize for only being able to reply to your message now. Yes, I am using a breadboard. Regarding crosstalk issues, I'm not sure, but I think this circuit is very simple and should be stable. I am using a 9.1k resistor. I will upload my circuit diagram later. Thank you very much.

thank you for your reply. I haven't tried using the internal pull-up resistor yet, because my project requires the use of an external pull-up. However, I will try using the internal pull-up to test it and let you know the result. Thank you very much.

Thank you for your reply. Oh, is that so? Sorry, I'm still new to Arduino. During my learning process, I found quite a few people using Serial.print in interrupts for easier debugging. I will try it later and let you know the result. Thank you.

Thank you for your reply! I will try to avoid performing serial input/output operations within interrupts, and I'll upload a photo of my setup. However, I've seen many demos that support printing within ISRs, which is a bit confusing to me.

Of course you have encountered some very poor coding examples on the web.

As @J-M-L and I pointed out, with some Arduinos, a workaround was put in place so that beginners, who have little to no idea how these things work, could get away with such bad practice.

However, the workaround supports Serial.print only, and not I2C or other serial protocols.

I really appreciate your reply. I will try my best to avoid these problems and handle them in a more standardized way.

I really appreciate your reply. I will try my best to avoid these problems and handle them in a more standardized way.

There are lots of non-obvious rules and gotchas (which can lead to serious errors) to do with implementing interrupts. And, there aren't many occasions where the return is actually worth the effort required to implement them.

For many projects, monitoring an input pin by polling is much simpler, more reliable and sometimes even faster than using interrupts.

I would try with shorter wires / no bteadboard. (Just solder a small piece of wire to the resistor and connect it to 5V on the same board.

Great approach. I've also tried using a perfboard for soldering instead of a breadboard, but the error still occurred. Today, it happened again after nine hours of work. It's really frustrating, and I suspect it might be an issue with the board. I even tried switching to an Uno, but the same problem persists...