Learning Objectives
-
Learn how to use a pushbutton as input with the ESP32 in Wokwi.
-
Understand the concept of state toggling (press once = ON, press again = OFF).
-
Learn about debouncing to avoid false button presses.
-
Simulate the project online without physical hardware.
What You’ll Need in Wokwi
-
ESP32 board
-
Pushbutton
-
LED
-
Resistor (220Ω for LED, 10kΩ for button pull-down if needed — but we’ll use
INPUT_PULLUPin code) -
Wires
Steps in Wokwi
-
Go to https://wokwi.com.
-
Start a new project → Select ESP32.
-
Add the following parts from the Add Part (+) menu:
-
1 LED
-
1 Pushbutton
-
2 resistors (optional if not using internal pull-up)
-
-
Wire the circuit as follows:
-
LED anode (+) → GPIO 14 of ESP32
-
LED cathode (–) → resistor → GND
-
Pushbutton one leg → GPIO 12 of ESP32
-
Pushbutton opposite leg → GND
-
By learning how to detect button presses, control an LED, and even modify the logic to create a toggle with debouncing, you’ve taken an important step toward building more interactive and practical electronic projects. These basic principles are the foundation for countless real-world applications, from home automation to IoT systems
CODE:
#include <Arduino.h> // Ensures ESP32 recognizes Arduino functions
#define LED_PIN 14
#define BUTTON_PIN 12
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable internal pull-up resistor
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // Read button
if (buttonState == LOW) { // Button pressed
digitalWrite(LED_PIN, HIGH); // Turn LED on
} else {
digitalWrite(LED_PIN, LOW); // Turn LED off
}
}
Comments
Post a Comment