NeoPixel project - USB Stable / 9V not

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
avengerhed
 
Posts: 4
Joined: Tue Jun 24, 2014 10:15 am

NeoPixel project - USB Stable / 9V not

Post by avengerhed »

I know what you're thinking.

I'm not powering my NeoPixels with 9V.

I am, however, attempting to power my Arduino Uno with a 9V battery with some poor NeoPixel performance resulting. The entire circuit performs flawlessly when I'm powering the Ardunio off the USB port.

First, some background.
Inspired by Miria Grunick's BlinkyBox toy and looking to learn something new, I set out to build a button box for my daughter. This is my first project with NeoPixels, with an Arduino, and with coding of any kind, so be gentle, please.

The concept is built around a ring of 24 neopixels from Adafruit - the box has 3 lighted colored toggles that act as R/G/B color selectors. Flipping the toggles dictates the color of the pixels. If no toggles are turned on, the code randomly chooses a color. A latching pushbutton provides an alternate color source: when activated, a potentiometer is mapped to a modified version of Adafruit's color wheel code.

The color feeds into patterns. I have 4 patterns, modified from either the strandtest code or Miria's BlinkyBox code. 4 momentary pushbuttons each trigger an interrupt routine that simply sets a pattern variable. The loop points off to the appropriate pattern routine depending on the pattern variable.

Power
The NeoPixels as well a few LEDs on the box are powered off of 4 Eneloop AAs (4.8v). The battery pack ground is also connected to the Ardunio circuit ground. I am trying to power the Arduino from a 9V battery.

I thought the problem was related to the toggle switches I am using. The toggles are lighted. I am using 10k pulldown resistors and sensing "HIGH" condition on those pins. When the toggles are on, their internal LEDs illuminate. On my first circuit, I would experience the NeoPixel oddities after a period of time (10-60seconds) when all of my toggles were off. If any one of them was on, the NeoPixels would recover and go back to behaving. initially thought I could be causing problems by powering the toggle LEDs from the 4.8v pack so I moved the power and ground on those toggles to the Arduino 5V line. On that second circuit, the NeoPixels do not behave at all when the Ardunio is powered by a 9V battery. The NeoPixels freak out as described below after a few seconds of normal operation. Turning the toggles on or off does not cause the NeoPixels to recover. Please note - both circuits are completely stable when the Arduino is powered by the USB cable.

NeoPixel Behavior
When the performance of the device fails, the NeoPixels randomly flash all over the place. I have connected a NeoPixel circuit without a common ground between the NP power and the Ardunio and the effect is similar but it is much more substantial in this case in terms of brightness and number of pixels going haywire.

Can anyone suggest why the device is failing when the Ardunio is on battery power only? Is there any filtering I need to do or resistors I need to add? I am using a 25v 1000uF Cap across the battery pack and a 500 ohm resistor on the NeoPixel signal line. Any help would be very much appreciated.

I'll post my code below. My commenting is spotty in some places where the code isn't mine and I'm sure the code is inefficient but I will do my best to explain. Again, this code/circuit performs perfectly when the Arduino is on USB power. I had it running for more than 30 minutes tonight with no hiccups at all. On 9V power, I get 10-20s before the NeoPixels freak out (this is worse now, with circuit #2 than it was with circuit #1 (10-60s)

Thank you very much. From me and my mini-engineer!

UPDATED after further testing showed more 9V instability on circuit number 2.
Last edited by avengerhed on Thu Jun 26, 2014 9:20 am, edited 1 time in total.

avengerhed
 
Posts: 4
Joined: Tue Jun 24, 2014 10:15 am

Re: NeoPixel project - USB Stable / 9V not

Post by avengerhed »

Code: Select all

//Kylie's Button Box
#include <Adafruit_NeoPixel.h> 
#include <PinChangeInt.h>


#define PXLPIN 7                  // Set Neopixel pin

#define RED_BTN 2                 // Set Pushbutton pin numbers
#define GRN_BTN 3
#define BLU_BTN 4
#define RAINBOW 5

#define RED_TOG 8                 // Set Toggle Switch pin numbers
#define GRN_TOG 9
#define BLU_TOG 10
#define PSH_TOG 11

#define POT A1                    // Set Potentiometer pin


volatile int pattern = 0;        // Initialize interrupt variable for pattern
volatile int rainbow = 0;
volatile int chaseState = 0;
volatile int rainbowState = 0;
volatile int fadeState = 0;

int isRandom = 0;                //How to tell if a random color is being chosen
                                 //Later used to filter patterns that can't handle random colors

