0

For an ESP32, under Mongoose OS, I’m trying to write some code to detect that a button was pushed (GPIO pin pulled to GND). I wrote the code below, but it constantly prints that the button is pushed, so it thinks the button is always pushed, except when I actually push it. When I push and hold it, the output stops. The button is wired between the GPIO pin and GND, with no pullup resistor since there is an internal pullup. I wonder if my code is wrong and would appreciate your comments, thank you.

I have pasted the relevant code below:

// GPIO 36
#define BTN_MOB 36

#ifdef BTN_MOB
mgos_gpio_set_mode(BTN_MOB, MGOS_GPIO_MODE_INPUT);
#endif

static void button_cb(int pin, void *pParam)
{
  if(pin == BTN_MOB)
    LOG(LL_INFO, ("***** BUTTON PRESSED\r\n"));
}

mgos_gpio_set_button_handler(BTN_MOB,
                  MGOS_GPIO_PULL_UP,
                  MGOS_GPIO_INT_EDGE_NEG, 
                  100 /* debounce ms */,
                  button_cb, /* callback handler */
                  NULL); /* arguments to callback handler */
Jim Archer
  • 1,337
  • 5
  • 30
  • 43

1 Answers1

1

As it turns out, GPIO pins 34, 35, 36 and 39 are actually GPI - input only and have no internal pullup or pulldown resistors. I switched to a different GPIO with internal pullup and this solved the problem.

Jim Archer
  • 1,337
  • 5
  • 30
  • 43