Multiple rgb leds controlled by transistors

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.

Breadboard:
http://puu.sh/l8INg/e9354ab63b.png

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);
}

chairr: Will this burn out the arduino/leds somehow? Thanks.

|500x295 Might do! :roll_eyes:

No current limiting resistors in each anode of each individual LED.

Balanced against that is that you lose almost a volt in the emitter followers, so you are only feeding about 4.3 V to the LEDs.

OK, just add the 12 resistors. Try 150 ohms for the reds, 100 Ohms for the other two colours.

Note that the Arduino may be overloaded if the batteries go flat.

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.

As long as the power feed to the emitter followers cannot be turned off while the Arduino is powered up, it will be fine.

If the 5V is regulated, you can feed it to VCC on the Arduino.

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!

I try to avoid repeating myself. :grinning:

You need a 1K resistors in line with each transistor base.

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?

when a npn is used as an emitter-follower like that, there is no reason to have a resistor on the base,

Yes of course that is true, sorry.

The main problem is that Fritzing is so god dammed awful it is impossible to see what is happening.

He should not be using the transistor in this mode anyway.

I suspect he mistook something. :grinning:

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 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.

No, get the resistors right and you won't.

Grumpy_Mike: He should not be using the transistor in this mode anyway.

What do you mean I should not be using them this way? Am I suppose to put the leds somewhere else? What are the risks?

Grumpy_Mike: No, get the resistors right and you won't.

Really?

Care to explain?

Paul__B:
Really?

Yes really.

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.

But you knew that didn’t you.

But you now mention a clamping diode.

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? :grinning:

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.