-->
Page 1 of 1

Beginner: External Wired Button Controlling Onboard LED

PostPosted: Tue Nov 01, 2016 11:38 pm
by primetime
Hi guys!

Just a bit of background. I started coding as a hobby and I'm moving on to programming microcontrollers. I picked up a ESP8266 based NodeMCU board (Lolin V3) and I'm playing around with it. I'm a novice at electronics also, so if I wire things improperly, I apologize. I'm posting this here to get some feedback as well as to help some other beginners with trying to figure stuff out.

Here's how I wired my button to the NodeMCU
Image

Code: Select all-- NodeMCU Onboard LED Control by External Button
led = 4
pin = 1

local count = 0

function onled()
   state = gpio.read(4)
   if state == 0 then
      gpio.write(led, gpio.HIGH)
      print("LED Off")
   else
      gpio.write(led, gpio.LOW)
      print("LED On")
   end
   gpio.write(pin, gpio.LOW)
   count = count + 1
   print("Push Counter is: "..count)
end


gpio.mode(pin, gpio.INT)
gpio.mode(led, gpio.OUTPUT)
gpio.write(pin,gpio.LOW)
gpio.trig(pin, "up", onled)


Quick explanation:
GPIO 4 has an LED (Lolin v3 module) that lights up when the pin is set to LOW

GPIO 1 is the "trigger" pin. GPIO 1 initial state is LOW. If voltage is applied to the PIN the GPIO 1 state is read as HIGH and the LED is turned off or on depending on the state of the LED. I wired it to the 3v3 pin that passes through the button. When the button is pressed, the connection is complete which routes 3 volts to GPIO 1.

Any feedback is appreciated. I am going to try to work on some sort of debounce function when I get the chance.

Re: Beginner: External Wired Button Controlling Onboard LED

PostPosted: Sat Nov 12, 2016 6:51 pm
by cmaciel
TerryE has a very good example of debouncing by SW. I am using it and it works great!

See the following thread:

viewtopic.php?f=24&t=6426

Regards,

Cesar

Re: Beginner: External Wired Button Controlling Onboard LED

PostPosted: Sun Nov 13, 2016 12:41 am
by swilson
Also, check out the Bounce2 library if you haven't. Works very good.

https://github.com/thomasfredericks/Bounce2

Re: Beginner: External Wired Button Controlling Onboard LED

PostPosted: Sun Nov 13, 2016 12:51 am
by trackerj
Might worth to take a look also to this post:

Buttons, pushbuttons and debouncing story


Image