0Pricing
Arduino & IoT Academy · Lesson

Debounce with millis()

Ignore noise with a short stability window.

Debounce with millis() is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Not Just delay()?

You could pause with delay() after a press, but that freezes your whole board. The smarter tool for clean buttons is millis().

millis() Is a Stopwatch

millis() returns how many milliseconds your board has been running. You compare two readings to measure elapsed time without stopping.

unsigned long now = millis();

The Debounce Plan

The idea is simple: when the pin changes, start a timer. Only trust the new state if it holds steady past a short window.

Pick a Debounce Window

A delay of about 50 milliseconds is the classic choice. It's long enough to outlast bounce, yet far too fast for you to notice.

const unsigned long DEBOUNCE = 50;

Remember the Last Change

You need a variable to store when the pin last flipped. Compare it to the current time on every loop pass.

unsigned long lastChange = 0;

Track Two States

Keep the raw reading and the confirmed stable state separately. The raw one is noisy; the stable one is what your logic trusts.

int lastRaw = HIGH;
int stable = HIGH;

Detect a Flip

Each loop, read the pin. If it differs from the last raw value, the signal just moved, so reset your timer.

int raw = digitalRead(BTN);
if (raw != lastRaw)
  lastChange = millis();

Wait for Stillness

If enough time has passed since the last flip, the reading has settled. Now it is safe to accept it as the real state.

if (millis() - lastChange > DEBOUNCE) {
  stable = raw;
}

Act on the Edge

To fire once per press, watch for the stable state changing to LOW. That single edge is your clean, debounced press.

if (stable == LOW && lastStable == HIGH)
  Serial.println("clean press");

Update for Next Time

At the loop's end, save the current readings into your last variables. This keeps the comparison fresh on every pass.

lastRaw = raw;
lastStable = stable;

Non-Blocking Wins

Because millis() never pauses the board, your sensors and outputs keep running. You debounce while everything else stays alive. 🚀

Quick Check

Let's check how millis() debouncing decides a press is real.

Recap: Clean Presses

You used millis() to ignore bounce without freezing the board, accepting a press only once it settles. Solid, professional input! ✅

Frequently asked questions

Is the “Debounce with millis()” lesson free?

Yes — the full text of “Debounce with millis()” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Debounce with millis()”?

Ignore noise with a short stability window. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Debounce with millis()” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Buttons 'Bounce'
  2. Debounce with millis()
  3. Store a Value in EEPROM
  4. Remember a Counter After Reset
← Back to Arduino & IoT Academy