Scenario
I have created a nice electronic door lock for my dorm room. It is currently an Arduino Diecimila with a servo [un]locking the door. It has a numerical keypad with 3×4 buttons and 5 LED's (2 serie pairs and one single LED). It also currently runs on a cellphone charger.
I have now redesigned it to run on a standalone Arduino (ATmega328), but would really like to have it run on AA batteries or even a 9V battery.
For the software part, I figured I could put sleep
calls for certain times inside the loop method to keep the ATmega power consumption as low as possible. And let the LED's "flash" with as long as possible time off.
Question 1
When a button is pressed during the few milliseconds which the board sleeps, will it be "remembered"/"held" until it comes out of sleep and then be picked up as a button press?
What would be the best way to handle this button press on sleep? Can I code it to wake up upon button activity, or must I just let it sleep for e.g. 10m.s. in every loop?
Question 2
How would I approach the math of calculating how many AA batteries are needed to run this device for 10 months?
Also, I don't know how to measure the average power usage per minute or so, since it alternates quickly etc.
Best Answer
The Atmega328 provides six power saving modes, ordered from minimal to excellent (estimated current consumptions from this forum post):
Quoting the original question:
You would need to use
sleep_cpu()
after setting up the sleep mode you require, from the list above. The Arduino Playground has a useful post about this.The application needs to be interrupt driven, use the above sleep modes extensively, and wake the processor up on button push, timer overflow and watchdog timer events to actually execute tasks.
Additional power savings can be obtained through the following steps:
Incorporating these suggestions allows for running microcontroller applications for weeks or months on a single CR2032 coin cell, and years on a LR123 type lithium cell. Of course, your mileage may vary depending on what sensors, outputs and actual processing your application requires.
Some useful references: