
Bu projeyi gerçekten kurmak istersen parçaları hobidevre.com'dan temin edebilirsin →
Build it for real
To build this project for real, get the parts from hobidevre.com → hobidevre.com →
More build guides: hobidevre.com/blog
What bouncing looks like
When you press a pushbutton, two metal contacts meet. They do not settle into a perfect connection at once; they can make and break contact a few times as they come together. The same happens on release. The duration varies with the switch and its condition, but it is short — typically a matter of milliseconds.
A human would never notice this. An Arduino input reads thousands of times per second, so it can see each flicker as a separate press. A program that counts button presses may count five or six for a single click, or an interrupt may fire repeatedly for one action.
The circuit: pull-up or pull-down
A button needs a defined voltage when it is not pressed. Without a resistor, the input is left floating and can read unpredictably. The simplest solution on an Arduino is the internal pull-up, enabled with INPUT_PULLUP. Connect one side of the button to the input pin and the other to GND. The input normally reads HIGH and goes LOW when the button is pressed.
The alternative is an external pull-down resistor holding the input LOW until the button connects it to the supply. Both approaches work. The internal pull-up uses fewer parts and inverts the logic, so remember that pressed means LOW in code.
See it with a counter
Before fixing bounce, prove it exists. Write a tiny sketch that increments a counter every time the input changes state and prints the count to the serial monitor. Press the button once. If the number jumps by more than one, you are seeing contact bounce rather than a code error.
Observing the problem first matters because a different symptom can look similar. A floating input, a loose wire and a real bounce all produce extra changes, but only bounce is cured by a debounce routine.
Software debounce with millis()
The robust software approach keeps track of the last stable reading and the time it was taken. Each loop, read the pin and compare it with that stable value. If it differs, start a timer. Only accept the new state as stable once the input has remained different for the whole debounce interval. This ignores any flicker that settles quickly.
Use millis rather than delay for the timing. millis returns the time since the board started, so the loop stays free to do other work while the timer runs. The interval does not need to be exact; a short window comfortably longer than the contact settling time is usually enough.
const int BUTTON = 2;
const int LED = 13;
unsigned long sonDegisim = 0;
int kararliDurum = HIGH;
int okunan = HIGH;
void setup() {
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int simdiki = digitalRead(BUTTON);
if (simdiki != okunan) {
sonDegisim = millis();
okunan = simdiki;
}
if (millis() - sonDegisim > 30 && kararliDurum != okunan) {
kararliDurum = okunan;
digitalWrite(LED, kararliDurum == LOW ? HIGH : LOW);
Serial.println(kararliDurum == LOW ? "basildi" : "birakildi");
}
}
Why blocking delay is a poor fix
A quick fix is to wait a short moment after detecting a change and then read the pin again. It often works for a simple demo, but delay stops the processor completely. While it waits, nothing else runs: no sensor is read, no display updates and no other button is serviced.
As soon as a project does more than blink one LED, that pause becomes a problem. The timer-based version costs a few variables and behaves the same regardless of what else the loop is doing.
A small hardware filter
Debounce can also be handled in hardware. A resistor and capacitor at the input smooth the fast transitions so the pin sees one slower edge. A Schmitt trigger input is often added to turn that slow edge into a clean logic transition.
Hardware filtering removes the burden from software and is useful when an interrupt must fire exactly once. It also adds parts and takes board space, so for most hobby projects the timer method is the simpler choice.
Choosing an approach
For one or two buttons in a normal sketch, use INPUT_PULLUP and a millis-based filter. For an interrupt-driven input that must be exact, consider a hardware filter as well. In either case, test with real presses rather than only by shorting the wires with a tool, because a tool makes much cleaner contact than a finger.
Get the parts
Pushbuttons and resistors for these experiments are available from hobidevre.com.
Frequently asked questions
How long does contact bounce last?
It is usually only a few milliseconds and varies between switches, which is why a short filter window rather than an exact value is used.
Do I need a resistor with INPUT_PULLUP?
No. The pull-up is inside the board. Connect the button between the input and GND and use LOW to mean pressed.
Can debounce be done entirely in hardware?
Yes, with a resistor and capacitor and preferably a Schmitt trigger input. Many projects still prefer the software method because it needs no extra parts.