Skip to content

Technik Blog

Programmieren | Arduino | ESP32 | MicroPython | Python | Raspberry Pi | Raspberry Pi Pico

Menu
  • About me
  • Contact
  • Deutsch
  • English
Menu

Arduino UNO – Button debouncing

Posted on 6. September 202210. March 2024 by Stefan Draeger

If you build a pushbutton into your circuit, sooner or later you will encounter that it sends a signal more than once. This behavior is called “bouncing”.

Arduino UNO - Button debouncing
Arduino UNO – Button debouncing

In the tutorial Arduino Lektion 33: Taster mit Pull Down Widerstand I already explained how to build a small circuit with a push button and a pull down resistor. However, this circuit has exactly the described problem, if you press the button and immediately release it again more than one signal is sent.

  • Resources required
  • Circuit – button with pull down resistor at Arduino UNO
  • Sample code with bouncing button
  • Solution with Bounce2 library
  • Code – Button with Bounce2 lib without bouncing

Resources required

If you want to build the following example, then you need:

  • Arduino UNO R3*,
  • USB Cable*,
  • button for Breadboard connection*,
  • 10 kOhm Resistor*,
  • breadboard wires*, 10 cm, male – male,
  • breadboard*

Note from me: The links marked with an asterisk (*) are affiliate links. If you make a purchase through these links, I will receive a small commission to help support this blog. The price for you remains unchanged. Thank you for your support!

Circuit – button with pull down resistor at Arduino UNO

Arduino UNO with Button and Pull Down Resistor
Arduino UNO with Button and Pull Down Resistor

Sample code with bouncing button

const int btnUp = 2; //Button connected at digital Pin D2
int statusBtnUp = 0; //Field for storage the state
int index = 0; // counter
void setup() {
  Serial.begin(9600); //Begin of serial communication with 9600 baud
  pinMode(btnUp,INPUT); //set button pin as input
}
void loop() {
  //if the button was pressed then...
  if(digitalRead(btnUp) == HIGH){ 
    //increment the index 
    index = index+1;
    //print the value to the serial interface
    Serial.println(index);
  }
}

Solution with Bounce2 library

Here the Bounce2 library helps, this library contains functionalities which suppress exactly this “bouncing” of buttons. (How to install a library of the Arduino IDE I have already explained in detail in the tutorial Arduino IDE, Einbinden einer Bibliothek).

Arduino IDE - Bounce2 Bibliothek
Arduino IDE – Bounce2 Bibliothek

If the library has been successfully installed, then it must be added to the Sketch via the command “#include “.

First of all, an instance of “Bounce” must be created for each button. In the constructor of the Bounce object the pin to which the button is connected and additionally a value for a pause (in milliseconds) is passed.

//include library
#include <Bounce2.h>

//define field for button pin
//in my case the button is attachted to digital pin 2
#define BTN 2

//create a instance from button with pin and a delay of 50 ms.
Bounce btnBouncer = Bounce(BTN, 50);

In the “setup” function, the pinMode for the buttons must be set to Input as before.

//define that the button pin works as input
pinMode(BTN, INPUT);

The function “loop” must be rewritten significantly, because we now use the commands and functions from the Bounce2 library.

In the first step we have to update the button.

//update the button
btnBouncer.update();

To check afterwards whether the button has been pressed.

//if the button was pressed, then...
if(btnBouncer.fell()){
  //increment the index field
  index = index+1;
  //print the value to the serial interface
  Serial.println(index);
}

If you want to check whether the button has been released again, use the “rose” function.

Code – Button with Bounce2 lib without bouncing

Here now the whole sketch to rebuild and test yourself.

#include <Bounce2.h>
#define BTN 2
int index = 0;
Bounce btnBouncer = Bounce(BTN, 50);
void setup() {
  Serial.begin(9600);
  pinMode(BTN, INPUT);
  
  digitalWrite(BTN, HIGH);  
}
void loop() {
  btnBouncer.update();
  if(btnBouncer.fell()){
    index = index+1;
    Serial.println(index);
  }
  
}

Leave a Reply Cancel reply

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

Fragen oder Feedback?

Du hast eine Idee, brauchst Hilfe oder möchtest Feedback loswerden?
Support-Ticket erstellen

Newsletter abonnieren

Bleib auf dem Laufenden: Erhalte regelmäßig Updates zu neuen Projekten, Tutorials und Tipps rund um Arduino, ESP32 und mehr – direkt in dein Postfach.

Jetzt Newsletter abonnieren

Unterstütze meinen Blog

Wenn dir meine Inhalte gefallen, freue ich mich über deine Unterstützung auf Tipeee.
So hilfst du mit, den Blog am Leben zu halten und neue Beiträge zu ermöglichen.

draeger-it.blog auf Tipeee unterstützen

Vielen Dank für deinen Support!
– Stefan Draeger

Categories

Links

Blogverzeichnis Bloggerei.de TopBlogs.de das Original - Blogverzeichnis | Blog Top Liste Blogverzeichnis trusted-blogs.com

Stefan Draeger
Königsberger Str. 13
38364 Schöningen

Tel.: 01778501273
E-Mail: info@draeger-it.blog

Folge mir auf

  • Impressum
  • Datenschutzerklärung
  • Disclaimer
  • Cookie-Richtlinie (EU)
©2025 Technik Blog | Built using WordPress and Responsive Blogily theme by Superb
Cookie-Zustimmung verwalten
Wir verwenden Cookies, um unsere Website und unseren Service zu optimieren.
Funktional Always active
Die technische Speicherung oder der Zugang ist unbedingt erforderlich für den rechtmäßigen Zweck, die Nutzung eines bestimmten Dienstes zu ermöglichen, der vom Teilnehmer oder Nutzer ausdrücklich gewünscht wird, oder für den alleinigen Zweck, die Übertragung einer Nachricht über ein elektronisches Kommunikationsnetz durchzuführen.
Vorlieben
Die technische Speicherung oder der Zugriff ist für den rechtmäßigen Zweck der Speicherung von Präferenzen erforderlich, die nicht vom Abonnenten oder Benutzer angefordert wurden.
Statistiken
Die technische Speicherung oder der Zugriff, der ausschließlich zu statistischen Zwecken erfolgt. Die technische Speicherung oder der Zugriff, der ausschließlich zu anonymen statistischen Zwecken verwendet wird. Ohne eine Vorladung, die freiwillige Zustimmung deines Internetdienstanbieters oder zusätzliche Aufzeichnungen von Dritten können die zu diesem Zweck gespeicherten oder abgerufenen Informationen allein in der Regel nicht dazu verwendet werden, dich zu identifizieren.
Marketing
Die technische Speicherung oder der Zugriff ist erforderlich, um Nutzerprofile zu erstellen, um Werbung zu versenden oder um den Nutzer auf einer Website oder über mehrere Websites hinweg zu ähnlichen Marketingzwecken zu verfolgen.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Einstellungen anzeigen
{title} {title} {title}