RGB LED common anode control

Hi guys,

I'm an absolute novice at using microcontrollers, so bear with me if my question has a simple answer! I am trying to setup a basic circuit with an RGB LED, and am aiming to have it fade from one colour to the next, and keep cycling through using different strengths of each colour. The problem is that the RGB LED that I have utilises a common anode (common positive, in case I got that mixed up haha). I am trying to use PWM to control the (apparent) pin output voltage. My question is, how can I connect the LED in order to be able to control each colour individually (using PWM if possible)? I have an inkling that I may need to use transistors or something instead? I also thought about connecting the PWM pins as a reverse voltage against the 5V pin, but I figure this would just damage the board, rather than actually providing me with backwards control.

Cheers for any help or links to information if you can't be bothered typing it out!

Alex

Your common anode goes to +5, then have a resistor (220?) between each cathode and a PWM outpin. With that arrangement, an LED is on when the output is "0"; when PWM=255 it's off and when PWM=0 it's full on.
The outputs' states aren't +5 and "5V open circuit" - they're switching between +5 and ground.

** Here's my clunky little programme that runs some levels, simple. **

// anode to +5, cathode thru 220? to "pin11"
// "1" = off, "0" = on
// PWM=255=off(dimmest), PWM=0=on(brightest)

const byte LEDpin = 11;
void setup()
{
  pinMode (LEDpin, OUTPUT);
}

void loop()
{
  analogWrite (LEDpin, 250);  // dim  (LEDpin, brt)
  delay(1000);
  analogWrite (LEDpin, 200);  // 
  delay(1000);
  analogWrite (LEDpin, 150);
  delay(1000);
  analogWrite (LEDpin, 100);
  delay(1000);
  analogWrite (LEDpin, 50);
  delay(1000);
  analogWrite (LEDpin, 10);   
  delay(1000);
  analogWrite (LEDpin, 255);  // full off
  delay(2000);
}

Maybe what you're searching for is sth like RGB LED strip tutorial. It works for me, though there are some problems with overheating.

The arrangement where the output pin is providing a path to ground is known as current sinking. It will not damage the board and in fact it the way the majority of professional engineeres design an LED driver.

Hello,

if you want to connect more than one LED to your port, you can use a N FET like IRL540.
You can drive a complete 5 m strip without any heat problems.

best regards Andreas

Hey everyone,

Thanks for the advice (and the code, runaway_pancake). I'm going to go give this a shot right now!

Cheers again,
Alex

LXL15:
Hey everyone,

Thanks for the advice (and the code, runaway_pancake). I'm going to go give this a shot right now!

Cheers again,
Alex

Note that pancake's code is for one LED. You effectively have 3, so you can check each other by changing the pin number, or amplify the code for all 3 pins. Something like this

// anode to +5, cathode thru 220? to "pin11"
// "1" = off, "0" = on
// PWM=255=off(dimmest), PWM=0=on(brightest)

const byte redpin = 9;
const byte greenpin = 10;
const byte bluepin = 11;
void setup()
{
  pinMode (redpin, OUTPUT);
  pinMode (greenpin, OUTPUT);
  pinMode (bluepin, OUTPUT);
}

void loop()
{
  analogWrite (redpin, 250);  // dim  (LEDpin, brt)
  analogWrite (greenpin, 250);  // dim  (LEDpin, brt)
  analogWrite (bluepin, 250);  // dim  (LEDpin, brt)
  delay(100);

  analogWrite (redpin, 200);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 150);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 100);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 200); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 150); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 100); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 250);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 200);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 150);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 100);  
  delay(100);

  analogWrite (redpin, 50);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 100);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 150);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 200);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 50); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 100); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 150); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 200); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 50);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 100);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 150);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 200);  
  delay(100);

  analogWrite (redpin, 250);
  analogWrite (greenpin, 250); 
  analogWrite (bluepin, 250);  
  delay(100);
}