ESP32 - Button - LED

The button is a basic component and widely used in many ESP32 projects. It is simple to use. However, it may make the beginners confuse, due to mechanical, physical issues and ways to use it as well. This tutorial makes it easy for the beginners

We are going to learn how to:

  • Turn on LED if button is pressing.
  • Turn off LED if button is NOT pressing.

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×push button
1×LED
1×220 ohm resistor
1×Breadboard
5×Jumper Wires

About Button and LED

ESP32 - Button

One button's pin is connected to VCC or GND. The other pin is connected to an ESP32 pin.

By reading the state of ESP32's pin (configured as input pin), we can detect the button is pressed or NOT.

Button State and Pressing State

The relation between the button state and the pressing state depends on how we connect the button with ESP32 and the setting of the ESP32's pin.

There are two ways to use a button with ESP32:

  • One button's pin is connected to VCC, the other is connected to an ESP32's pin with a pull-down resistor
    • If the button is pressed, ESP32's pin state is HIGH. If otherwise, ESP32's pin state is LOW
    • We MUST use an external resistor.
  • One button's pin is connected to GND, the other is connected to an ESP32's pin with a pull-up resistor
    • If the button is pressed, ESP32's pin state is LOW. If otherwise, ESP32's pin state is HIGH
    • We can use either an internal or external resistor. The internal resistor is built inside ESP32, we just need to set via ESP32 code.

※ NOTE THAT:

There are two common troubles that beginners usually get into:

  • Floating input problem:
    • Symptom: the reading value from the input pin is not matched with the button's pressing state.
    • Cause: input pin is NOT used pull-up or pull-down resistor.
    • Solution: Use pull-up or pull-down resistor. It will be described in Button - Pullup tutorial
  • Chattering phenomenon:
    • It should be considered in only some application that needs to detect exactly number of the pressing.
    • Symptom: Button is pressed one, but ESP32 code detects several times.
    • Cause:Due to mechanical and physical issues, the state of the button (or switch) is quickly toggled between LOW and HIGH several times
    • Solution:Debounce. It will be described in Button - Debounce tutorial.

To make it easy for beginners, this tutorial uses the simplest method: initializes the ESP32 pin as an internal pull-up input without using the external resistor. The beginners do NOT need to care about how to wire the pull-up/pull-down resistor. The beginners just need to use the ESP32 code.

ESP32 - LED

If you do not have knowledge of LED learn about it in Blink LED tutorial

Wiring Diagram

Schematic Diagram

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

Breadboard Connections

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

click to see ESP32 pin out

ESP32 Code

Copy

const int buttonPin = 2;                       // the number of the pushbutton pin
const int ledPin =  21;                        // the number of the LED pin
int buttonState = 0;                           // variable for reading the pushbutton status
void setup() {
    pinMode(ledPin, OUTPUT);                   // initialize the LED pin as an output:
    pinMode(buttonPin, INPUT_PULLUP);          // initialize the pushbutton pin as an input:
}                                               
void loop() {
   buttonState = digitalRead(buttonPin);       // read the state of the pushbutton value:
   if (buttonState == HIGH) {                  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
       digitalWrite(ledPin, HIGH);             // turn LED on:
   } else {
       digitalWrite(ledPin, LOW);              // turn LED off:
   }
}                

Quick Steps

  • power up your board
  • Open Arduino IDE
  • Select the right board
  • Select the right port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Arduino IDE Upload Code
  • Press and keep pressing the button several seconds
  • See the changes you made

Code Explanation

  • configure an ESP32's pin to digital input mode by using pinMode() function.
  • Read the pin state by using digitalRead(pinNumber)

Read the line-by-line explanation in comment lines of source code!

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!