ESP32 - Knop - Debounce

Wanneer een knop wordt ingedrukt/losgelaten of wanneer een schakelaar wordt omgezet tussen AAN en UIT, verandert de status één keer van LOW naar HIGH (of HIGH naar LOW). Is dit correct?

⇒ Nee, dat is het niet. Dit komt omdat in de fysieke wereld wanneer u eenmaal op een knop drukt, de knopstatus snel meerdere keren schakelt tussen LOW en HIGH in plaats van slechts één keer. Dit is een mechanische en fysieke eigenschap. Dit fenomeen staat bekend als chattering. Door chattering leest een MCU (bijv. ESP32) meerdere knopdrukken terwijl slechts één druk daadwerkelijk is uitgevoerd. Dit veroorzaakt verkeerde werking. Het proces om dit fenomeen te elimineren heet debounce. Deze handleiding laat zien hoe u dit doet.

ESP32 chattering phenomenon

Deze tutorial behandelt:

Hardware vereist

1×ESP32 ESP-WROOM-32 Ontwikkelingsmodule
1×USB-kabel Type-C
1×Breadboard-montage knop met kap
1×Breadboard-montage knopkit
1×Paneel-montage drukknop
1×Drukknopmodule
1×Breadboard (experimenteerprint)
1×Jumperdraden
1×(optioneel) DC-voedingsconnector
1×(Aanbevolen) Schroefklem Uitbreidingsboard voor ESP32
1×(Aanbevolen) Breakout Expansion Board for ESP32
1×(Aanbevolen) Stromsplitter voor ESP32

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over knop

We hebben specifieke tutorials over knoppen. De tutorial bevat uitgebreide informatie en stapsgewijze instructies over hardware pinout, het werkingsprincipe, de bedrading naar ESP32, ESP32 code en meer... Meer weten? Bezoek de volgende link:

Bedradingsschema

ESP32 Knop Bedradingsschema

This image is created using Fritzing. Click to enlarge image

Om het duidelijk te maken, laten we ESP32 code draaien ZONDER en MET debounce en de resultaten vergelijken

Knop uitlezen zonder Debounce

Snelle stappen

  • Als u voor het eerst ESP32 gebruikt, bekijk dan hoe u de omgeving instelt voor ESP32 in Arduino IDE.
  • Maak de bedrading volgens bovenstaande afbeelding.
  • Sluit de ESP32 aan op uw PC via een micro-USB-kabel.
  • Open Arduino IDE op uw PC.
  • Kies de juiste ESP32 board (bijv. ESP32 Dev Module) en COM-poort.
  • Kopieer onderstaande code en plak deze in Arduino IDE.
/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-button-debounce */ #define BUTTON_PIN 21 // GIOP21 pin connected to button // Variables will change: int lastState = LOW; // the previous state from the input pin int currentState; // the current reading from the input pin void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); if (lastState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if (lastState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last state lastState = currentState; }
  • Compileer en upload de code naar het ESP32 bord door op de Upload knop in Arduino IDE te klikken
Arduino IDE Upload Code
  • Open de Serial Monitor in Arduino IDE
Hoe open je Serial Monitor in Arduino IDE
  • Druk één keer op de knop, houd deze enkele seconden ingedrukt en laat los.
  • Bekijk het resultaat in de Serial Monitor. Dit ziet er ongeveer zo uit:
COM6
Send
The button is pressed The button is pressed The button is pressed The button is released The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

⇒ Zoals u ziet, heeft u slechts één keer gedrukt en losgelaten, maar de ESP32 leest meerdere drukken en loslaten.

※ Notiz:

Het chattering fenomeen gebeurt niet altijd. Mocht het niet optreden, probeer dan bovenstaande test meerdere keren.

Knop uitlezen met Debounce

