I am making an rgb led colour mixer, which utilises a 10k potentiometer and 4 rgb leds. The rgb leds are common cathode, and thus have r,g,b pins along with a GND pin. I know that the arduino itself cannot power 4 leds as there is not enough amperage (apologies if i sprout garbage out my mouth, I am still very new to this), so I have got my hands on a 4×AA battery pack and some 548 NPN transistors. I have built the project on a breadboard and an Arduino UNO. It works fine but I am worried that I have missed something as this is my first project (ever). Will this burn out the arduino/leds somehow? Thanks.
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int potPin = A0;
int val = 0;
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
//Pot reader
val = analogRead(potPin);
Serial.println(val);
//Red orange yellow green
if ( val <= 341 )
{
//We now know the val is between 0 and 341.
//Map the val to 0-255 for the LED.
val = map(val, 0, 341, 0, 255);
//Higher val turns red down.
//Lower val turns red up (common anode).
analogWrite(redPin, val);
//Write the complimentary amount to the green pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(greenPin, 255-val);
//Turn blue pin off.
analogWrite(bluePin, 255);
}
//Green-teal-blue spectrum
else if ( val <= 683 )
{
//We now know the val is between 341 and 683.
//Map the val to 0-255 for the LED.
val = map(val, 341, 683, 0, 255);
//Higher val turns green down.
//Lower val turns green up (common anode).
analogWrite(greenPin, val);
//Write the complimentary amount to the blue pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(bluePin, 255-val);
//Turn red pin off.
analogWrite(redPin, 255);
}
//Blue-indigo-purple-red spectrum.
else
{
//We now know the val is between 683 and 1023.
//Map the val to 0-255 for the LED.
val = map(val, 683, 1023, 0, 255);
//Higher val turns blue down.
//Lower val turns blue up (common anode).
analogWrite(bluePin, val);
//Write the complimentary amount to the red pin.
//The mixed color values add up to 255.
//This keeps the LED at constant brightness.
analogWrite(redPin, 255-val);
//Turn green pin off.
analogWrite(greenPin, 255);
}
//Sampling rate.
delay(20);
}
Sorry, forgot to mention. The leds already have 100 ohm resistors for each rgb pin. Forgot to mention as they are mounted on a pcb (off ebay!). I'm more worried about the arduino though. The battery pack will be replaced by a proper 5V DC power source asap.
So it's fine if I have a constant 5v DC power source replacing the batteries though right? I accidently blew up an led today when I was tired so I'm super paranoid now!
Mike, i am very reluctant to question what you say, but i have been told that when a npn is used as an emitter-follower like that, there is no reason to have a resistor on the base, the current regulates itself. Can you enlighten us please?
It is the case that using an emitter follower, you must not insert base resistors.
Grumpy_Mike:
The main problem is that Fritzing is so god dammed awful it is impossible to see what is happening.
Could be worse.
Grumpy_Mike:
He should not be using the transistor in this mode anyway.
Actually, while there are the problems with sufficient voltage that I mentioned, using any other mode would make things a lot more complex. You cannot use a PNP transistor here because it would be held ON by the difference between the 6V LED supply and the 5V Arduino supply. You would then have to use an NPN transistor - and more base resistors - to drive it.
You have a 5V swing on the Arduino output and 6V on the PNP's emitter. So you have a pull up from the base to the 6V rail. Then a resistor from base to the Arduino, and a clamping diode from output pin to rail. When the Arduino output is high there is 1V between the output and the emitter. If the potential divider between the two resistors results in a voltage of only say 0.3V between emitter and base of the PNP then the transistor will be off. Only when the Arduino output goes low will the PNP turn on.
Yes, I certainly did know that, but it was missing from your previous description, wasn't it? So now you are adding more components again and "get the resistors right" was just a little misleading, wasn't it?
OK. Use a pulldown resistor as well and you can remove the clamping diode. So that is just three resistors, which is "getting your resistors right".
I am sure you can work that one out.