Skip to content

ESP32 Interrupt Argument Passed Incorrectly

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 Interrupt Argument Passed Incorrectly. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 interrupt callback receives wrong argument or NULL when a custom argument is passed.

Quick Fix

Wrong

int pin = 4;
gpio_isr_handler_add(4, myISR, &pin);  // pin goes out of scope
ISR receives dangling pointer. Reading it gives garbage values.
// Use static or heap-allocated argument
static int pinNumber = 4;
gpio_isr_handler_add(pinNumber, myISR, &pinNumber);
void IRAM_ATTR myISR(void* arg) {
  int pin = *(int*)arg;
  if (pin == 4) {
    // Handle GPIO 4
  }
}
ISR receives correct pin number. GPIO 4 events handled properly.

Prevention

Ensure argument remains valid while ISR is active. Use static or heap-allocated memory. Pass structured data via pointer to struct. Do not pass stack-local variables. Use uint32_t value for simple integers.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### Can I pass an integer directly as interrupt argument?

Yes. Cast to void*: gpio_isr_handler_add(4, isr, (void*)4). Cast back: int val = (int)arg. Works for small values up to pointer size.

Why does my ISR argument become garbage?

The argument pointer references a variable that went out of scope. Use static variables or heap memory that persists.

Can I pass a struct with multiple values?

Yes. Allocate a struct on heap, fill it, pass the pointer. Free it when ISR is removed or no longer needed.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro