I'm making a basic "door open" detector that, when a door is opened, it turns on an LED and then sounds an alarm via a piezo.

This is controlled via a 5v Adafruit Trinket.

Is a 9v battery the correct thing to power this with, if the goal is to change batteries as infrequently as possible?

enter image description here

Here's the code, incase that's relevant.

#include <elapsedMillis.h>
elapsedMillis timeElapsed;

const int switchPin = 2;
const int ledPin = 1;
const int piezoPin = 0;
const uint32_t openLength = 5 * 1000L; // X seconds
int notes[] = {862,294,730};

void setup() {
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(piezoPin, OUTPUT);
  digitalWrite(switchPin, HIGH);

void loop() {
  if(digitalRead(switchPin) == LOW){    
    digitalWrite(ledPin, LOW);
    timeElapsed = 0;
  } else {
    digitalWrite(ledPin, HIGH);

    if (timeElapsed > openLength){
      for (int n = 0; n < 200 ; n++){

        if(digitalRead(switchPin) == LOW){ break; }

void beep(int delayAmount)
  for (uint16_t t = 0; t < 30*1000/2; t += delayAmount)

UPDATE: I had a 9V plugged in to this for about 3 days before it used all the power in 9V. Is that an issue w/ voltage on the battery (i.e. I should be using AA's instead of a 9V)? Or does that point largely to some other inefficiency w/ the code or hardware?

  • Not the problem, but your notes[random(0,2)] will only be 0 or 1 (2 is never reached). But you have 3 values in your array.
    – aaa
    Commented May 3, 2016 at 7:05
  • 1
    Also, a quick/dirty fix could be to put the switch between the battery and the microcontroller (if it can handle the current/voltage). Then your microcontroller will only power up when the switch is connected ;). This way you won't drain anything when the switch is off!
    – aaa
    Commented May 3, 2016 at 7:08
  • @Paul Bah! Of course! It delays the reaction time a few seconds, since the micro has to boot, but not a big deal in this case. Genius!
    – Shpigford
    Commented May 3, 2016 at 10:40
  • If you use an Arduino withouth bootloader (or modify it) you can get an extremely fast startup time. Microcontrollers won't need to set up an OS (Or well, the bootloader waits for code to be uploaded).
    – aaa
    Commented May 3, 2016 at 14:26
  • @Paul Actually I just realized that what you mentioned won't work for my use case. It's a magnetic contact switch and I need it to boot when the magnets disconnect...which I believe can only be detected via software. I could be wrong, though: adafruit.com/products/375
    – Shpigford
    Commented May 3, 2016 at 21:16

2 Answers 2


A couple things to note:

  • Get rid of the PWR LED on the Trinket so it's not always drawing current.
  • Sleep the MCU whenever possible, only waking on interrupts.
  • Converting 9V to 5V with an LDO (in this case, the MIC5225) implies a 55% efficiency, max, which isn't great. But at least the LDO has a low ground current.

Also, ask yourself if you really need a micro for this task.

If you want a ballpark for runtime, find the capacity of your battery, estimate your current draw, and see what your upper bound on runtime is.

  • Added an update w/ more info...wondering if I have a battery issue or a hardware/software issue.
    – Shpigford
    Commented May 2, 2016 at 21:48
  • @Shpigford With regards to your update, it's both. 9V is not an ideal battery (and a few AAs have a lot more capacity), and you could be sleeping almost all the time. Again, ask yourself if you really need a micro to accomplish this task.
    – uint128_t
    Commented May 2, 2016 at 21:54

You can power the Trinket (ATTiny85) from as little as 2.7 volt. So you could power it by connecting 2 (or 3) AA-batteries directly to the 5V pin.

Using a 9V battery means you would "throw away" 4 of the 9 Volts to get 5Volt. So you'd waste almost half of the capacity of the battery.

Also a 9V battery only has around 500mAh, where as an AA battery has 2500mAh.

  • Added an update w/ more info...wondering if I have a battery issue or a hardware/software issue.
    – Shpigford
    Commented May 2, 2016 at 21:48
  • Like i said, AA have 5 times the capacity. So if you now get 3 days with a 9v battery, AA would give you only 15 days.
    – Gerben
    Commented May 4, 2016 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.