> [!TLDR] Using LEDs and Buttons on [[ESP32]] > This tutorial explains how to control LEDs and buttons with an [[ESP32]] microcontroller. It covers hardware connections, requiring an [[ESP32]], [[Diodo emissor de luz|LED]], resistor, button, and software like the [[Arduino IDE]]. Code examples demonstrate controlling the [[Diodo emissor de luz|LED]] based on button presses. The first example turns the [[Diodo emissor de luz|LED]] on when the button is pressed. The second example toggles the [[Diodo emissor de luz|LED]]'s state with each button press and includes debouncing to prevent spurious readings from button bounce. # Introduction Using LEDs and buttons with an [[ESP32]] involves controlling GPIO pins (General Purpose Input/Output). Here's a breakdown of how to do it! ## Requisites - Hardware - [[ESP32]] - [ESP32](app://obsidian.md/ESP32) is a series of low cost and low power consumption microcontrollers. It is also a system-in-one-chip with integrated microcontroller, Wi-Fi and Bluetooth - [[Diodo emissor de luz]] - Common prototype [[Diodo emissor de luz|LED]]. - [[Resistores]] - Used to limit the current thought the [[Diodo emissor de luz|LED]] - [[Botões e interruptores]] - Used to control when the [[Diodo emissor de luz|LED]] is on or off. - Software - [[Arduino IDE]] or [[Wokwi]] for programming/simulating the code to run on the [[ESP32]]. - Make sure to [[Instalando o driver da ESP32 no Windows|install the ESP32 driver]]. # Hardware Connections * **LED:** * **Anode (longer leg):** Connect to an [[ESP32]] GPIO pin through a current-limiting resistor (220-470 ohms is typical). The resistor prevents excessive current from damaging the [[Diodo emissor de luz|LED]]. * **Cathode (shorter leg):** Connect to GND. * **Button:** * **One button pin:** Connect to an [[ESP32]] GPIO pin. * **Other button pin:** Connect to GND. > [!INFO] Pull-up resistor > You might also want a pull-up resistor (10k ohms) between the GPIO pin and 3.3V to ensure a stable reading when the button isn't pressed. The [[ESP32]] has built-in pull-up [[Resistores]] that you can enable in software by defining the pin as an `INPUT_PULLUP`. ![[Using LEDs and Buttons on ESP32 - Hardware.png]] # Example Code 1 - Controlling an LED with a Button ```cpp const int ledPin = 2; // GPIO pin connected to the LED const int buttonPin = 4; // GPIO pin connected to the button void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor } void loop() { // Read the button state (LOW when pressed, HIGH when not pressed) int buttonState = digitalRead(buttonPin); // If the button is pressed, turn the LED on if (buttonState == LOW) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } } ``` **Explanation:** * `pinMode(ledPin, OUTPUT);`: Configures the `ledPin` as an output. * `pinMode(buttonPin, INPUT_PULLUP);`: Configures the `buttonPin` as an input and enables the internal pull-up resistor. * `digitalRead(buttonPin);`: Reads the digital value (HIGH or LOW) of the `buttonPin`. * `digitalWrite(ledPin, HIGH);`: Turns the LED on. * `digitalWrite(ledPin, LOW);`: Turns the LED off. # Example 2 - Toggling an LED with a Button ```cpp const int ledPin = 2; const int buttonPin = 4; bool ledState = false; void setup() { pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT_PULLUP); } void loop() { int buttonState = digitalRead(buttonPin); // Check for button press and debounce (optional but recommended) static unsigned long lastDebounceTime = 0; unsigned long debounceDelay = 50; // Debounce time in milliseconds if (buttonState == LOW && millis() - lastDebounceTime > debounceDelay) { lastDebounceTime = millis(); // Reset the debounce timer ledState = !ledState; // Toggle the LED state digitalWrite(ledPin, ledState ? HIGH : LOW); } } ``` **Explanation of Toggling and Debouncing:** * **Toggling:** The `ledState = !ledState;` line inverts the `ledState` variable each time the button is pressed, effectively toggling the LED on and off. * **Debouncing:** Buttons can "bounce" (rapidly fluctuate between HIGH and LOW) when pressed. Debouncing code ignores these fluctuations, ensuring a single clean press is registered. The example above uses a simple debouncing technique.