RGB Led with Arduino Nano | Multiple colors

 

Controlling common Anode RGB LED with Arduino Nano.

Component List:

  1. RGB led - common Anode
  2. Arduino Nano
  3. Resistors



Resistors used: Red - 82 ohm, Green - 56 ohm, Blue - 56 ohm. Allowing approximately 20 mA to flow through LED.

Connection:  Red pin to D9, Green pin to D10, Blue Pin to D11, common pin to 5V of Nano

Working:

Cathode (-) pins of RGB are connected to 8-bit PWM pins of Arduino for voltage control. Providing a value of 0 means grounding the respective pin and turning the respective color ON.

Giving a value of 255 means 5v being applied to the pin, turning OFF the respective color.
So the value of 0 - 255 allows to control the intensity of the color.

By controlling the cathode voltage of all 3 pins, different colors can be made in a RGB led.

Arduino IDE code:

#define RedLED 9                   // D9 Pin      
#define BlueLED 11                 // D10 Pin    
#define GreenLED 10                // D11 Pin    

void setup()
{
pinMode(RedLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
pinMode(GreenLED, OUTPUT);

// Initiate RGB
analogWrite(RedLED, 255);
analogWrite(BlueLED, 255);
analogWrite(GreenLED,255);

/* White Color */
analogWrite(RedLED, 0);
analogWrite(BlueLED, 0);
analogWrite(GreenLED,0);
delay(200);
}

void loop()
{
  uint8_t Red = 0, Blue = 255, Green = 255;
  analogWrite(BlueLED, Blue);
 
  // Red to Green
  for( ; Red < 255, Green > 0 ; Red ++, Green --)
  {
    analogWrite(RedLED, Red);
    analogWrite(GreenLED, Green);
    delay(200);
  }
  // Green to Blue
  for( ; Green < 255, Blue > 0 ; Green ++, Blue --)
  {
    analogWrite(GreenLED, Green);
    analogWrite(BlueLED, Blue);
    delay(200);
  }
  // Blue to Red
  for( ; Blue < 255, Red > 0 ; Blue ++, Red --)
  {
    analogWrite(BlueLED, Blue);
    analogWrite(RedLED, Red);
    delay(200);
  }
}

For I2C driver program in ATMega328P, see this post


Comments