First of all a few theory

Bi-colour leds.

A bi-colour LED has two LEDs wired in 'inverse parallel' (one forwards, one backwards) combined in one package with two leads. Only one of the LEDs can be lit at one time.


PWM

Pulse-width modulation (PWM), or pulse-duration modulation (PDM), is a commonly used technique for controlling power to inertial electrical devices, made practical by modern electronic power switches.

The average value of voltage (and current) fed to the load is controlled by turning the switch between supply and load on and off at a fast pace. The longer the switch is on compared to the off periods, the higher the power supplied to the load is.

The PWM switching frequency has to be much faster than what would affect the load, which is to say the device that uses the power. Typically switchings have to be done several times a minute in an electric stove, 120 Hz in a lamp dimmer, from few kilohertz (kHz) to tens of kHz for a motor drive and well into the tens or hundreds of kHz in audio amplifiers and computer power supplies.

The term duty cycle describes the proportion of 'on' time to the regular interval or 'period' of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.

The main advantage of PWM is that power loss in the switching devices is very low. When a switch is off there is practically no current, and when it is on, there is almost no voltage drop across the switch. Power loss, being the product of voltage and current, is thus in both cases close to zero. PWM also works well with digital controls, which, because of their on/off nature, can easily set the needed duty cycle.


For this tutorial you need the following parts:

A bi-color led

An Arduino UNO (or other) board


Connections

Connect the central pin of your bicolor led to the Arduino GND pin.

Connect the red pin of the biclor led to Arduino pin 11.

Connect the green pin of the bicolor led to Arduino pin 10.

 

That's all


The Software 

The software works as follow:

It sets the pin 10 and the pin 11 in output mode, then for each one of the pins run two loops, the first one writes (using the analogWrite function) on the pin values from 0 to 255 with steps of 5, every 20 ms. The second loop writes (using the analogWrite function) on the pin values from 255 to 0 with steps of -5, every 20 ms. 


There isn't anymore to say.


Here you are the code

//  Fade in/out is written by Luigi D'Andrea (www.xappsoftware.com)
//  It is distributed under the BSD license

//  Redistribution and use in source and binary forms, with or without modification, 
//  are permitted provided that the following conditions are met:
//  
//  Copyright (c) 2012, Luigi D’Andrea (www.xappsoftware.com)
//  All rights reserved.
//  
//  * Redistributions of source code must retain the above copyright notice,
//    this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright notice, 
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//  * Neither the name of the http://www.xappsoftware.com web site nor the names
//    of its contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//  
//  THIS SOFTWARE IS PROVIDED BY http://www.xappsoftware.com AND CONTRIBUTORS
//  “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
//  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
//  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
//  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
//  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE./* Fade in/out bicolor led */
int light; 
int ledPin1 = 11; 
int ledPin2 = 10;

void setup() 
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() 
{
  for(light=0; light <255; light=light+5) 
  {
    analogWrite(ledPin1, light);
    delay(20);
  }
  for(light=255; light >0; light=light-5) 
  {
    analogWrite(ledPin1, light);
    delay(20);
  }

  for(light=0; light <=255; light=light+5) 
  {
    analogWrite(ledPin2, light);
    delay(20);
  }
  for(light=255; light >=0; light=light-5) 
  {
    analogWrite(ledPin2, light);
    delay(20);
  }
}