int REDST = 0;                   // Initialize state variables for toggles
int GRNST = 0;
int BLUST = 0;
int PSHST = 0;

int Pot = 0;                       // Initialize colors to off
int r = 0;
int g = 0;
int b = 0;

uint32_t color = (0, 0, 0);

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PXLPIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

  // Constants used for rainbows (from BlinkyBox)
  const int NUM_COLORS = 16;
  const int rainbow_r[] = {127, 127, 127, 127, 127,  64,   0,   0,   0,   0,   0,  20,  40,  83, 127, 127};
  const int rainbow_g[] = {  0,  20,  40,  83, 127, 127, 127, 127, 127,  64,   0,   0,   0,   0,   0,   0};
  const int rainbow_b[] = {  0,   0,   0,   0,   0,   0,   0,  32, 127, 127, 127, 127, 127,  83,  40,  20};

void setup() {
  strip.begin();
  strip.show();                   // Initialize all pixels to 'off'
  strip.setBrightness(35);        // Limit strip maximum brightness

  pinMode(RED_TOG, INPUT);        // Set Switch Pins to Input (addded 10k pulldown resistors)
  pinMode(GRN_TOG, INPUT);
  pinMode(BLU_TOG, INPUT);
  pinMode(PSH_TOG, INPUT_PULLUP);
  
  pinMode(RED_BTN, INPUT_PULLUP);  // Set Buttons to Input with built-in pullup resistors
  pinMode(GRN_BTN, INPUT_PULLUP);
  pinMode(BLU_BTN, INPUT_PULLUP);
  pinMode(RAINBOW, INPUT_PULLUP);
  
  PCintPort::attachInterrupt(RED_BTN, interruptRed, FALLING);  //Set interrupts for each momentary button
  PCintPort::attachInterrupt(GRN_BTN, interruptGrn, FALLING);
  PCintPort::attachInterrupt(BLU_BTN, interruptBlu, FALLING);
  PCintPort::attachInterrupt(RAINBOW, interruptRBW, FALLING);
  
}

void interruptRed(){  //Press red button, set pattern accordingly
  pattern = 0;
}

void interruptGrn(){
  pattern = 1;
}

void interruptBlu(){
  pattern = 2;
}

void interruptRBW(){ //Press rainbow button, set rainbow variable
  rainbow = 1;
}


void loop() {
  // Select Color
  color = (0,0,0);                          //zero color
  isRandom = 0;                             //zero random variable every loop
  Pot=map(analogRead(POT),0,1023,0,255);    //read potentiometer and map to 0-255 value
  if (digitalRead(PSH_TOG) == LOW){        //when the pushbutton toggle is on, use the Pot to pick a color
   if(Pot < 85) {                          //modified from Adafruit wheel routine
   r = (Pot * 3);
   g = (255 - Pot * 3);
   b = (0);
  } else if(Pot < 170) {
   Pot -= 85;
   r= (255 - Pot * 3);
   g= (0); 
   b= (Pot * 3);
  } else {
   Pot -= 170;
   r = (0);
   g = (Pot * 3);
   b = (255 - Pot * 3);
  }
    
       
  } else {                               //if the pushbutton is off
   if (digitalRead(RED_TOG) == HIGH) {   // check each toggle, if the toggle is on
     r = 254;                            //then set the corresponding color intensity value near to the max
   }
   else {
     r = 0;                              //otherwise, set the value to zero
   }
   if (digitalRead(GRN_TOG) == HIGH) {
     g = 253;
   }
   else {
     g = 0;
   }
  if (digitalRead(BLU_TOG) == HIGH) {
     b = 255;
   }
   else {
     b = 0;
   }
  }
  if ((r==g) && (g==b)){                  // if Red = Green and Green = Blue, all the switches must be off
    isRandom = 1;                        // in that case, set the isRandom indicator to 1 (yes) 
    r = random(0,255);                   // and pick out random values for the color intensities
    g = random(0,255);
    b = random(0,255);
  }
  if (rainbow == 1) {                    //if that rainbow button was pressed
    rainbowlights(5000);                 // make the rainbow pattern happen
    rainbow = 0;                         // set the rainbow indicator back to 0 (off)
  } else {                              //if the rainbow button wasnt pressed
    if (pattern == 1){                  //choose a pattern based on the value of the pattern variable
      color = strip.Color(r,g,b);       //send a color wipe using the selected, mapped, or random color values
      colorWipe(color, 50);              //Adafruit routine
    } else if (pattern == 0){    
      twinkleLights(800, r, g, b);      //modified from BlinkyBox
    } else if (pattern == 2){
      if (isRandom == 1){                //fadeLights will use a new color for every single step if random colors
        fadeLights(1200, 150, 0, 170);   // are being generated, in that case, just use this static color
      }                                  
      else fadeLights(1200, r, g, b);   
    }
  }
}