Snelle stappen

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-button-debounce */ #define BUTTON_PIN 21 // GIOP21 pin connected to button #define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters // Variables will change: int lastSteadyState = LOW; // the previous steady state from the input pin int lastFlickerableState = LOW; // the previous flickerable state from the input pin int currentState; // the current reading from the input pin // the following variables are unsigned longs because the time, measured in // milliseconds, will quickly become a bigger number than can be stored in an int. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // initialize the pushbutton pin as an pull-up input // the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed. pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: currentState = digitalRead(BUTTON_PIN); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited long enough // since the last press to ignore any noise: // If the switch/button changed, due to noise or pressing: if (currentState != lastFlickerableState) { // reset the debouncing timer lastDebounceTime = millis(); // save the the last flickerable state lastFlickerableState = currentState; } if ((millis() - lastDebounceTime) > DEBOUNCE_TIME) { // whatever the reading is at, it's been there for longer than the debounce // delay, so take it as the actual current state: // if the button state has changed: if(lastSteadyState == HIGH && currentState == LOW) Serial.println("The button is pressed"); else if(lastSteadyState == LOW && currentState == HIGH) Serial.println("The button is released"); // save the the last steady state lastSteadyState = currentState; } }
  • Compileer en upload de code naar het ESP32 bord door op de Upload knop in Arduino IDE te klikken
  • Open de Serial Monitor in Arduino IDE
Hoe open je Serial Monitor in Arduino IDE
  • Houd de knop enkele seconden ingedrukt en laat los.
  • Bekijk het resultaat in de Serial Monitor. Dit ziet er ongeveer zo uit:
COM6
Send
The button is pressed The button is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

⇒ Zoals u ziet, drukte en liet u één keer los, en de ESP32 registreert ook één keer drukken en loslaten. Het chattering effect is geëlimineerd.

Wij maken het eenvoudig - ESP32 Knop Debounce Code met Library

Om het makkelijk te maken voor beginners, vooral bij meerdere knoppen, hebben we een knoplibrary ontwikkeld, genaamd ezButton. U kunt hier meer leren over de ezButton library.

ESP32 Knop Debounce Code voor één enkele knop

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-button-debounce */ #include <ezButton.h> #define DEBOUNCE_TIME 50 // debounce tijd in milliseconden, verhoog deze als er nog chatter optreedt ezButton button(21); // maakt een ezButton object aan, gekoppeld aan pin GPIO21 void setup() { Serial.begin(9600); button.setDebounceTime(DEBOUNCE_TIME); // debounce tijd instellen op 50 milliseconden } void loop() { button.loop(); // MOET eerst de loop() functie aanroepen if (button.isPressed()) Serial.println("The button is pressed"); if (button.isReleased()) Serial.println("The button is released"); }

ESP32 Knop Debounce Code voor meerdere knoppen

Laten we debounce code schrijven voor drie knoppen.

Het bedrading schema

ESP32 Knop Library Bedradingsschema

This image is created using Fritzing. Click to enlarge image

/* * Deze ESP32 code is ontwikkeld door newbiely.nl * Deze ESP32 code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/esp32/esp32-button-debounce */ #include <ezButton.h> #define DEBOUNCE_TIME 50 // debounce tijd in milliseconden, verhoog deze als er nog chatter optreedt ezButton button1(21); // maakt een ezButton object aan voor pin GPIO21; ezButton button2(22); // maakt een ezButton object aan voor pin GPIO22; ezButton button3(23); // maakt een ezButton object aan voor pin GPIO23; void setup() { Serial.begin(9600); button1.setDebounceTime(DEBOUNCE_TIME); // debounce tijd instellen op 50 milliseconden button2.setDebounceTime(DEBOUNCE_TIME); button3.setDebounceTime(DEBOUNCE_TIME); } void loop() { button1.loop(); // MOET eerst de loop() functie aanroepen button2.loop(); button3.loop(); if (button1.isPressed()) Serial.println("The button 1 is pressed"); if (button1.isReleased()) Serial.println("The button 1 is released"); if (button2.isPressed()) Serial.println("The button 2 is pressed"); if (button2.isReleased()) Serial.println("The button 2 is released"); if (button3.isPressed()) Serial.println("The button 3 is pressed"); if (button3.isReleased()) Serial.println("The button 3 is released"); }

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

Aanvullende kennis

  • De DEBOUNCE_TIME waarde hangt af van de hardware. Verschillende hardware kan verschillende waarden vereisen.
  • Debounce moet ook toegepast worden op aan/uit schakelaars, limiet schakelaars, reed switches, touch sensors ...

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!