3 color LED (super newb question)

So I put together my first arduino board and started making a couple simple projects (blinky light, etc). I purchased a 3 color LED but things seem to be backwards in my mind.

the LED looks like

if I hook up a circuit like

with A grounded and C and D not then the LED shines red (C is blue and D is green). I was thinking I could hook up A,C, and D up to D7-9 and then switch the colors by bringing the pin high. But alas it does not work. I know why I have the current flowing the wrong way. Is there a way to sink (I think that is the terminology) instead of sourcing the power?

int redPin = 7; 
int bluePin=8;
int greenPin = 9;

void setup()
{
  pinMode(redPin, OUTPUT);      // sets the digital pin as output
  pinMode(bluePin, OUTPUT);      // sets the digital pin as output
  pinMode(greePin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(redPin, HIGH);
  digitalWrite(bluePin, LOW);
  pinMode(grenPin, LOW);  

  delay(1000);                  
  digitalWrite(redPin, LOW);    
  digitalWrite(bluePin, HIGH);
  delay(1000);                  
}

Sorry for such a basic question but I can't find the answer anywhere. I can get it to work with 3 transistors but I would prefer to keep the design simple.

Haven't tried with Arduino, but a pin at the low state will usually sink. So just put the common Vcc to the Arduino's Vcc, grounds to pins and put pins to the LOW state to make the current flow. Totally non instinctive, but should work.

The last time I've used a RGB led, it was supposed to be a common cathode model, but proved out to be common anode. So they do exist, too.

About your resistor, make sure that all leds use same current/amperage. In the models I used, all three leds had different specs.

Yes remove the resistor from the Vcc side and put B direct to Vcc. The put a resistor in each of the lines A C & D and the other end to the Arduino outputs, don't connect anything to ground the Arduino does that for you when you set the output line to low. In that way you can have a different resistor in each of the LEDs and balance the brightness of each colour.

The way you had it won't work because once one LED is on there will be insufficient voltage across the other LEDs to turn them on.

The way you had it won't work because once one LED is on there will be insufficient voltage across the other LEDs to turn them on.

The way you have it with one resistor, you have the 3 LEDs in parallel, so the voltage across them has to be the same (Kirchov's Voltage Law), but they're semiconductors which want to drop different voltages, so what will happen is the lowest voltage one (Red) will drop the voltage below the Vf for the other 2 colours. In other words, if you try to light more than one colour, on the red one will light.

It is a very bad idea to ever put LEDs in parallel without separate resistors because even if they're from the same batch they all have slightly different Vf's because of the realities of the manufacturing process. So the ones with the slightly lower Vfs will take more current from the others. If there's a lot of LEDs, it could easily take the LED over it's current limit so it will dim and burn out over time and either short out everything or pass the current to the other LEDs which will then burn out, etc.

Here's some arduino code with pwm for color mixing that assumes that you have B to VCC (No resistors) And a,c,d with the appropriate resistors to pwm ports

int red = 0, green = 0, blue = 0;
void loop() // will just go through various brightnesses of white (RGB) adjust variables accordingly
{
     analogWrite(redPin, 255 - red); // Every time the PWM turns off the
     analogWrite(bluePin, 255 - blue); // arduino sinks current so when its
      analogWrite(greenPin, 255 - green); //set to 255 no current is sunk so the color turns off
     red++;
     green++;
     blue++;
}

Unless I my understanding of analogWrite() is wrong, I don’t think this code will work completely right. In its current state, red, green, and blue will go from 0 to 255 before a single PWM period has elapsed and you won’t see any fading at all. You should add a delay to loop to control how quickly the PWM duty cycle changes. Also, it might be good to catch when the color variables reach 256 and either reset to them to zero or maybe start them counting back down to zero.

  • Ben

Unless I my understanding of analogWrite() is wrong, I don't think this code will work completely right. In its current state, red, green, and blue will go from 0 to 255 before a single PWM period has elapsed and you won't see any fading at all. You should add a delay to loop to control how quickly the PWM duty cycle changes. Also, it might be good to catch when the color variables reach 256 and either reset to them to zero or maybe start them counting back down to zero.

  • Ben

oops, forgot about the delay ;D yeah, but it was just meant to be an example, to show how PWM could be used with a common anode RGB led in a real skech there would be checks and proper delays