Tutorial 7 – Debouncing a Pushbutton

Welcome back to the CraftedTech Engineering Arduino tutorial series! In Tutorial 6, we learned how to use pushbuttons to control an LED. Now, we’re going to tackle a common issue when working with pushbuttons: debouncing.

When you press a pushbutton, the mechanical contacts may create multiple rapid on/off signals, known as bouncing. In this tutorial, we’ll show you how to debounce the button using software, ensuring smooth and reliable input for your projects.


🔌 What You’ll Need

  • 1 Arduino Board (e.g., Arduino UNO)
  • 1 Pushbutton
  • 1 10kΩ Resistor
  • 1 LED
  • 1 220Ω Resistor
  • Breadboard
  • Jumper wires
  • USB cable for Arduino

You can order the required components here:


🧠 What Is Button Debouncing?

When a mechanical button is pressed or released, the electrical contact may bounce several times before settling into its final state. This can lead to false readings, like multiple button presses. Debouncing is the technique used to filter out these unwanted signals, ensuring that each button press or release is registered only once.


🧾 Step-by-Step Instructions

🔧 Step 1: Circuit Wiring

  • One side of the pushbutton to digital pin 2
  • Other side of the pushbutton to 5V
  • 10kΩ resistor from pin 2 to GND (pull-down resistor)
  • LED anode (long leg) to digital pin 13 via 220Ω resistor
  • LED cathode to GND

💻 Step 2: Arduino Code with Debouncing

int buttonPin = 2; // Pin connected to the pushbutton int ledPin = 13; // Pin connected to the LED int buttonState = 0; // Current button state int lastButtonState = 0; // Previous button state unsigned long lastDebounceTime = 0; // Time of last button press unsigned long debounceDelay = 50; // Debounce delay in milliseconds void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int reading = digitalRead(buttonPin); // Read the button state // Check if button state has changed if (reading != lastButtonState) { lastDebounceTime = millis(); // Reset debounce timer } // If enough time has passed without changes, consider it stable if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // Only toggle the LED if the new button state is HIGH if (buttonState == HIGH) { digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle LED Serial.println("Button Pressed"); } } } lastButtonState = reading; // Store the button state for the next loop }

🔍 Code Breakdown

  • Debounce logic: The code uses the millis() function to measure the time between button state changes.
  • The debounceDelay ensures that only stable button presses (after a slight delay) are processed.
  • The LED toggles only if a valid, debounced button press occurs.

🧪 Try This!

  • Change the debounce delay to see how it affects the reliability of button presses.
  • Add multiple buttons and control more LEDs with debouncing!

🛠️ Troubleshooting Tips

  • Ensure your wiring matches the code for correct button behavior.
  • If the LED flickers unexpectedly, adjust the debounce delay.
  • Use the Serial Monitor to track button presses and debug if needed.

🧭 What You Learned in This Tutorial

✔️ How to debounce a pushbutton using Arduino
✔️ The importance of filtering button bounce
✔️ How to toggle an LED based on a debounced button press
✔️ The role of timing functions like millis() in Arduino code


📌 Next Up: Tutorial 8 – Using Multiple LEDs with PWM

In our next tutorial, we’ll show you how to control multiple LEDs with Pulse Width Modulation (PWM), allowing you to create smooth lighting effects.


📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!

🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering

 

Back to blog

Leave a comment