Interrupts pins issue


I'am a newbie on electronic, I just bought an arduino MEGA 2560 and i'am trying to play with interrupts.

I just to try to log something on the serial link when a falling edge is detected onto the selected interrupt pin.
Here is the code:

void setup()
attachInterrupt(5, onoff, FALLING);

void onoff()
Serial.println("falling edge");

I plug the interrupts pin 5 (pin 18 in my board) to a circuit this a pull up resistor and a push button
=> Value of the digital must be 1 when button is unpressed and must be 0 when button is pressed.

For some interrupts pin (0, 1 and 3) all works fine (my code is updated according to the selected pin), but for unknown reason it does not work for others pins => the "falling edge" log is printed continuously on the serial link, if I keep the button pressed the log stopped and restart infinitely when the button is unpressed

I tried to run this sample without any wires plugged in the board, for interrups pin 0,1,3 nothing is never log => OK
but for pin 2 and 5 same error :frowning:

Is this a board issue ? is this normal ?

Many thanks for your help and sorry for my bad english :slight_smile:

Did you enable the pullups properly? Otherwise the pins will be picking up random noise.

Yes i am because with if use pin 0 instead of 5 all work fine
Moreover even if nothing is plugged on the board I have the same behavior :frowning:

Post your whole sketch.

const int motorpin = 13; 
int speed = 150;
boolean run = true;

void setup()
  pinMode(motorpin, OUTPUT); 
  attachInterrupt(5, onoff, FALLING);

void onoff()
  Serial.println("Falling edge");
  run = !run;
  analogWrite(motorpin, 0);
  speed = 150;

void loop()
    analogWrite(motorpin, speed);
    speed += 10;
    if (speed > 250) speed = 0;

Note that the motor is supplied by an external battery. (Grounds of the external battery and board are wired)

Yes i am because with if use pin 0 instead of 5 all work fine

Please explain which line of code illustrates this.

With interrupt 0 each time I pressed the button the "Falling edge" log is printed on the console whereas with interrupt 5 the log is printed in loop

With interrupt 0 each time I pressed the button the "Falling edge" log is printed on the console whereas with interrupt 5 the log is printed in loop

This reply does not answer the question. The question that Udo asked was whether you have enabled pullup resistors, or wired external resistors, so that you KNOW whether the interrupt pin is getting a definite +V or 0.

You said that, yes, you were enabling the pullup resistors, and showed your code to prove it. Well, as I look at your code, it proves that you have NOT enabled the pullup resistors on any pins.

Now, it would be helpful when you refer to pin numbers if you clearly distinguished between the number assigned to the pin on the board layout and the interrupt number that the pin may correspond.

When you say pin 0, I think of the pin that is part of the serial port instance. That pin 0 has extra resistors associated with it, and no user-accessible interrupt function.

So, what do you mean by pin 0?

sorry to be not clear.

By 0 i mean interrupt 0.

So I re-explain my issue.
If I use the pin 18 for my interruption the toy motor will never spin and the log print in my "interrupt handler" is printed in loop.
If i use an another pin for interruption all works fine...

PaulS also pointed at the pullups. Why do you believe you have proper pullups? Most probably you don't

The fact that one of your interrupts is working to your satisfaction and one not, does not mean you do not have a problem with your wiring setup. The fact is that the hardware side of things must be correct before the software functions can work reliably and consistantly.

So how are you wiring your push button to your pin(s)? A drawing of some form would be the most help here Vs a written explanation of your wiring. If you do indeed lack pull-ups there are easy solutions at hand. Just slow down and try and answer the questions that have been given to you, there is a reason for these questions and will most likely help lead to success.



Here is a drawing of my wiring.

Note that the "power" of the capacitor is 220uF

Diode wired across motor terminals is wired backwards, turn it around.

Exactly, It was a mistake in my drawing (not in my wiring)

(I have updated the schema)

I'm totally confused. I see no way of controlling the motor, isn't in permenantly on?

Pin 18 is not an interrupt on the Mega.


Pin 18 is not an interrupt on the Mega.

Actually, it is:

External Interrupts: 2 (interrupt 0), 3 (interrupt 1), 18 (interrupt 5), 19 (interrupt 4), 20 (interrupt 3), and 21 (interrupt 2). These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.

Yep sorry, I misread the schematics.


The schematic does not show motorpin (13), so I guess that the schematic is not complete?

If nothing is plugged into the board, the input pin(18) is floating, unless you enable the internal pull up, which the code does not.

So being interrupted continuously by falling edge seems reasonable in that case.

try adding

digitalWrite(18, HIGH) ; to the setup() function and see if the behaviour is better with the board is disconnected from the circuit.

Yes the schematic was false, I have updated it, this time I am sure all is correct :slight_smile:

Thanks for your advice, I didn't know that the input is floating if nothing is plugged !!

has anything changed now?

the schema makes sense I think

  • although having to scroll back to find it is odd
  • and having it change after it was posted tends to make reading the following comments difficult, as the refer to earlier versions of the schema.