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
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.
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;
}