Skip to main content

Arduino Push Button Toggle: Debouncing a Push Button to Control an LED

Learn how to use a push button as a toggle switch and prevent unwanted multiple inputs using software debouncing.

In the previous project, we learned how to use a push button as a digital input to control an LED.

The behavior was straightforward:

Button pressed → LED ON

Button released → LED OFF

But what if we want the button to behave like a toggle switch?

For example:

Press once → LED ON

Press again → LED OFF

Press again → LED ON

This is different from the previous project because the LED should remain in its current state after the button is released.

To accomplish this, we need to detect a new button press rather than simply checking whether the button is currently pressed.

There is another issue we need to address: mechanical button bounce.

In this project, we will learn how to use software debouncing to make the Arduino reliably recognize each physical button press as a single event.

What You Will Learn

After completing this project, you will understand:
  • What mechanical switch bounce is.
  • Why button debouncing is necessary.
  • The difference between a button's current state and a new button press.
  • How to use variables to remember a button's previous state.
  • How to detect a state change.
  • How to implement a toggle function.
  • How delay() can be used for simple software debouncing.

What Is Button Bounce?

A push button is a mechanical component.

When you physically press a button, its internal contacts do not always transition cleanly from one electrical state to another. They can make and break contact several times within a very short period.

Instead of the Arduino seeing:
LOW → HIGH

it may temporarily see something more like:
LOW → HIGH → LOW → HIGH → LOW → HIGH

Although this happens very quickly, the Arduino can detect those changes.
This means that one physical press could potentially be interpreted as multiple presses.
This unwanted behavior is called switch bounce or contact bounce.

What Is Debouncing?

Debouncing is the process of preventing these rapid unwanted transitions from being interpreted as multiple button presses.

There are two common approaches:

Hardware debouncing

Additional electronic components, such as capacitors and resistors, can be used to filter the signal.

Software debouncing

The Arduino program can wait briefly after detecting a button transition and then check the button again.
In this project, we will use software debouncing.

What Is a Toggle?

Before writing the code, it is important to understand what we want the button to do.

In the previous project:
  • Button pressed → LED ON 
  • Button released → LED OFF

In this project:
  • First press → LED ON 
  • Second press → LED OFF 
  • Third press → LED ON 
  • Fourth press → LED OFF

The button therefore toggles the LED between two states.
The LED's state is remembered even after the button is released.

Supplies

The hardware setup is the same as Project #04: Controlling an LED Using a Push Button.

You will need:
  • Arduino Uno R3
  • 1 × LED
  • 1 × Push button
  • 1 × 220 Ω resistor
  • 1 × 10 kΩ resistor
  • Solderless breadboard
  • Jumper wires
  • USB cable
  • Computer with the Arduino IDE

Hardware Setup

Since the hardware is the same as the previous project, you can reuse the same circuit.


Connections

LED

  • LED anode (+) - D3 through the 220Ω resistor
  • LED cathode (-) - GND

Push Button

  • Push button - D9
  • 10 kΩ resistor - Button input and GND
  • Other side of button - 5V
The 10 kΩ resistor acts as a pull-down resistor, ensuring that the button input has a defined LOW state when the button is not pressed.

How the Circuit Works

When the button is released:
Button → LOW

When the button is pressed:
Button → HIGH

The Arduino reads this state through D9.

The important difference in this project is that we don't simply ask:

"Is the button currently pressed?"

Instead, we ask:

"Has the button just changed from released to pressed?"

That distinction allows us to toggle the LED once for each press.

Coding

The program needs to remember three things:
  1. The current button state.
  2. The previous button state.
  3. The current LED state.
We can represent these using variables.

Complete Code

const int ledPin = 3; 
const int buttonPin = 9; 
int buttonState = LOW; 
int lastButtonState = LOW; 
int ledState = LOW; 

void setup() { 
pinMode(ledPin, OUTPUT); 
pinMode(buttonPin, INPUT); 
digitalWrite(ledPin, ledState); 
} 

void loop() { 
buttonState = digitalRead(buttonPin); 
if (buttonState == HIGH && lastButtonState == LOW) { 
ledState = !ledState; 
digitalWrite(ledPin, ledState); 
delay(50); 
} 
lastButtonState = buttonState; 
}

Understanding the Code

1. Define the LED and Button Pins

const int ledPin = 3; 
const int buttonPin = 9;

These variables tell the Arduino where the LED and push button are connected.

2. Store the Button's Current State

int buttonState = LOW;

This variable stores the button's current state.
It can be either:
 
HIGH

or:
 
LOW

3. Store the Previous Button State

