Arduino Buttons

This is a three part question but very related.

  1. I have the ESP8266 12e, I have performed the following connection (shown below), but on D0 (GPIO16), when I do this, the second blue light turns on, what is this light, is this a bad thing?

    (Image credit from: ESP8266 NodeMCU Digital Inputs and Digital Outputs (Arduino IDE) | Random Nerd Tutorials)

  2. Is there a way to set up the same circuit with (I think up to 16 times/terminals) but using only one resistor, (note, that I only need inputs, no outputs including LEDs)?

  3. Is there anything that I should know about when setting up 16 buttons? Is this the max, or is it less (or more)?

  • the blue light is likely a built in LED that you turn on and off. Depends on your ESP board on which GPIO it is
  • each (external) LED will need its own resistor.
  • buttons don't need an external resistor if you set the pin as INPUT_PULLUP (that will reverse the logic, LOW will mean button is pressed).

1. This is the picture (Fig-1) of my NodeMCU (ESP8266-12E) with two onboard Blue LEDs which I have marked as Led1 and Led2.

nodeMCUPicLed
Figure-1:

2. Led1 is connected internally with GPIO-4/D4, and it blinks during sketch uploading. The Led1 also responds to blink program of Step-4.

3. Led2 is connected internally with GPIO-16/D0. The Led2 also responds to blink program of Step-4. Caution: Do not connect any circuit with GPIO-16/D0 during sketch uploading as it prevents the sketch from uploading!

4. Tset Sketch to blink Led1/Led2.

#define ledpin 16  //or 4

void setup()                                                                                                                  
{
  pinMode(ledpin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  digitalWrite(ledpin, HIGH);
  delay(1000);
  digitalWrite(ledpin, LOW);
  delay(1000);
}

Thanks, but how do I do this with out resisters? Can you share a diagram/flow?
All of the replies are super helpful!!!

how to do what?

How do I connect buttons to the device with out resistors. The diagram that I saw said that I needed the resistor.

See the first part of this tutorial about how to use INPUT_PULLUP mode with a button. It shows how to wire diagonally across the button to guarantee you pick up switched legs.

https://roboticsbackend.com/arduino-input_pullup-pinmode/

HUGE THANKS!!!!! I appreciate your help everyone!!!!

1. Every GPIO-pin (when configured as an input line) can be optionally connected with an internal pull-up resistor (Fig-1). In that case, there is no need to use external pull-up or pull-down resistors.

sw1led1D
Figure-1:

2. The following sketch checks that Button is closed and then turns on Led3.

#define Button 12  //GPIO-12
#define Led3 4

void setup()
{
    Serial.begin(115200);
    pinMode(Button, INPUT_PULLUP);    //internal pull-up is connected
    pinMode(Led3, OUTPUT);
    digitalWrite(Led3, LOW);    //Led3 is OFF
}

void loop()
{
    if(digitalRead(Button) == LOW)  //when Button is closed, logic level at Dpin-12 is LOW
    {
          digitalWrite(Led3, HIGH);  //Led3 is ON
          while(1);    //wait for ever
    }
}

3. Button is a mechanical switch, and it makes hundreds of make-and-break events before settling at the final closed position. This behavior is known as bouncing. If you want to detect the final state of the Button once bouncing has disappered, you may apply software debouncing by including the Debounce.h Library in the sketch. Here is the sketch that detects the closed condition of the debounced Button.

#include<Debounce.h>
Debounce Button(12);       //Here, Button is an objcet attached with DPin-12
#define Led3 4

void setup()
{
    Serial.begin(115200);
    pinMode(12, INPUT_PULLUP);    //internal pull-up is connected
    pinMode(Led3, OUTPUT);
    digitalWrite(Led3, LOW);    //Led3 is OFF
}

void loop()
{
    if(!Button.read() == LOW)  //when Button is closed, logic level at Dpin-12 is LOW
    {
          digitalWrite(Led3, HIGH);  //Led3 is ON
          while(1);    //wait for ever
    }
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.