Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By DirkusMaximus
#61645 While I've had some pretty decent success with my recently acquired NodeMCU v2 board, I am having one issue that's been bugging me. I have a button set up to a pin with gpio.trig to fire off a function. However my desk lamp ALSO fires off the function when I turn it on or off. So does the nearby water cooler, when the compressor kicks on or off. Sometimes just walking past it can fire it. I've read before that it's a good practice to pull unused gpio pins high to avoid this type of thing, but before I go jamming a bunch of resistors in there, I have some questions...

    * Do I also need to pull the SD1/2/3, CLK and CMD pins up? What about the two "reserved" pins next to ADC0? Could those also cause this?
    * Are there pins that I should NOT pull up? (aside from the GND/3v3/Vin of course)
    * Can I be a bit lazy and just pull up an unused rail on the bottom edge of my breadboard with a 10k resistor and then just tie them all to that, or do they each need to be pulled up separately to help isolate them from each other?

Thanks in advance to the hardware gurus out there trying to keep me from frying my board. :)
User avatar
By DirkusMaximus
#61691 Disregard. Solved my hardware problem in software. Added a check to see if the button was still pressed 1/8th of a second later. The EMI spikes are much shorter than that, I'm sure. Someone in the IRC channel said this was "debouncing", but my definition of debouncing was ignoring multiple triggers from a single button press. In this case, it's filtering out false inputs when the button ISN'T being pressed. Debounce, filtering, whatever you call it, this is the complete code for the gpio.trig I'm using in case any other noobs also run into this.

Code: Select allbutton_pin=1 -- whatever pin you have the button on
debounce_ms=5000 -- debounce time in millis, I use 5 seconds to use this as a rate limiter too
button_filter_ms=125 -- how long the button must be pressed to be considered a press.  Eliminates EMI issues. I use 125

gpio.trig(button_pin,"down",function()
      tmr.alarm(1,button_filter_ms,tmr.ALARM_SINGLE,function ()
      if (gpio.read(button_pin)==gpio.LOW) then
         debounce = tmr.now() - pulse;
         pulse = tmr.now();
         if (debounce < debounce_ms) then
            return;
         end
         -- Code to do whatever the button does here
      end
   end)
end)