Jennifer Chen
Published © GPL3+

Intro to Soldering & Debouncing

Learn how to solder and debounce a switch.

BeginnerFull instructions provided5,416
Intro to Soldering & Debouncing

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
3 mm LED: Green
3 mm LED: Green
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×5
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Connecting Pushbutton and LED to Arduino

Code

Debounce switch without using delay()

Arduino
// Only change LED state on button transition, add switch debouncing
// Debounces without using delay

// Massimo Banzi, from "Getting Started with Arduino"

const int LED = 13;    // the pin for the LED
const int BUTTON = 6;  // the input pin where the
			             // pushbutton is connected
int val = 0;     // val will be used to store the state
		     // of the input pin
int old_val = 0; // this variable stores the previous
			// value of "val"
int state = 0;   // 0 = LED off and 1 = LED on

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

void setup() {
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input
}

void loop(){
  val = digitalRead(BUTTON); // read input value and store it
  unsigned long currentMillis = millis(); //check for current time
  
  
 //give time for debouncing
  if(currentMillis - previousMillis >= 100){
    
  // check if there was a transition
  // which depends on the current value 
  // AND the previous value
    if ((val == 1) && (old_val == 0)){
      state = 1 - state; // change the state
     }
     
     previousMillis = currentMillis; // save the last time the button 
                                     // was pressed
     old_val = val; // val is now old, let's store it
  }   



  if (state == 1) {
    digitalWrite(LED, HIGH); // turn LED ON
  } else {
   digitalWrite(LED, LOW);  // turn LED OFF
  }
}

Switch Debouncing using delay()

Arduino
// Only change LED state on button transition, add switch debouncing
// Massimo Banzi, from "Getting Started with Arduino"

const int LED = 13;    // the pin for the LED
const int BUTTON = 6;  // the input pin where the
			             // pushbutton is connected
int val = 0;     // val will be used to store the state
		     // of the input pin
int old_val = 0; // this variable stores the previous
			// value of "val"
int state = 0;   // 0 = LED off and 1 = LED on

void setup() {
  pinMode(LED, OUTPUT);   // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input
}

void loop(){
  val = digitalRead(BUTTON); // read input value and store it

  // check if there was a transition
  // which depends on the current value 
  // AND the previous value
  if ((val == HIGH) && (old_val == LOW)){
    state = 1 - state; // change the state
    delay(10);           // wait for switch to stop bouncing
  }

  old_val = val; // val is now old, let's store it

  if (state == 1) {
    digitalWrite(LED, HIGH); // turn LED ON
  } else {
   digitalWrite(LED, LOW);
  }
}

Credits

Jennifer Chen

Jennifer Chen

13 projects • 12 followers

Comments