Description
Board
Arduino Nano ESP32
Device Description
N/A
Hardware Configuration
N/A
Version
IDE Name
Arduino IDE, Arduino CLI
Operating System
N/A
Flash frequency
N/A
PSRAM enabled
yes
Upload speed
N/A
Description
The first parameter of the attachInterrupt
function is an interrupt number. The digitalPinToInterrupt
function can be used to derive the interrupt number from the Arduino pin number. So it is common to use the two functions together. For example:
attachInterrupt(digitalPinToInterrupt(interruptPin), interruptISR, CHANGE);
Arduino pin numbers are arbitrary integers used to reference an I/O pin in Arduino code. The Arduino pin number has traditionally been mapped to the low level pin identifiers (which won't necessarily have any direct correlation to the pin number) by the core variant. When this platform was created, that standard approach was abandoned and the low level GPIO numbers were used as Arduino pin numbers, forcing any mapping to be done via macros (e.g., D2
). When support for the Nano ESP32 board was added to the platform, it was necessary to implement support for true Arduino pin numbers, while also retaining the GPIO system. This was done in some cases by passing the pin number argument of functions through a digitalPinToGPIONumber
conversion function.
The digitalPinToInterrupt
function passes the pin number argument to digitalPinToGPIONumber
, which is appropriate:
arduino-esp32/cores/esp32/Arduino.h
Line 145 in 7018cd1
The attachInterrupt
function passes the interrupt number argument to digitalPinToGPIONumber
:
arduino-esp32/cores/esp32/io_pin_remap.h
Line 44 in 7018cd1
🐛 This causes the interrupt number to be treated as an Arduino pin number, and that pin number converted to a GPIO number. In the case where the Arduino pin number is different from the GPIO number, this causes the interrupt to be attached to a different pin than the one the user intended.
Sketch
- Select the Nano ESP32 board from Arduino IDE's Tools > Board menu.
- Select Tools > Pin Numbering > By Arduino pin (default) from the Arduino IDE menus.
- Upload the following sketch to a Nano ESP32 board:
const int interruptPin = D2; volatile bool interruptFlag = false; void setup() { Serial.begin(9600); pinMode(interruptPin, INPUT_PULLUP); // Also set the pull-up on the pin that is incorrectly attached due to the bug. pinMode(digitalPinToGPIONumber(interruptPin), INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(interruptPin), interruptISR, CHANGE); } void loop() { if (interruptFlag) { interruptFlag = false; Serial.println("interrupt triggered"); } } void interruptISR() { interruptFlag = true; }
- Open Serial Monitor.
- Connect the pin labeled "D2" on the Nano ESP32 silkscreen to ground.
🐛 Nothing is printed in Serial Monitor, indicating that the interrupt was not triggered even though the sketch code should have attached it to Arduino pin 2. - Connect the pin labeled "D5" on the Nano ESP32 silkscreen to ground.
🐛 "interrupt triggered
" is printed in Serial Monitor, even though the sketch code should not have attached the interrupt to Arduino pin 5.
Debug Message
N/A
Other Steps to Reproduce
I provided instructions for reproducing the fault in the "Sketch" section above.
Additional context
This was previously reported at #10209, but the reporter closed that issue after they found a workaround, even though the bug was not fixed.
CC: @pillo79
Additional reports
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.