#104
The LED lights up when the button is pressed and turns off when released. Shows how the ESP32's digital input pin and internal pull-up resistor are used. The INPUT_PULLUP mode eliminates the need for an external resistor.


1const int LED_PIN = 5;
2const int BUTTON_PIN = 4;
3
4void setup() {
5 pinMode(LED_PIN, OUTPUT);
6 pinMode(BUTTON_PIN, INPUT_PULLUP);
7}
8
9void loop() {
10 if (digitalRead(BUTTON_PIN) == LOW) {
11 digitalWrite(LED_PIN, HIGH);
12 } else {
13 digitalWrite(LED_PIN, LOW);
14 }
15}