All tutorials

2 · Inputs & sensors

Toggle button (debounced)

Intermediate20 min

Goal

One press turns on, the next turns off — with no contact bounce.

Why it matters

A mechanical contact bounces for ~10 ms: without filtering, one press counts as ten.

Steps

  1. 01Store the previous button state (lastState).
  2. 02Detect the falling edge HIGH → LOW.
  3. 03Ignore changes happening less than 50 ms after the previous one (millis()).
  4. 04Toggle a boolean ledOn and apply it.

This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.

Next tutorials