So I made a standalone circuit for a night light that fades through the visible spectrum with an RGB LED.
When I plug in the power leads into the 3.3 or 5V and ground on my arduino uno, it works perfectly.
I want to power it from three 1.5V button cells for a total of 4.5V. When I attach the power leads to the batteries, the light flickers, when changing from red to green (mostly in the yellow area), and it is a lot dimmer (even when compared to 3.3V in arduino).
I installed a 0.1uF capacitor across the 4.5V and ground power leads, it didn’t seem to make a difference.
Any thoughts or ideas how to make this work? Would it be better to use a 12V battery and regulate the voltage down to 5V?
I have attached a circuit diagram (except it is just a chip, no arduino, same circuit) and code in case it is useful.
#define GREEN 3
#define BLUE 5
#define RED 6
#define delayTime 120
int ldr = 1; //Define photosensor in Analog port 1
int lightinput = 0; //Default value for lightinput is zero
int trigger = 350; //Threshold value of light to trigger activation.
//I have found between 200 and 600 works best. You don't want it so
//sensitive that it detects itself and flickers at the end of it's
//cycle.
void setup() {
pinMode(GREEN, OUTPUT); //define as digital outputs
pinMode(BLUE, OUTPUT);
pinMode(RED, OUTPUT);
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs
}
int redVal;
int blueVal;
int greenVal;
void loop() {
lightinput = analogRead(ldr); //difine lightinput as value from photosensor
if (lightinput < trigger) //compare value to threshold trigger
{ //open if statement (if DARK)
int redVal = 255;
int blueVal = 0;
int greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
greenVal += 1;
redVal -= 1;
analogWrite( GREEN, 255 - greenVal );
analogWrite( RED, 255 - redVal );
delay( delayTime );
}
redVal = 0;
blueVal = 0;
greenVal = 255;
for( int i = 0 ; i < 255 ; i += 1 ){
blueVal += 1;
greenVal -= 1;
analogWrite( BLUE, 255 - blueVal );
analogWrite( GREEN, 255 - greenVal );
delay( delayTime );
}
redVal = 0;
blueVal = 255;
greenVal = 0;
for( int i = 0 ; i < 255 ; i += 1 ){
redVal += 1;
blueVal -= 1;
analogWrite( RED, 255 - redVal );
analogWrite( BLUE, 255 - blueVal );
delay( delayTime );
}
} //close if statement
else
{ //open else (if LIGHT)
digitalWrite(GREEN, HIGH); //I am using a common ANODE LED so a value
digitalWrite(BLUE, HIGH); //of HIGH is off, and LOW is full brightness.
digitalWrite(RED, HIGH); //Opposite for common CATHODE LEDs
delay(12000); //This code is supposed to save energy by only detecting
//light every two minutes as opposed to constantly, however it does not
//work.
} //close else
} //close void loop