Free Shipping over ₹1199

ESP32-S3 Button Controlled LED with Arduino IDE

Introduction

This project introduces digital input handling using the ESP32-S3 DevKit-N16R8. A push button is connected to a digital input pin to control an LED. When the button is pressed, the LED turns on; when the button is released, the LED turns off — demonstrating real-time input detection.

Required Components

  • ESP32-S3 Board
  • LED
  • 220Ω, 10KΩ Resistor
  • Push Button
  • Jumper Wire
  • Breadboard

Pinout

  • Switch Pinouts
    • Button Pressed: When you press the button, the metal contacts inside touch, creating a connection. This allows electricity to flow, completing the circuit.
    • Button Released: When you release the button, the contacts separate, breaking the connection. This stops the flow of electricity, turning off whatever device it’s controlling.
  • LED Pinout
    • LED Anode (+)
    • LED Cathode (-)

Circuit Diagram

  • ESP32-S3 GPIO 2 → 220Ω Resistor → LED (Anode +)
  • ESP32-S3 GND → LED (Cathode -)
  • ESP32-S3 GPIO 0 → One Side of Push Button
  • ESP32-S3 GND → One Side of Push Button
  • ESP32-S3 3.3V → One Side of Push Button

Code / Programming

/*
Filename: ol_button_led_control.ino  
Description: ESP32-S3 program to control the onboard LED using a push-button. LED turns ON when the button is pressed and OFF when released.  
Author: www.oceanlabz.in  
Modification: 1/4/2025  
*/

#define LED_PIN 2           // Onboard LED is connected to GPIO 2
#define BUTTON_PIN 0        // GPIO 0 (BOOT button or external push button)

void setup() {
  pinMode(LED_PIN, OUTPUT);                     // Set LED pin as output
  pinMode(BUTTON_PIN, INPUT_PULLUP);            // Set button pin as input with internal pull-up resistor
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);    // Read the button state

  if (buttonState == LOW) {                     // If button is pressed (LOW)
    digitalWrite(LED_PIN, HIGH);                // Turn LED ON
  } else {
    digitalWrite(LED_PIN, LOW);                 // Turn LED OFF
  }
}

Explanation

  • Push button is connected to GPIO 0, using the internal pull-up resistor.
  • When the button is pressed, GPIO 0 reads LOW, and the onboard LED (GPIO 2) turns on.
  • When the button is released, GPIO 0 reads HIGH, and the LED turns off.

Troubleshooting

  • LED Not Responding? Check if GPIO 2 is used and onboard LED is supported on your board revision.
  • LED Always ON? Ensure internal pull-up is enabled or manually connect a 10kΩ resistor to 3.3V.
  • Button Not Working? Try using another safe GPIO like 4 or 10 instead of GPIO 0.

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Need Help?