Here is How to Turn on LED on Button Press and Turn Off After a Period on Arduino ESP32. For this guide, we will use the onboard boot button and the onboard LED of ESP32. You can not use the EN button of ESP32 for this purpose. The onboard boot button is attached to Pin 0 and the onboard LED of ESP32 attached to Pin 2.
This guide has more usage on advanced projects of IoT. Instead of LED, the job can be sending an alert or turning on bulb over the internet. The code of this guide is dependent on millis()
, avoiding delay()
. If you are new, you should read the function references on Arduino’s official site.
First, in slightly easy way :
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | void setup() { pinMode(2,OUTPUT); // LED output pinMode(0,INPUT); // Button input } void loop() { static unsigned char ledState = LOW; static unsigned char buttonState = LOW; static unsigned char lastButtonState = LOW; static unsigned long ledCameOn = 0; // If the LED has been on for at least 5 seconds then turn it off. if(ledState == HIGH) { if(millis()-ledCameOn > 5000) { digitalWrite(2,LOW); ledState = LOW; } } // If the button's state has changed, then turn the LED on IF it is not on already. buttonState = digitalRead(0); if(buttonState != lastButtonState) { lastButtonState = buttonState; if((buttonState == HIGH) && (ledState == LOW)) { digitalWrite(2,HIGH); ledState = HIGH; ledCameOn = millis(); } } } |
Now some programming matters. Using const
instead of define
may not consume RAM. unsigned long
stores the the current value of millis()
when the event takes place.
This is the final code with more control on timing of when LED will be on :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | const byte BUTTON=0; // boot button pin (built-in on ESP32) const byte LED=2; // onboard LED (built-in on ESP32) unsigned long buttonPushedMillis; // when button was released unsigned long ledTurnedOnAt; // when led was turned on unsigned long turnOnDelay = 20; // wait to turn on LED, 20 almost instant unsigned long turnOffDelay = 5000; // turn off LED after this time bool ledReady = false; // flag for when button is let go bool ledState = false; // for LED is on or not. void setup() { pinMode(BUTTON, INPUT_PULLUP); pinMode(LED, OUTPUT); digitalWrite(LED, LOW); } void loop() { // get the time at the start of this loop() unsigned long currentMillis = millis(); // check the button if (digitalRead(BUTTON) == LOW) { // update the time when button was pushed buttonPushedMillis = currentMillis; ledReady = true; } // make sure this code isn't checked until after button has been let go if (ledReady) { //this is typical millis code here: if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) { // okay, enough time has passed since the button was let go. digitalWrite(LED, HIGH); // setup our next "state" ledState = true; // save when the LED turned on ledTurnedOnAt = currentMillis; // wait for next button press ledReady = false; } } // see if we are watching for the time to turn off LED if (ledState) { // okay, led on, check for now long if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) { ledState = false; digitalWrite(LED, LOW); } } } |
The above code was originally written by James of baldengineer.com
. It is near perfect and avoids various physical problems with buttons.