int lastButtonState = LOW;

This variable is important because the Arduino needs to compare the current button state with its previous state.

For example:
 
Previous state: LOW 
Current state: HIGH

This tells us that the button has just been pressed.

4. Store the LED State

int ledState = LOW;

This variable remembers whether the LED is currently ON or OFF.
That allows us to toggle it.

5. Read the Button

Inside loop():
 
buttonState = digitalRead(buttonPin);

The Arduino continuously reads D9.
The result is stored in buttonState.

6. Detect a New Button Press

This is the most important part:

if (buttonState == HIGH && lastButtonState == LOW) {

We are checking two conditions:
 
Current button state = HIGH AND Previous button state = LOW

In other words:

The button was previously released and has just become pressed.

This is called state change detection.
It prevents the Arduino from toggling the LED continuously while the button is being held down.

7. Toggle the LED

Inside the if statement:
 
ledState = !ledState;

The ! operator means logical NOT.

Therefore:
 
LOW → HIGH 
HIGH → LOW

So every time a new button press is detected, the LED changes to the opposite state.
For example:
LED OFF 
↓ 
Button press 
↓ 
LED ON
↓
Button press
↓
LED OFF

8. Write the New State to the LED

digitalWrite(ledPin, ledState);

This sends the value stored in ledState to the LED.

If ledState is HIGH, the LED turns ON.
If ledState is LOW, the LED turns OFF.

9. Add a Short Debounce Delay

delay(50);

This introduces a short 50-millisecond pause after detecting a button press.
The purpose is to give the mechanical contacts time to settle before the program continues reading the button.
For a simple beginner project, this provides an easy-to-understand form of software debouncing.

10. Remember the Current State

At the end of the loop:
 
lastButtonState = buttonState;

The current state becomes the previous state for the next iteration.
This allows the Arduino to detect the next transition.

Understanding the Complete Logic

The program effectively does this:
Read button 
↓ 
Is button HIGH 
AND was it previously LOW? 
↓ 
YES 
↓ 
Toggle LED 
↓ 
Wait briefly 
↓ 
Save current button state 
↓ 
Repeat

If the button remains pressed:
 
HIGH → HIGH → HIGH → HIGH

there is no new LOW → HIGH transition, so the LED does not keep toggling.

When the button is released:
 
HIGH → LOW

and then pressed again:
 
LOW → HIGH

the Arduino detects another new press and toggles the LED again.

Uploading the Program

After entering the code:
  1. Connect the Arduino Uno R3 to your computer.
  2. Select the correct board.
  3. Select the correct port.
  4. Click Verify.
  5. If the sketch compiles successfully, click Upload.
  6. Wait for the upload to complete.
  7. Press the push button and observe the LED.

Expected Result

The LED should behave like a toggle switch:
Press 1 → LED ON 
Press 2 → LED OFF 
Press 3 → LED ON 
Press 4 → LED OFF 
...

The LED should remain in its current state after the button is released.
This is different from the previous project, where the LED followed the button's state directly.

Troubleshooting

The LED toggles multiple times from one press

Check your debounce logic.

Make sure you have:
 
delay(50);

after detecting the button press.

You can experiment with values such as:
 
delay(20);

or:
 
delay(100);

to see how the behavior changes.

The LED does not toggle

Check:
  • Button wiring.
  • 10 kΩ pull-down resistor.
  • LED polarity.
  • 220 Ω resistor.
  • D3 and D9 connections.
  • Arduino board and port selection.

The LED turns ON but immediately turns OFF

Make sure you are detecting a LOW → HIGH transition, rather than simply checking whether the button is HIGH.

The important condition is:
 
buttonState == HIGH && lastButtonState == LOW

Experiment 1: Change the Debounce Time

Try:
 
delay(20);

Then try:
 
delay(100);

Observe whether the behavior changes.
This is a useful way to understand the relationship between software timing and mechanical inputs.

Experiment 2: Add a Second LED

Try adding another LED.
Can you modify the program so that:

First button press:
LED 1 → ON LED 2 → OFF

Second button press:
LED 1 → OFF LED 2 → ON

This introduces the idea of controlling multiple outputs from a single input.

Experiment 3: Create a Counter

Instead of simply switching the LED ON and OFF, try counting button presses.

For example:
Press 1 → Count = 1 
Press 2 → Count = 2 
Press 3 → Count = 3 
...

You could eventually display the count on an LCD or seven-segment display.
This is where a simple push button begins becoming a useful user-interface component.

What You Learned

In this project, you learned how to make a push button behave like a toggle switch using Arduino.

You learned:
  • What mechanical switch bounce is.
  • Why debouncing is necessary.
  • How software can be used to reduce the effects of switch bounce.
  • How to store the current button state.
  • How to store the previous button state.
  • How to detect a LOW → HIGH transition.
  • How to toggle an output using the ! operator.
  • How a button can control an output without the output directly following the button's state.

An Important Programming Concept: State

This project introduces an important idea that you will encounter repeatedly in programming and embedded systems: state.

The Arduino needs to remember what happened previously.

For example:
Previous state = LOW Current state = HIGH

The Arduino can determine that the button has just been pressed.

Without remembering the previous state, the program would not know whether the button is:
  • continuously being held down, or
  • has just been pressed.
This concept of storing and comparing states will become increasingly useful in more advanced Arduino projects.

Going Further: Better Debouncing

The delay(50) method is deliberately simple and appropriate for introducing the concept.
However, delay() pauses the Arduino's execution. This can become a problem when an Arduino project needs to perform other tasks at the same time.
A more advanced implementation uses millis() to debounce the button without blocking the program.
That approach is worth learning as your projects become more complex.

Conclusion

In the previous project, we learned how to use a push button as a digital input.
In this project, we took the next step by making the button behave like a toggle switch.

We also learned why mechanical buttons can produce unwanted rapid transitions and how software debouncing can help us interpret a physical button press reliably.

The key idea is:

Physical button → detect state change → debounce → toggle output

This simple pattern is used in many real-world embedded systems and user interfaces.

What's Next?

We have now learned how to:
  • Control an output.
  • Read an input.
  • Detect changes in an input.
  • Maintain the state of an output.
  • Handle a mechanical input more reliably.
The next projects can build on these concepts by introducing additional components and more complex interactions.

Comments

Popular posts from this blog

Arduino Serial Communication Beginner Guide (UART Explained)

Serial communication is the backbone of most Arduino projects. It allows your Arduino to talk to your computer, Bluetooth modules, Wi-Fi modules, sensors, and even other Arduinos. If you have ever used the Serial Monitor, you have already used serial communication - now you will understand how it really works. By the end of this guide, you will learn: What serial communication is What UART means How Arduino communicates with devices How to send and receive data How baud rate works Common mistakes beginners make What is Serial Communication? Serial communication is a method of sending data one bit at a time over a single wire. Instead of sending many bits simultaneously (parallel communication), serial sends bits sequentially: Example: The letter A in binary = 01000001 These bits are sent one after another very quickly. This method is: Simple Reliable Perfect for microcontrollers What is UART? UART stands for: Universal Asynchronous Receiver Transmitter It is the hardware ins...

How to Connect HC-06 Bluetooth Module with Arduino (Step-by-Step Beginner Guide)

Bluetooth communication is one of the easiest ways to make Arduino projects wireless. Once you understand how to use a Bluetooth module, you can build robots, home automation systems, and smartphone-controlled devices. In this tutorial, you will learn how to interface the HC-06 Bluetooth module with Arduino and send data wirelessly using your phone.    By the end of this guide, you will be able to: Understand how the HC-06 Bluetooth module works Configure the module using AT commands Safely connect it to an Arduino Send and receive serial data over Bluetooth Build the foundation for wireless Arduino projects Watch the Project Overview What is the HC-06 Bluetooth Module? The HC-06 is a Bluetooth-to-Serial module designed to add wireless communication to microcontrollers such as Arduino. It uses Bluetooth 2.0 (SPP - Serial Port Protocol), which means it behaves like a wireless serial cable. Whatever is sent from the phone appears in Arduino’s Serial Monitor and vice versa. ...

How to Use SoftwareSerial on Arduino (Connect Multiple Serial Devices)

In the previous tutorial, you learned how Arduino uses hardware serial communication through pins 0 (RX) and 1 (TX). That built-in serial port is mainly used to communicate with your computer through the USB cable and Serial Monitor . But here is the problem beginners quickly face: Many popular modules use serial communication, such as: Bluetooth modules ( HC-05 / HC-06 ) GPS modules GSM modules Wi-Fi modules ( ESP8266 ) Arduino Uno has only one hardware serial port. So how can we connect these modules while still using the Serial Monitor? The solution is SoftwareSerial . In this tutorial you will learn how to create additional serial ports using normal digital pins. By the end, your Arduino will be able to talk to multiple serial devices. What is SoftwareSerial? SoftwareSerial is a special Arduino library that allows digital pins to behave like RX and TX pins. This means Arduino can create extra serial ports using software instead of hardware. Hardware Serial vs Software Seri...