Interrupt code that works on Uno doesn't work on Mega

Hi

I have a fairly simple project that does work on my Uno board but when switching to my Mega 2560 the interrupt does not work. What I am trying to do is to take a trigger signal from my external power supply and count how many times that signal is triggered. The trigger signal is a 10us positive pulse, so the Uno could detect the rising edge.

With the Mega board the interrupt will not happen. I have tried to change pin from 2 to 21 to 53 but no difference. Not sure what I am doing wrong.

I used this code:

volatile unsigned long count=0;
unsigned long prior_count=0;


void setup() {
  Serial.begin(9600);

  pinMode(2, INPUT);
  attachInterrupt(digitalPinToInterrupt(2), pulse, RISING);

  Serial.println("Test");
  Serial.println(count);
}

void loop() {
    unsigned long new_count = count;
  if (new_count != prior_count) {
    Serial.println(new_count);
    prior_count = new_count;
   }
}

void pulse() {
  count = count + 1;

}

Thanks for all suggestions.
BR
KaFP

pinMode(2, INPUT);
Hopefully, you have a pull down resistor.

Hi I did try a pull down resistor but it didn't help. Also the PSU does keep the signal low until it triggers.

Just to mention, I have measured the trigger signal with an oscilloscope and cannot see anything wrong.

Pin 2 appears to be a good choice for a mega: attachInterrupt() - Arduino Reference.

What voltage did you see on the oscilloscope during the positive pulse ?

The trigger pulse was 5V.

You should actually do this on an 8 bit MCU (non-atomic 32bit read operation):

noInterrupts() ;
unsigned long new_count = count;
interrupts() ;

but I doubt if that is your problem.

I'd connect a 10k pull down resistor to pin 2 and touch a jumper lead between +5v and pin 2 to see if that generates a message with the value new_count.

1 Like

Thanks I will try it out.

OK that worked perfectly (10k pulldown and trigger with wire to 5V). However connecting to the trigger signal on PSU, it does not work. Is the trigger signal too fast to detect? 10us is that on the limit?

No.
I think we need a schematic.

Only pin 2 and 3 are safe for external interrupts. Pin 18 to 20 also are used for Wire and Serial1. If you want to change your interrupt pin then use a #define or const int declaration for the selected pin so that the pin is fully initialized in setup().

OK that worked perfectly (10k pulldown and trigger with wire to 5V). However connecting to the trigger signal on PSU, it does not work. Is the trigger signal too fast to detect? 10us

How frequently to these 10us pulses arrive?

The pulses comes once every 15 seconds.

The only thing connected to the arduino is the pulse signal 5V and Gnd. The 5V is connected to pin 2 and Gnd to Gnd. Pin 2 has a pull-down resistor,10k to Gnd.
That is all.

What if you use a pull-up instead and watch the pin with/out the external device attached?

I guess that your pin 2 is defective.

Please see post #9

Hi

This is the schematic of the test setup.

Yes, there is not much to it. I can only guess that the shape/amplitude of the input pulse is not good enough. You could try something like this (R2 should be 10k), You'd then look for a falling edge. I suppose you could also look at using the Mega's built-in comparator to trigger an interrupt.

Thanks that is probably the way to do it.

At least I now know that the code works for the Mega and the Uno, the only difference is probably the input on the MCU chip itself.

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