CPP: Button - lvidarte/esp8266 GitHub Wiki

Schematic

Download the project

Checkout this PlatformIO project code here

Code

#include <Arduino.h>

// D0 - Internal LED (GPIO16)
// D5 - Button (GPIO14)

void setup ()
{
  pinMode(D0, OUTPUT);
  pinMode(D5, INPUT_PULLUP);
}

void loop ()
{
  static uint8_t state;

  // digitalRead() returns the current level of the pin,
  // either 1 for a high logic level or 0 for a low logic level.
  // Notice how the button is at a high level (value returns 1) when
  // it's not pressed. This is because the pull-up resistor keeps the pin at
  // a high level when it's not connected to ground through the button.
  // When the button is pressed then the input pin connects to ground
  // and reads a low level (value returns 0).
  state = digitalRead(D5);

  // Remember that the internal led turn on
  // when the pin is LOW
  digitalWrite(D0, state);

  delay(100);
}

Pull Up resistor

With a pull-up resistor, the input pin will read a high state when the button is not pressed. In other words, a small amount of current is flowing between VCC and the input pin (not to ground), thus the input pin reads close to VCC. When the button is pressed, it connects the input pin directly to ground. The current flows through the resistor to ground, thus the input pin reads a low state.

Here is a good explanation about this circuit.

⚠️ **GitHub.com Fallback** ⚠️