My sketch works when attached to the computer but not when attached to battery

I have a small app that works just fine when the Uno R3 board is attached to my Windows computer with the USB cable. However, if I remove the USB cable and power the board with a 9V battery then the program will not work. When the board is attached to the computer the "L" led, near pin D13, is on and is yellow. When the 9V battery is attached the led is off. In both cases, the green led near pin D2 is on.

I suspect the "L" led being on and off may be the key, if I only knew what that key is.

Any thoughts?

Thanks in advance.

I suspect you code is setting there waiting for the connection to your PC to be established. A classic coding failure.

Do you have a line of code that is waiting for Serial to be connected ?

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Using a "smoke detector" 9v battery with Arduino is not recommended because it extremely weak capacity. Depending of the circuit current consumption the battery may emptied in less than 15-20 minutes.

I did have some lines like

Serial.println("AAA");

for debugging but those have all been commented out to include the Serial.begin.

There is not much code left after commenting out a bunch of code. However, it is now pretty ugly looking with all of the comments.

I will get the code more readable shortly.

I am using an IR receiver and IR transmitter and am using the IRremote library.

What is strange is that the sketch was working with the battery yesterday but not when I started it up this morning.

Can anyone tell me more about the "L" led?

If you've already used this battery, it may not have enough charge to even start the Arduino.

I am using the battery that came with my Elegoo kit. Let me see if I can find another battery to try.

The IR transmitter consumes significant current. Most likely your battery is simply not strong enough to operate the transmitter.

Try to use a pack of 6x AA batteries or any DC 5v mobile charger (USB connection)

In all my years of working with Arduino, I've never paid much attention to this LED. It's unlikely to be relied upon as a diagnostic tool; the problem is usually obvious from other factors.

Yeah, that's attached to pin 13 on the UNO R3, and is controlled by your code. Maybe you commented out some statement like
pinMode(13, OUTPUT);
or,
digitalWrite(13, HIGH);

because you had no clue?

Seriously, on this forum, not showing your code is close to a capital offence when asking a question like yours.

I think that the battery is the issue. I plugged the Arduino USB cable into the adapter that I use to charge my phone and everything works fine.

1 Like

Those PP3 9V batteries are really not practical power source, the capacity is only ~500mAh.
You could use 5-6 AA batteries instead, or power bank.

Try 6 AA Alkaline. Connected in series.

I'm just making a guess and will have to do a little more testing. However, it looks like that LED could be an indicator of power level available to the board. I suspect that if I get a fresh battery then the led will start to light up.

I use the "L" LED as a heartbeat for the sketch. If my "L" stops toggling on... off... on... off.. et c. I know my code has severely slowed or stopped.

Yes, a "power only" cable will let you know if the power supply (battery here) is insufficient.

1 Like

Just an update.
I ordered a 6 AA battery holder to take the place of the 9V battery. Pretty cheap on Amazon.
I put all of the serial print coded back into my sketch and it does not cause an issue with the code running while not connected to the computer. The code runs fine when attached to my phone charger adapter.
There is nothing in my code that does anything with pin 13 and the "L" led. By default it looks like the on/off state of the led is an indicator of the battery power output.

So the issue resolved completely?

I am 99.52% convinced that my new battery pack will solve the issue. I think that I may have been taking into account that TV remote controls can go months without changing batteries. I just thought that the 9V battery would run for months without issue. Obviously, a bad assumption.

Yes, there is.

Not, it does not.

There are no pinmode or digitalwrite commands. Are there other ways to control the led?

I will know better about the led reflecting battery state once I get a new battery.