How to deep sleep ATtiny/ATmega (to run for years with batteries) but still detect a button press?

ATTINY's are designed to easily do this since they can wake from power down sleep mode by a pin change...

enter image description here

Basically you want to...

  1. Connect a normally-open push button between an IO pin and ground.
  2. Enable the pull-up on the IO.
  3. Enable the pin change interrupt on the IO pin.
  4. Enable interrupts.
  5. Enter "power down" sleep mode.

When the button is pushed, it will connect the IO pin to ground, which will cause a pin change interrupt, which will wake the chip from sleep.

Note that you do not need to do anything in the pin change ISR (you can use the EMPTY_INTERRUPT compiler macro). If the ISR is empty, execution will pick up on the line after the sleep upon waking.

While in deep sleep, the chip will use an astonishingly small amount of power. For example, an ATTINY25/45/85 running off of a 3V coin cell at room temp will use less than 0.2uA...

enter image description here

A typical CR2032 coin cell is rated for 235mAh to 2V, so at this rate we'd expect to be able to wait for a button press like this for 100+ years.

IRL the battery will likely self discharge and physically deteriorate long before then (rated at 1% per year), but still not too shabby.

enter image description here