ArduinoCore-mbed icon indicating copy to clipboard operation
ArduinoCore-mbed copied to clipboard

Arduino Giga attachInterrupt() resets pinMode Pullup/Pulldown

Open N9W9 opened this issue 2 years ago • 2 comments

When using the attachInterrupt() function set Pullups/Pulldowns are reset, the Pin is left floating.

When setting the pinMode after attaching the interrupt, it does work fine. However this is inconsistent with most examples which set the pinMode before attaching the Interrupt (e.g. https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/; also on the Arduino Giga Cheat Sheet) It is also inconsistent with Arduino Mega, where the pinMode does not get reset after the attachInterrupt() call.

Example Code:

volatile int count = 0;
const byte pin = 2;

void setup() {
  Serial.begin(9600);
  //pinMode(pin, INPUT_PULLDOWN);    // Setting the Pin Mode here does not work!
  attachInterrupt(digitalPinToInterrupt(pin), ISR_count, RISING);
  pinMode(pin, INPUT_PULLDOWN);       // Pin Mode has to be set after the attachInterrupt()
  delay(100);
  Serial.println("Start");
}

void loop() {
  Serial.println(String("Pin State: ") + digitalRead(pin) + String(" Interrupt Count: ") + count);
}

void ISR_count() {
  count++;
}

N9W9 avatar Nov 28 '23 08:11 N9W9

Hit the same issue, as above moving pinmode after attachinterrupt fixes this pullup issue, but means the activation of the pull-up (or down) can cause an interrupt.

rchesterr avatar Feb 19 '24 23:02 rchesterr

This is being tackled by this PR https://github.com/arduino/ArduinoCore-mbed/pull/256 , but was never merged due to the inefficiency of the solution. Any idea is appreciated :slightly_smiling_face:

facchinm avatar Feb 21 '24 08:02 facchinm