Delay() not working when running program on battery power

Hello everyone!
I am using Arduino UNO to execute a simple program that turns the first LED on an LED strip to red, then waits for 1s and turns the second LED to green. This seems to work when the Arduino is powered from the USB, but when I power it from a 5V battery, only the first LED glows red. It looks like the delay() call is permanently blocking execution when using a battery. I played around with the delay value, and the issue seems to be happening when the value is >= 9 ms

Below is my code -

#include <FastLED.h>

#define NUM_LEDS 10
#define LED_PIN 2

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);
}

void loop() {
  leds[0] = CRGB::Red;
  FastLED.show();
  delay(1000); 
  leds[1] = CRGB::Green;
  FastLED.show();
}

Any idea why the delay() is working when the Arduino microcontroller is powered from a USB, but not from a battery? Are there any workarounds or steps to resolve this issue?

Thanks,
Dhruv

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.

In that case, it is probably time to blame the battery - which you don't describe very well.

There are no "5V batteries", so what are you actually using, and how did you connect it? Post a wiring diagram.

I am using the battery that came with the Arduino kit. Looks like it is a PowerfulCell 1604D 6F22 9V battery.

These are pretty useless for most Arduino projects.

image

It will not be able to supply sufficient current to drive the LEDs.

I see. What is the recommended battery to use for Arduino projects?

6 (six) AA batteries will give you 9V for power input on the power jack on the Arduino.

image

A 5V USB phone charger will work well, if you cut the USB cable and apply 5V to the Arduino "5V" pin, GND to GND.

Do you need to do that? Couldn't you just plug into the USB(B) port of the Uno from the charger?

Sure, if the phone charger has a USB type B plug. Mine certainly don't.

Cool... I was thinking of this type of charger, then just a USB A-B cable.
image

Yes, just plug in a so called printer cable.

image

I would expect the LED to be red over 99% of the time since there is only a few microseconds between setting the LED to green and setting it to red again. About once a second you may see a slight flicker.

Try this:

void loop() 
{
  leds[0] = CRGB::Red;
  FastLED.show();
  delay(1000);

  leds[1] = CRGB::Green;
  FastLED.show();
  delay(1000);
}

That should give you about one second of red alternating with about one second of green instead of about one second of red alternating with a few microseconds of green.

missing second delay, so no idea why this works “correctly” on USB power

A second delay in this case is not needed as the LEDs are not being toggled, i.e. they are just being turned ON, green 1 second after red.

But we need more information. :woozy_face:

good eye

Thanks for all the responses. Yes, the original code was trying to light up 10 LEDs with a 1s delay after each LED being lit. This worked fine with USB power, but with battery power only the first LED was glowing. So I was trying to isolate the problem, and it came down to the use of the delay() function. BTW, I changed the battery to a brand new 9V Duracell, and it worked fine.

Well, that's a new one.