Control LED using push button

I ran following example code on my mega board. LED goes turn on when I press push button and LED goes turn off when I release push button.

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);

I have heard the switch de-bouncing. When we press or release button, contacts of button connect several.

I don't get idea where is code written for switch de-bouncing in program

I don't get idea where is code written for switch de-bouncing in program


Take a view into the examples of the IDE.

I have copy pasted this code from IDE's example.

I don't understand where the code for switch de bouncing is written in the whole program

that's what your code is written to do. it could be just one line

void loop() {
    digitalWrite (ledPin, digitalRead (buttonPin));

sounds like you want the LED to turn on/off with each press of the button

byte butState;

void loop() {
    byte but = digitalRead (buttonPin);

    if (butState != but)  {     // state change
        butState = but;
        delay (10);             // debounce

        if (LOW == but)         // pressed
            digitalWrite (ledPin, !  digitalRead (ledPin));

Buttons must be in steady state to read buttons.

Can you please explain with real world example when the switch de bouncing is needed and ? when the switch de bouncing is not needed.

what do you mean?

a button switch is "pressed" on the transition from open to close and releases on the transition from closes to open

mechanical switches bounce. a conventional approach is to ignore any transitions that occur after a change in state. (optical switches have been used to avoid bounce)

the code i posted has a 10 msec delay to ignore subsequent transitions

Push ON - Push OFF

  1. Press the button and LED goes ON
  2. Release the button and the LED goes OFF
  3. Repeat all

can you explain it, in this example switch de bounce is needed or not if yes why is it and if not why not?

Well if you could see it, or look with an appropriate instrument, you would see that when you push the button down, the LED goes on and off many times, and ends up being on.

And when you release the button, you would againd]see the LED go off and on many times, landing finally being off.

Try writing a sketch that counts the number of times you press the button - one count up for each time you press the button.

Then you might just see what all we talking about when the necessity of debouncing pushbuttons, either in software or hardware, is mentioned.


1 Like

I want to understand before writing the code. When switch de bouncing is needed and when not.I gave an example and asked if I needed switch de bouncing or not.

see post #6

So do you understand? We can't really know if it matters to you.

If you don't care that your simple switch will be doing as I described, even if it doesn't look to be, then you are all set.

If on the other hand, turning on-off-on-off-on-on might be a problem, or off-on-off-on-off-on-off, for whatever you are really doing, then you need debouncing.



Good switches don't need a lot of debounce. Cheap switches give a burst of noise (contact bounce) when they switch. Some things don't care, others do. For sustained, simple operation with a good switch is easy, but if you're trying to count pulses the noise may kill your app. This is why you use edge sensing instead of level sensing to trigger an app. Switch debounce time is usually short ~10mS or so.

seems like this is all you want -- your done

The default de-bounce time in the Debounce.h Library is 50 ms.

But it is adjustable… 50 ms is too short for some switches, 10 ms way generous for others.

And of course you don't need to wait around for any number of milliseconds if all you talking about is acting on a pushbutton make (or break). If you are just a bit more than lazy.


Hopefully, the Debounce.h Library does contain a suitable method to adjust de-bounce time.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.