Arduino Button Debounce Tutorial

preview_player
Показать описание
Arduino Button Tutorial: How to debounce a button on an Arduino Uno or Mega.

Arduino Turorial: How to use a Button with Arduino Uno

In this video we learn how to use a button with Arduino. We can now add buttons to all of our projects! It is very easy when you know why this happens and how to overcome it.

What is bounce?

Contact bounce (also called chatter) is a common problem with mechanical switches and relays. Switch and relay contacts are usually made of springy metals. When the contacts strike together, their momentum and elasticity act together to cause them to bounce apart one or more times before making steady contact. The result is a rapidly pulsed electric current instead of a clean transition from zero to full current. The effect is usually unimportant in power circuits, but causes problems in some analogue and logic circuits that respond fast enough to misinterpret the on‑off pulses as a data stream.

The effects of contact bounce can be eliminated by use of mercury-wetted contacts, but these are now infrequently used because of the hazard of mercury release. Alternatively, contact circuits can be low-pass filtered to reduce or eliminate multiple pulses. In digital systems, multiple samples of the contact state can be taken or a time delay can be implemented in order for the contact bounce to settle before the contact input is used to control anything. Bounce in an SPDT switch can be eliminated by an SR latch. All of these methods are referred to as "debouncing" circuits.

By analogy, the term "debounce" has arisen in the software development industry to describe rate-limiting or throttling the frequency of a method's execution.

--------------------
Code & Parts
--------------------

Want to learn to code?
Рекомендации по теме
Комментарии
Автор

I hope you took away lots from this video. Let me know below!
PS: I'm creating an OOP course! Sign up at for early access and a 50% launch discount.

Educs
Автор

Excellent. Best explanation I've seen. I've toiled for days with code that only reads once, sets millis but never reads again. This makes no sense. Why wait and then never check again. You are correct, and your explanation of WHY is perfect. Great Job

RonFinlay
Автор

Instead of the 10K resistor, you could use the internal pull up resistors by writing INPUT_PULLUP instead of just INPUT in pinMode.

kieran_hammond
Автор

FANTASTIC TUTORIAL...I've been looking for a debounce video/article and yours is the only one that's worked! Thnx.

alexandralazarenko
Автор

Great explanation. In my case I'd use the output of a TOF timer with the delay of the debouncing time, so that you don't freeze the arduino during the delay. In my blog TOF, TON and TP timers can be downloaded.

j-
Автор

I think hardware debounce is better because sometimes the code needs to run very fast without any delays.

parkerjohn
Автор

aight, imma try to use this for computer programming lab 3. I cant get my code to work so I'm gonna restart and use this

shipuku
Автор

thank you!! finally explained simple and easy!!!

alexkatsis
Автор

A modular way of adding the debounce function to the code. And the debouce timing is in msecs.

tianlun
Автор

Thanks I think this worked well for my piezzo buzzer program.

TraceguyRune
Автор

τελειο, ευχαριστω πολυ, συνεχισε την καλη δουλεια!

BisdremisKostas
Автор

In your debounceButton function are you sure you want to read the PIN again? what if you happen to read at the time the pin is at the same state as the previous state? it will make you think the state hasn't changed when infact it had.

yoMisterWhyte
Автор

this really helps...can you tell me how to run nema 17 with single press of button... i have to hold the button to rotate the motor. Also do explain how i can stop motor with encounter of IR LED signal

sb-rjyb
Автор

Thank you! That's what i was looking for!

cyjexdc
Автор

Use a timer instead if you can't use delay

mijkolsmith
Автор

Thanks a lot for your tutorials you definitely are gonna gain more subs your awesome

MsSebaastian
Автор

sir i want one proj if i press once led should on and if i press again led should go like that i wat 8 inputs and outputs.

ningarkumar
Автор

Contact bounce is not caused by elasticity or the switch physically rebounding, it is caused by the resistance of the air being overcome slightly before the switch is physically closed. This can happen multiple times on any given closing or opening of a physical switch. This can be seen whenever you see a spark while opening or closing of a switch. This spark would not be there if the switch were actually physically closed or fully open.

nmjerry
Автор

Beautiful Greek's English accent :)

polemos
Автор

Hi, nice video, any chance you could show how to solve duration of pushbutton case? I mean short vs long press will take different affect, eg. turn different LED's?

pawelwojcik
welcome to shbcf.ru