Battery life running Arduino Uno using 9v battery

Hey guys,

I realise this is an impossible question to answer but I was hoping you might have some sort of idea of how long I might expect 9v battery to last when running the code below?

I am using an ultrasonic sensor to trigger LEDs at various intervals of distance from the sensor, as a result the sensor is constantly being used.

Please excuse how messy the code is… I am new to this.

But if anyone has any idea, however rough that may be, of how long a 9v battery may last, I would be tremendously grateful.

Thanks in advance!

int ledPin = 10;
int greenLed = 13;
int buzzerPin = A0;
int yellowLed = 11;
int redLed = 8;
int newLed = 12;
int greenLed2 = 9;
long duration, cm;

void setup() {
pinMode(2, OUTPUT); // VCC
pinMode(3, OUTPUT); // trig pin
pinMode(4, INPUT); // echo pin
pinMode(5, OUTPUT); // GND
pinMode(buzzerPin, OUTPUT); // set the siren pin as output
pinMode(ledPin, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(newLed, OUTPUT);
pinMode(greenLed2, OUTPUT);
pinMode(yellowLed, OUTPUT);

pinMode(greenLed, OUTPUT); // set the siren pin as output

digitalWrite(2, LOW); // safety feature: connect GND then VCC
digitalWrite(5, LOW); // safety feature: connect GND then VCC
delay(500); // safety feature: connect GND then VCC
digitalWrite(2, HIGH); // safety feature: connect GND then VCC
Serial.begin(9600); // for display distance

}

void loop()
{
//code mesures time for the sound to bounce back from an object*
digitalWrite(3, HIGH); // turn on trigger
delayMicroseconds(5);
digitalWrite(3, LOW); //turn trigger off
duration = pulseIn(4, HIGH); // measure time for sound wave to come back
cm = duration / 58;

if (cm < 4) {
digitalWrite(buzzerPin, HIGH);
delay(200);
}

else if (cm < 10) {
digitalWrite(ledPin, HIGH);
delay(200);

}

else if (cm < 6) {
digitalWrite(ledPin, LOW);
delay(200);
}

else if (cm < 20) {
digitalWrite(greenLed, HIGH);
delay(200);
}

else if (cm < 30) {
digitalWrite(yellowLed, HIGH);
delay(200);

}

else if (cm < 40) {
digitalWrite(redLed, HIGH);
delay(200);

}

else if (cm < 50) {
digitalWrite(greenLed2, HIGH);
delay(200);

}

else if (cm < 60) {
digitalWrite(newLed, HIGH);
delay(200);

}

else if (cm > 3000) {
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(greenLed2, LOW);
digitalWrite(newLed, LOW);
delay(10);
}

else {
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
digitalWrite(greenLed, LOW);
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
digitalWrite(greenLed2, LOW);
digitalWrite(newLed, LOW);
}

if (cm > 80 || cm<4){ // Range: 3-100 cm
Serial.println(“N/A”);
}
else{
Serial.println(cm);

} }

I'm not going to guess... But the rumor is, you can't run an Arduino from a 9V because it doesn't last very long.

I will guess that the answer is in hours, not days. So, it shouldn't take you too long to run the experiment yourself.

The good news is that it can discharge down to about 5V (beyond it's normal life) and the Arduino will still run.

You mention Leds in your code. Every led takes some current and your sensor also. You can find some info about batteries for instance here. http://www.digikey.fi/product-detail/en/energizer-battery-company/EN22/N145-ND/704825 Let's say your Arduino system takes 50mA, then your battery will be flat after about 10 hours.

a rectangular 9v PP3 smoke alarm battery is rated for about 300 mAh, which means it would power a circuit drawing 50 mA for 6 hours.

willpardoe: Please excuse how messy the code is... I am new to this.

We are not quite as concerned about how messy the code is - though that has too much unnecessary white space for my liking - as how you post it.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can examine it conveniently and accurately.

If you do not do this, the code you post could well be garbled and is certainly anything but easy to read.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

And - before you post code, use "Auto Format" in the Tools menu to properly present the code.