void colorWipe(uint32_t c, uint8_t wait){
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

void clearStrip() {
    int j;
    for (j=0; j<strip.numPixels(); j++) {
       strip.setPixelColor(j, 0, 0, 0); 
    }
    strip.show();
}

void twinkleLights(int wait, int r, int g, int b) {
    clearStrip();
    int i;
    int j;
    int pixel;
    const int transitions = 30; 
    const int numTwinkles = 4;
    for (j=0; j<numTwinkles; j++) {
       pixel = random(0, strip.numPixels());
       
       // Fade in
       for (i=0; i<transitions; i++) {
          float percentage = float(i)/float(transitions);
          strip.setPixelColor(pixel, r * percentage, g * percentage, b*percentage); 
          strip.show();
          delay(wait/numTwinkles/transitions);
       }
       // Fade out
       for (i=transitions; i>=0; i--) {
          float percentage = float(i)/float(transitions);
          strip.setPixelColor(pixel, r *percentage, g *percentage, b*percentage); 
          strip.show();
          delay(wait/numTwinkles/transitions);
       }
    }delay(wait/4);
}

/*
void raindropLights(int wait, int r, int g, int b) {
    clearStrip();
    int n = strip.numPixels();
    int i = chaseState;

    setIfPresent(i-6, 0, 0, 0); 
    setIfPresent(i-5, r/32, g/32, b/32); 
    setIfPresent(i-4, r/16, g/16, b/16); 
    setIfPresent(i-3, r/8, g/8, b/8); 
    setIfPresent(i-2, r/4, g/4, b/4); 
    setIfPresent(i-1, r/2, g/2, b/2);
    setIfPresent(i, r, g, b );
    strip.show();           
    delay(wait/n);

    chaseState = chaseState + 1;
    if (chaseState == n)
      chaseState = 0;
}
*/
 
void rainbowlights(int wait) {
    int i;
    int j;
    int state = rainbowState;
    for (j=0; j<10; j++) {
       for (i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i, rainbow_r[state], rainbow_g[state], rainbow_b[state]); 
          state = state + 1;
          if (state == NUM_COLORS)
             state = 0;
       }
       strip.show();  
       delay(wait/NUM_COLORS);
       rainbowState = rainbowState + 1;
       if (rainbowState == NUM_COLORS)
          rainbowState = 0;
    }
} 

void setIfPresent(int idx, int r, int g, int b) {
   // Don't write to non-existent pixels
    if (idx >= 0 && idx < strip.numPixels()) 
        strip.setPixelColor(idx, r, g, b);
}

void fadeLights(int wait, int R, int G, int B) {
    const int transitions = 30;
    int j;
   
    int brightness = 0;
    if (fadeState < transitions) {
       brightness = fadeState;
    } else {
       brightness = transitions - (fadeState-transitions);   
    }
    float percentage = float(brightness)/float(transitions);
    for (j=0; j<strip.numPixels(); j++) {
      strip.setPixelColor(j, R * percentage, G * percentage, B *percentage);     
    }
    strip.show();
    delay(wait/transitions);
    fadeState = fadeState + 1;
    if (fadeState == 60)
      fadeState = 0;   
}


/*
//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<1; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();
     
      delay(wait);
     
      for (int i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}
*/

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: NeoPixel project - USB Stable / 9V not

Post by Franklin97355 »

I am, however, attempting to power my Arduino Uno with a 9V battery
A 9v battery will not give you enough current to reliably run an Arduino for more than a few minutes. You could use 4 AA alkaline or 5 AA NiMH for a 6v input to the Arduino.

Stabitha
 
Posts: 5
Joined: Wed Jun 25, 2014 11:46 pm

Re: NeoPixel project - USB Stable / 9V not

Post by Stabitha »

A 9v battery will not give you enough current to reliably run an Arduino for more than a few minutes. You could use 4 AA alkaline or 5 AA NiMH for a 6v input to the Arduino.
Also, be careful when passing int datatypes (-32768 to +32768 when signed) to setPixelColor(), which is expecting uint8_t (0 to 255 always). If you go out of range you will see random flashes and dead spots.

avengerhed
 
Posts: 4
Joined: Tue Jun 24, 2014 10:15 am

Re: NeoPixel project - USB Stable / 9V not

Post by avengerhed »

Thank you both very much!

I'll look to change up the power source and correct the code.

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”