Page 1 of 1

I'm using and ESP-32CAM and a reed switch triggers an interrupt on GPIO2 but not on GPIO4, any idea why?

Posted: Tue Oct 24, 2023 5:06 pm
by FBMinis
I'm programming this AI-Thinker ESP-32CAM in the Arduino IDE. Im not using any of the camera functions right now. The code below is what I have running.

Passing a magnet by the reed switch increases the counter. Trying the same code on pin 4 (GPIO4) doesnt produce any result.

Code: Select all

static void IRAM_ATTR reedswitch_ISR(void* arg);

int reedswitchpin = 2;
uint32_t numberToggles;
volatile bool toggled = false;
unsigned long reed_time = 0;
unsigned long last_reed_time = 0;

static void setupinterrupts() {
  pinMode(reedswitchpin, INPUT_PULLUP);

  // Install the GPIO ISR service
  gpio_install_isr_service(0); // You can use a different number (0, 1, 2) if needed

  esp_err_t err = gpio_isr_handler_add((gpio_num_t)reedswitchpin, &reedswitch_ISR, (void*) NULL);

  if (err != ESP_OK) {
    Serial.printf("gpio_isr_handler_add failed (%x)", err);
  }
  gpio_set_intr_type((gpio_num_t)reedswitchpin, GPIO_INTR_NEGEDGE);
}

static void IRAM_ATTR reedswitch_ISR(void* arg) {
  reed_time = millis();

  if (reed_time - last_reed_time > 250) {
    numberToggles++;
    toggled = true;
    last_reed_time = reed_time;
  }
}

void setup() {
  Serial.begin(115200);
  delay(100);
  setupinterrupts();
  delay(100);
  Serial.println("waiting...");
}

void loop() {
  if (toggled) {
    Serial.printf("reed has been toggled %u times\n", numberToggles);
    Serial.println(" ");
  }
  toggled = false;
}

Re: I'm using and ESP-32CAM and a reed switch triggers an interrupt on GPIO2 but not on GPIO4, any idea why?

Posted: Wed Oct 25, 2023 10:37 am
by FBMinis
I have learned that GPIO4 is connected to the onboard "flash" LED of the AI Thinker ESP32CAM.

I was able to setup interrupts on GPIO16 and GPIO02 that behave correctly.

Is there a way to setup and interrupt on GPIO04 if I dont plan on using he sdcard nor the flash led?

Re: I'm using and ESP-32CAM and a reed switch triggers an interrupt on GPIO2 but not on GPIO4, any idea why?

Posted: Wed Oct 25, 2023 6:31 pm
by PepeTheGreat
GPIO4 is alredy used for flash LED?
I do use gpio 12,13,15,14,2 and they all work.
If you want use gpio4 disable "ledcAttachPin(4, 7); //pin4 is LED"

Re: I'm using and ESP-32CAM and a reed switch triggers an interrupt on GPIO2 but not on GPIO4, any idea why?

Posted: Wed Oct 25, 2023 6:49 pm
by FBMinis
Thank you. It seems that removing R13 also disables the LED circuit, allowing to use GPIO4 as long as SDCards is not being used.