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.
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.
There are two common approaches:
In this project, we will use software debouncing.
In the previous project:
In this project:
The button therefore toggles the LED between two states.
The LED's state is remembered even after the button is released.
You will need:
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.
This variable stores the button's current state.
It can be either:
HIGH
or:
LOW
This variable is important because the Arduino needs to compare the current button state with its previous state.
For example:
Previous state: LOW
This variable remembers whether the LED is currently ON or OFF.
That allows us to toggle it.
buttonState = digitalRead(buttonPin);
The Arduino continuously reads D9.
The result is stored in buttonState.
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.
ledState = !ledState;
The ! operator means logical NOT.
Therefore:
LOW → HIGH
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.
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.
lastButtonState = buttonState;
The current state becomes the previous state for the next iteration.
This allows the Arduino to detect the next transition.
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.
Press 1 → LED ON
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 important condition is:
buttonState == HIGH && lastButtonState == LOW
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.
Can you modify the program so that:
First button press:
For example:
Press 1 → Count = 1
You learned:
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:
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.
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.
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
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:- The current button state.
- The previous button state.
- The current LED state.
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.
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.
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:
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:- Connect the Arduino Uno R3 to your computer.
- Select the correct board.
- Select the correct port.
- Click Verify.
- If the sketch compiles successfully, click Upload.
- Wait for the upload to complete.
- 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.
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.
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.
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.
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.



Comments
Post a Comment