Interrupts, Not Loops
You'll debounce a button and run a timer without polling, then put the board to sleep and measure how little current it draws.
About
The simple loop polls everything and burns power doing nothing. Interrupts flip that around: the chip sleeps and wakes when something happens. You'll write a handler for a button, run a timer that fires on its own, and measure the board's current awake and asleep with a multimeter. Two numbers will stick with you: how many bounces one press contains, and how few microamps a sleeping chip can draw. A bare ATmega328 sleeps at under a microamp; an Uno sleeps at around 30 mA, because the USB chip, the regulator and the power LED never do.
At a glance
- Per requirement
- 25 XP
- Completion bonus
- 150 XP
- Requirements
- 5
- Takes
- One weekend
- Skill level
- Intermediate (months of practice)
- Cost
- Cheap (under $10)
- Supplies
- You need some
- Where
- At home
- Other people
- On your own
Requirements
-
1
Count the bounces in a button press with a plain interrupt. Attach an interrupt on the falling edge to a counter and print the total after each press. A cheap tactile switch gives 2 to 20 edges per press. Keep the handler tiny, mark the counter volatile, and do the printing in the loop, never inside the handler. Take a screenshot of the counts for ten presses. Photo
-
2
Debounce it in the handler using the time since the last edge. Ignore any edge within 50 ms of the previous one. Now ten presses should count ten. Take a screenshot of ten clean counts. Photo
-
3
Blink an LED from a hardware timer interrupt while the loop does something slow. Set up Timer1 (or your board's equivalent) to fire every 500 ms and toggle the LED in the interrupt. In the loop, call delay(3000) and print a line, and the blink must keep perfect time anyway. Film the LED blinking steadily while the serial monitor shows the slow loop running. Video
-
4
Measure the board's current awake and in its deepest sleep. Put the multimeter in series with the supply on the mA range and note the awake number, then use the LowPower library or avr/sleep.h to enter power-down with a pin interrupt to wake. Note the sleeping number. On an Uno it barely changes, and that's the lesson; on a Pro Mini with the power LED removed, or a bare chip, it falls to microamps. Take a photo of both readings. Photo
-
5
Build a button that wakes the board, does a job, and sleeps again. Wake on the pin, read a sensor, print it or flash the LED, and go back to sleep. Add a watchdog wake every 8 seconds as a second trigger. Film the board waking on the press with the meter in frame showing the current drop afterward. Video