4.2 Pull-up/down resistors, debouncing

— Justin Love @ 6:45 pm

So the last question was a trick one. Both schematics are wrong. Each schematic will work fine when the button is depressed (i.e. circuit closed). However, when the button is not depressed (i.e. circuit open) the input is not connected to anything. This is called a floating input and will give inconsistent data. To correct this problem we use either a pull-down resistor or a pull-up resistor.


Electricity behaves a bit like water insomuch that it will flow to where it encounters the least resistance. When a switch is CLOSED it has no resistance – in this case the current will flow through the switch since it has less resistance than our pull-up/down resistor. When the switch is OPEN it has infinite resistance – in this case the current will flow through our pull-up/down resistor to +5V or GND accordingly.

Wire up your board with the pull-up resistor schematic – use digital pin 2 for your input:

pull up / down resistors

Notice that 100 Ohm resistor between the DIGITAL PIN and the switch? This is in case you accidentally set the PIN to OUTPUT by mistake – the 100 Ohm resistor will prevent a short circuit. Tip by electronics diva Lada Ada

Ok, there is one last thing you need to know about switches.

Because of their mechanical nature, a switch’s contacts will sometimes rapidly strike against each other as the switch is closed – a phenomenon called switch bouncing, contact bounce, or chatter. This causes a rapid series of electrical pulses that may be interpreted by the microcontroller as multiple button presses.

Switch bouncing can be fixed by creating a more complex circuit that adds a capacitor and a Schmitt trigger. If you are using a microcontroller an easy solution is to add a small delay when a state change is detected.

Cut and paste the following code into the Arduino editor, compile it and upload it to your Arduino.

 * Button with debounce
 * by DojoDave, debouncing by roguescience.org
 * Turns on and off a light emitting diode(LED) connected to digital
 * pin 13, when pressing a pushbutton attached to pin 2. Re-checks
 * value after 10ms to make sure it is not a bounce.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status
int bounceCheck = 0;            // variable for debouncing

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare pushbutton as input

void loop(){
  val = digitalRead(inputPin);            //read input value
  delay(10);                              //wait 10ms
  bounceCheck = digitalRead(inputPin);   //check again
  if(val == bounceCheck){                //if val is the same then not a bounce
    if (val == HIGH) {                   //check if the input is HIGH
      digitalWrite(ledPin, LOW);         //turn LED OFF
    } else {
      digitalWrite(ledPin, HIGH);       //turn LED ON


  1. The arduino has built-in pullup resistor. That seems worth a mention.

    Comment by Josiah — August 9, 2009 @ 7:33 pm
  2. Yes – thanks Josiah – when I first created the tutorial I was unaware of the internal pullups – nonetheless I think it is a good exercise to teach people how pullup resistors work first since they are ubiquitous in electronics. To enable the built-in (20k) pullup (off by default) on a pin use the following code:

    pinMode(pin, INPUT); // set pin to input
    digitalWrite(pin, HIGH); // turn on pullup resistors

    Works for analog pins too.

    More details: http://www.arduino.cc/en/Tutorial/DigitalPins

    Comment by roguescience — August 10, 2009 @ 10:45 am
  3. I understand software debouncing but how do you do it with hardware??

    Comment by Robert AKA SubMicro — December 31, 2009 @ 11:51 am
  4. nice info.thaks..

    Comment by angelina — March 21, 2010 @ 3:56 am
  5. Robert here is a good example of hardware debouncing using a SPDT switch and cross-coupled NAND http://www.elexp.com/t_bounc.htm – some people also use an RC filter in conjunction with a Shmitt trigger.

    Comment by roguescience — June 10, 2010 @ 3:04 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2015 Roguescience Arduino Tutorials | powered by WordPress with Barecity