Debounced Circuit
Learn how to debounce a circuit using an Arduino Uno, a few resistors and an LED.

Code written in C++
The purpose of this circuit is to only allow one button press to be registered each time the button is pressed. Once the button is pressed the LED goes from HIGH to LOW or vice versa. This code was programmed into an Arduino UNO. We then used a 1.50kΩ from the 5V power source to the switch. A 200kΩ resistor that goes from the switch to ground and a 3kΩ resistor going from a digital pin 3 to an LED. The digital pin 2 reads whether the the switch is HIGH or LOW.
#define pushButton 2
#define led 3
int LEDState = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
Serial.print("start \n");
pinMode(led, OUTPUT);
pinMode(pushButton, INPUT);
digitalWrite(led, LEDState);
}
void loop() {
// put your main code here, to run repeatedly:
debounce();
}
void debounce() {
int reading = digitalRead(pushButton);
// Read last button state and keep track of the time when the state changed
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// Current time - time when the last button state changed has to be greater then 0.05 seconds.
if ((millis() - lastDebounceTime) > debounceDelay) {
// If current reading is does not match the buttonState updated the buttonState to match
if (reading != buttonState) {
buttonState = reading;
// If the buttonState is HIGH, change the LEDState
if (buttonState == HIGH) {
LEDState = !LEDState;
}
}
}
digitalWrite(led, LEDState);
// Update the lastButton State to the current reading
lastButtonState = reading;
}