Go Down

Topic: Leonardo interrupt question (Read 144 times) previous topic - next topic


Jan 31, 2016, 03:48 pm Last Edit: Jan 31, 2016, 03:54 pm by wimpie3
Getting frustrated here, some help is needed.

I want to detect a pin change using an interrupt. The Leonardo seems to have five pins that can detect changes: 0, 1, 2, 3 and 7.

Now here is the problem. I cannot use 0 & 1 because I need these ports for Serial communication.

Ports 2 & 3 are working, AS LONG as I'm not using SDA & SCL. As soon as I do that, so WHILE I'm sending data over SDA & SDL, the interrupt gets fired (even if there is no pin change).

I've read somewhere that you cannot use ports 2 & 3 together with SDA & SCL. This leaves only one pin for my interrupt: 7. However,
Code: [Select]
attachInterrupt(digitalPinToInterrupt(7), myInterruptHandler, FALLING);is not working at all. The interrupt is never fired.

Google told me pin 7 is not available for attachInterrupt and that I should use an ISR routine, which fires an interrupt if there is a pin change on a BLOCK of pins. Can someone tell me if this is correct?


The digitalPintToInterrupt() function determine which external interrupt pin to attach the handler to. Pin 7 is not an external interrupt pin.

There is a library for pin change interrupts. You should be using that library, which provides a convenient method to determine which pin changed.
The art of getting good answers lies in asking good questions.


Would doing this work?
Code: [Select]
  EICRB |= (0<<ISC60)|(1<<ISC61); // interrupt type = falling
  EIMSK |= (1<<INT6); // activates the interrupt
ISR(INT6_vect) {
  // interrupt routine


Pin 7 is an external interrupt pin on the Leonardo, no need for pinchange interrupts.

External Interrupts: 3 (interrupt 0), 2 (interrupt 1), 0 (interrupt 2), 1 (interrupt 3) and 7 (interrupt 4).

So your first try should basically work.
Code: [Select]
attachInterrupt(digitalPinToInterrupt(7), myInterruptHandler, FALLING);
The above would make a good setting for closing to GND and INPUT_PULLUP.

Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)


Jan 31, 2016, 04:51 pm Last Edit: Jan 31, 2016, 04:56 pm by wimpie3
That's exactly what I thought, but it only works on pins 2 and 3, not on 7.


I have a NRF24L01 driving the external interrupt pin 7 of a Leonardo working right before my eyes.  ;)
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)


I guess I'll have to disable all my libraries one by one to see if any of them blocks the interrupt on D7...


I would check the interrupt first.

Can you read the pin? Is the value as expected (while operating the device on pin 7)?

I bet the interrupt fires on CHANGE if there is a change.

No connection / broken wires / faulty wiring ... double-, triple-check and verify!

Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131