Project tutorial
Intro to Soldering & Debouncing

Intro to Soldering & Debouncing © GPL3+

Learn how to solder and debounce a switch.

  • 3,358 views
  • 4 comments
  • 14 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

About this project

Intro to Soldering

This week's assignment was to create solder 4 short pieces of wire.  Adafruit's soldering tutorial has detailed instructions and was very helpful.  Instead of wasting new wire, I used pieces of scrap wire I had laying around, which happened to be stranded wire.

First I used a wire strippers to strip the ends of the wires.  The wire stripper has different holes for several common wire thickness, my wire was 28 gauge.  Then I twisted the ends together and used a soldering iron to melt the solder onto the connected wire.  


Debouncing

I connected an Arduino Uno to a pushbutton switch to change the state of an LED during the button transition.  The LED is connected to pin 13, which also controls the on-board LED.  The switch is connected to pin 6.  

Due to physical and mechanical issues, a single press of the button may be read as multiple transitions.  The Arduino debouncing tutorial explains it well.  To solve this issue, we can use the delay() function to debounce the button.  However, while the delay() function is being run, the Arduino won't read any button transitions.  



Another method to debounce is to use the millis() function to measure the amount of time between button transitions.  

I also did a video of debouncing using millis() in dark so the change in LED state is more visible.

After debouncing using millis(), the LED is much more responsive to button transitions.  If the LED is currently on, pushing the button will turn it off.  Similarly, if the LED is currently off, pushing the button will turn it on.


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);
  }
}

Schematics

Connecting Pushbutton and LED to Arduino
Blink without delay wiring

Comments

Similar projects you might like

Perfect Parker

Project tutorial by scott mangiacotti

  • 2,870 views
  • 2 comments
  • 9 respects

Portable Arduino Temp/Humidity Sensor with LCD

Project tutorial by ThothLoki

  • 153,323 views
  • 115 comments
  • 185 respects

Electronic Piano Keyboard With Preset Songs

Project tutorial by Lindsay Fox

  • 77,973 views
  • 60 comments
  • 150 respects

Simple Programmable Robotic Arm

Project showcase by Ryan Chan

  • 49,017 views
  • 63 comments
  • 145 respects

Arduino Simple "Camera Slider" Electronics

Project tutorial by Ian Cumming

  • 5,758 views
  • 3 comments
  • 15 respects

The Magnetic Field and RGB Tester

Project tutorial by Kutluhan Aktar

  • 3,530 views
  • 0 comments
  • 14 respects
Add projectSign up / Login