0

I have a very rudimentary setup with an arduino nano, an LED strip, a pushbutton and a resistor.

I was trying to use the pushbutton as a toggle, using this code

#include <stdio.h>
#include <stdlib.h>
#include<FastLED.h> // header file

#define NUM_LEDS 14 // number of led present in your strip
#define DATA_PIN 12 // digital pin of your arduino

CRGB leds[NUM_LEDS];

const int button = 2;            // GPIO 8 for the button
int status = false;

void setup() {
  //Serial.begin(9600);
  pinMode(button, INPUT);         // define button as an input 
  pinMode(DATA_PIN, OUTPUT);           // define LED as an output
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  if (digitalRead(button)==true) { // if button is pressed
    // flip status
    //Serial.println("Changing status");
    status = !status;
    //Serial.println(status);
    toggle(status);
  }                           

  while (digitalRead(button) == true) {
    //Serial.println("while loop");
    delay(50);
    //Serial.println(status);
  }//
 
  delay(50);
} // begin again

void toggle(bool status) {
  if (status == true) {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::White;  //set the all leds to white
    }
    FastLED.show();
  } else {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB::Black;  //set the all leds off
    }
    FastLED.show();
  }
}

This is my circuit enter image description here

I am powering this from a battery (PNY model 12200, 2200 mAh, Output 5 V=1A) via USB into the arduino board. I am having some issues. Mainly, the lights would go on by themselvs after a while, maybe not full power but still would be on. Not sure where the problem is, any hints appreciated.

I also sometimes get the lights turned off, maybe I'm feeding current into the wrong place and a safety mechanism either on the arduino or the battery stick turns everything off to protect itself?

7
  • the switch data pin is floating ... its value is indeterminate ... connect the pulldown resistor to the data pin (the other side of the switch)
    – jsotola
    Dec 20 '20 at 23:52
  • use INPUT_PULLUP mode so you don't have to mess with external resistors.
    – dandavis
    Dec 21 '20 at 21:19
  • @jstola Unless I misunderstood what to do at the level of the hardware, connecting a resistor from the output of the pushbutton to D2 didn't change the behavior. Dec 21 '20 at 23:49
  • @dandavis using INPUT_PULLUP didn't change the behavior either. Dec 21 '20 at 23:50
  • while(digitalRead(button) == true);{ I doubt the semicolon there is intentional. It may help to format your code. Tools/Auto Format will do that for you. If the commented out code isn't relevant to the question you might remove it.
    – timemage
    Dec 22 '20 at 2:00
1

The manual for the T2200, which I think is what you're referring to, says:

Note: If the PowerPack does not automatically power on when a device is connected press the power button. The PowerPack will turn off after 90 seconds when there are no devices connected.

This is a common feature for USB battery banks meant for charging phones and similar devices. It probably shuts off when the current drops below some set limit, say 50mA for more than 90s. But it could also be looking every 90s for the average current over the course of 10ms. They just don't say. Given their description it sounds likely that it regularly turns on for some short time to probe for a load. All in all, not knowing the specifics of this may drive you nuts. Part of the thing to keep in mind is that someone using this thing to charge a cellphone wouldn't necessarily notice or care if it cut in and out every few seconds. If you want to use this to power an Arduino it's worth getting a good handle on how it behaves through some experimentation. Some of these power banks do have a way to disable their load detection and auto-shutoff feature; I did not see anything about that for this model.

The Nano, the official one anyway, has a 500mA PTC polyfuse on the USB VBUS ("5V"). Your code says #define NUM_LEDS 14. The WS2812b LEDs use something on the order of 50mA each at max output. You should probably measure what you're actually drawing, with the intensity you are commanding them to be, to be sure you're a good margin below 500mA. If you are in fact using 14 of them, and if it's possible that all can go full brightness, then you are perhaps trying to use something like 735mA, quite a bit above 500mA. You way find that it's necessary to route 5V around your Arduino to your LED strip, ideally through its own over-current protection.

Each time you power on or reset your project there's going to be some time before the sketch sets the pin connected to WS2812 data signal to output and some defined level. It may be a good idea to add an external pull resistor on the LED data signal, probably to GND, so that during power-up and reset the LED strip data signal is not picking up noise and trying to interpret it as color data and changing your load on your power source in the process. Having the powerbank potentially turning your project on and off gives extra opportunities for this to happen. If you trip the polyfuse, either by commanding the LEDs to draw too much or having the floating data pin do likewise, the voltage on the Nano side of it will drop. When the voltage drops it may trip the AVR microcontroller's brownout detector, which in turn will reset it the AVR, again bringing about a floating data pin on your LED strip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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