Jul 25

Interrupt firing only once? - Arduino IDE + ESP8266

Category: Design,electronics  , — Published by goeszen on July 25, 2017 at 3:07 pm

One line fix: Your problem is that you're not using a PULLUP! (via)

Long story

Adding a simple push-button to an experiment is pretty straightforward. It's simple with NodeMCU's firmware and LUA, and just as simple in an Arduino IDE sketch. Just make sure that you either add a pullup-resistor externally, on breadboard, or use the internal one and make the pin an "INPUT plus PULLUP" (see image and source-code on bottom of the post).

Compare the Arduino docs about DigitalPins and InputPullup. If you don't add the PULLUP, a registered attachInterrupt even will fire once, for example on "FALLING" (when the button connects pin "button" to Ground), but a subsequent RISING will not trigger again.

A bit of code to catch a button being pressed, in Arduino lingo, with some debouncing (manually done, in contrary to using a debounce lib). The button for this code is wired to Ground and Pin D7:

volatile long debounce_timeout = 0;
#define button 13 // NodeMCU label is D7, GPIO pin is 13, constant D7 would be a shorthand for value 13


...

void button_down() {
  detachInterrupt(button);
  attachInterrupt(button, button_up, RISING);
  debounce_timeout = millis();
}

void button_up() {
  detachInterrupt(button);
  attachInterrupt(button, button_down, FALLING);
  if ( debounce_timeout + 100 < millis() ) {
    // set a flag here, which triggers things to do in loop() when the button has been pressed
  }
}

void setup() {
  pinMode(button, INPUT_PULLUP);
  attachInterrupt(button, button_down, FALLING);
}

void loop() {
}

--

This post is part of a series of Mini Tutorials on the ESP8266:
Previous post: Arduino IDE on ESP8266 (NodeMCU dev board)
Next port: Fixing being unable to access tty on Ubuntu

I'm tinkering here with a NodeMCU ESP8266 (ESP-12E) board.
I'm using the Arduino IDE.

Leave a Reply

=