2.3 GPIO - LED Button
Last updated
Last updated
In previous guide we learned how to use IO port output, and here we will use IO port as input. This example reads digital inputs like a button switch and control digital outputs like an LED using the ESP32-S3 with PlatformIO IDE.
A push button is an electronic switch. You can turn the switch on by pressing the switch button. When you release your hand, the switch is turned off.
To show you how to use digital inputs and digital outputs, we’ll build a simple project example with a pushbutton and an LED. We’ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.
Please note that when pushing the button swith, there is vibration that is not felt by humans, but it is completely sensed by the microcontroller. Usually we will use software delay of 10ms for debouncing. For example, when a button is pressed, the pin is low at the first reading, and then read the pin level again after a delay of 10ms. If it is still at a low level, it proves that the button has been pressed.
Here’s a list of the parts to you need to build the circuit:
ESP32-S3-N16R8
LED
330 - 1k Ohm resistor
Pushbutton
Breadboard
Jumper wires
Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 12 and the pushbutton to GPIO 5.
Here we use the GPIO pin for input, so when we use the pinMode
method, the second parameter cannot pass OUTPUT, and we need to use the digitalRead
method to get the input value.
Unlike output, when setting input pins, we need to configure pull-up or pull-down resistors to determine the high or low level in the circuit. To do this, we use the pinMode
function and the INPUT_PULLUP or INPUT_PULLDOWN constant. Such as you want to set a pin as a pull-up resistor, you need to call the pinMode(pin, INPUT_PULLUP) function, where pin is the pin number.
Copy the following code to your PlatformIO IDE.
In the following two lines, you create variables to assign pins:
The button is connected to GPIO 5 and the LED is connected to GPIO 12. Next, you create a variable to hold the button state. By default, it’s 0 (not pressed).
In the setup(), you initialize the button as an INPUT, and the LED as an OUTPUT. For that, you use the pinMode() function that accepts the pin you are referring to, and the mode: INPUT or OUTPUT.
In the loop() is where you read the button state and set the LED accordingly.
In the next line, you read the button state and save it in the buttonState variable. As we’ve seen previously, you use the digitalRead() function.
The following if statement, checks whether the button state is HIGH. If it is, it turns the LED on using the digitalWrite() function that accepts as argument the ledPin, and the state HIGH.
If the button state is not HIGH, you set the LED off. Just set LOW as a second argument to in the digitalWrite() function.
You can add debouncing as the following code.
In this section, you’ve learned how to read digital inputs and control digital outputs with the ESP32 using PlatformIO IDE.
Finally, if you want to learn more about other controllers, take a look at our Youtube channel:
Refer the previous tutorial: