Unexpected behavior using digital pins to control an RGB LED

Bought my first arduino board two days ago and have just been playing around with LED's etc, I've built a 3x3 cube and have had no problem programming it to display an array of patterns etc.

Today I got some 3mm RGB LED's. I have it plugged into a breadboard with the common cathode plugged into a 10k Ohm resister, and then into the 5v on the arudino board.
Each of the three annodes is plugged into digital pins 8,9 and 10.

This is the code I'm using:

int red = 8;
int blue = 9;
int green = 10;
int time = 500;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT); 
 
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH); 
}


void loop() {

  digitalWrite(red, LOW);     //Red
  delay(time);   
  digitalWrite(red, HIGH); 
  delay(time); 
  digitalWrite(green, LOW);   //Green
  delay(time);
  digitalWrite(green, HIGH);
  delay(time);
  digitalWrite(blue, LOW);    //Blue
  delay(time);
  digitalWrite(blue, HIGH);
  delay(time);
  
  digitalWrite(red, LOW);    //Red & Green
  digitalWrite(green, LOW);
  delay(time);
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
  delay(time);
  
  digitalWrite(green, HIGH);  //Green & Blue
  digitalWrite(blue, HIGH);
  delay(time);
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
  delay(time);
  
  digitalWrite(red, LOW);   //Red & Blue
  digitalWrite(blue, LOW);
  delay(time);
  digitalWrite(red, HIGH);
  digitalWrite(blue, HIGH);
  delay(time);
  
}

With the above code I was trying to flash on then off each color on separately, then start mixing to colors two at a time.

When the program runs, turning on each colour one at a time works as expected, but things start turning weird when I try to use two colours.

This is what I see happening:

red on
pause
red off
pause

blue on
pause
blue off
pause

green on
pause
green off
pause
(where it gets weird)
red on
red off
blue on
blue off
pause
green on
green off
pause

This red bit then continually repeats.

the common cathode plugged into a 10k Ohm resister, and then into the 5v on the arudino board.

You can't do that. Each anode requires its own resistor. That is why you are seeing your "problems"

Cheers grumpy, I'll give that a try, I did it the way I have now as I only had two 10k resisters left, I'll go pick some more up tomorrow :stuck_out_tongue:

I only had two 10k resisters left, I'll go pick some more up tomorrow

Grumpy Mike said each anode needs its own resistor. He did not say that each anode needs its own 10K resistor. 10K is very high for an LED resistor. Typically, something around 220 is more appropriate. You need to look at the specs for your LEDs and use Ohms law to determine the appropriate sized resistor. Don't just grab something handy.

Grumpy_Mike:

the common cathode plugged into a 10k Ohm resister, and then into the 5v on the arudino board.

You can't do that. Each anode requires its own resistor. That is why you are seeing your "problems"

Also having the cathode plugged into 5V won't help. XD

Also having the cathode plugged into 5V won't help.

Sure it will. Turning the anode pins off then allows current to flow, causing the light to go on. Turning the anode pin on changes the potential difference to 0, so no current flows, and the light goes off.

PaulS:

Also having the cathode plugged into 5V won't help.

Sure it will. Turning the anode pins off then allows current to flow, causing the light to go on. Turning the anode pin on changes the potential difference to 0, so no current flows, and the light goes off.

Silly me getting my anodes mixed up with my cathodes :blush:

EDIT:
I take that back I was not getting mixed up. From what you say 'the anode pins off then allows current to flow' but diodes prevent flow from cathode to anode (well mine never work well when plugged in the wrong way round :relaxed:)

You're quite right - you can either
connect the anode to +5V and the cathode to an output pin (via a current-limiting resistor), so writing the pin LOW will light the LED,
or
you can connect the anode to the output pin and the cathode (again, with a current-limiter) to 0V, so writing a HIGH will light the LED.

I was following through an arudino tutorial (Arduino Tutorial - Lesson 3 - Breadboards and LEDs).

I missread this as 10k :stuck_out_tongue: "Always use a resistor! A 1.0K ohm is a good place to start. We'll cover how to figure out the best resistor value later on."

Thanks for the help guys.

Always use a resistor! A 1.0K ohm is a good place to start.

I would say that between 330R and 512R is a good place to start.

Founds the spec sheet, I work it out to be 150 ohm for the red, and 90 ohm's each for the blue/green. (5v power source, 20mA draw and 2.00v drop for Red, 3.0v for green/blue).

Also something interesting I spotted on the spec sheet, there listed as common Annode. From the way I have them wired up I would think they are the opposite?

Edit: Tested all four LED's I bought, 2 where common cathode, and two common anode, the website had them listed as common anode. Doesn't bother me, but I'll probably send the website an email in-case they send the wrong LED's to someone that actually needs them on way or the other.

Edit: Tested all four LED's I bought, 2 where common cathode, and two common anode, the website had them listed as common anode. Doesn't bother me, but I'll probably send the website an email in-case they send the wrong LED's to someone that actually needs them on way or the other.

The last batch of RGB LED's I bought via eBay had a few common cathodes mixed in with my common anode order. :frowning: