Recent Changes - Search:

Reference Links

Topics

Programming

Prior Class Pages

PM Wiki

edit SideBar

RGBLEDs

//RGB LEDs have 3 diodes inside and typically share either a common power or common ground wire. You can often tell by looking at the shape of the component inside the epoxy case. The big part is always attached to the ground wire.

//set up circuit with RGB LED

//how do we pick which resistor to use?

We use Ohms Law, otherwise known as V = IR. Voltage in Volts = Current in Amps times Resistance in Ohms.
We need to solve for R to find the resistor value, but first we have to know what values to put in for the other two (V and I).

I is almost always 20mA when we are calculating for 1 LED. That the max current for most small LEDs. Remember, I is in amps. 1 amp = 1000mA so 20mA is equal to .02 amps

Start with the voltage drop for the LED (red, green or blue) requiring the least voltage

Since we don't know which one is which, we should err on the side of caution. Red is the most sensitive, requiring only 1.8 volts to go on. Its vF ("forward voltage") is 1.8. Blue and Green are often higher (usually around 3).

V is the amount of voltage we need to remove with the resistor to make the LED not overheat. Since the Arduino puts out 5v, and we need 1.8v for the red LED, 5-1.8=3.2 is the amount of V we need to get rid of. So we solve for the below to get R.

3.2 = .02 * R R = 3.2/.02, R = 160

Our resistors must be 160 ohms or greater. A 220ohm or 330ohm resistor will work fine, although the LEDs may not be as bright as they could be.

Download all this information here:
Full Description With Code: http://jasonkrugman.com/classes/images/led_fading_and_functions_demo.pdf
Circuit Diagram in Fritzing: http://jasonkrugman.com/classes/images/rgb_led_diagram.fzz

Most Basic Sketch - turn on and off a single LED

int ledPin = 3;

void setup(){

  Serial.begin(9600);  
  pinMode(ledPin, OUTPUT);

}

void loop(){

  digitalWrite(ledPin, HIGH);
  Serial.println("High");
  delay(1000);
  digitalWrite(ledPin, LOW);
  Serial.println("low");
  delay(1000);

}

Fade a single LED with a for loop

int ledPin = 3;

void setup(){

  Serial.begin(9600);  
  pinMode(ledPin, OUTPUT);

}

void loop(){

for(int i=0; i<255; i++){

  analogWrite(ledPin, i);
  Serial.println(i);
  delay(10);

}

for(int i=255; i>0; i--){

  analogWrite(ledPin, i);
  Serial.println(i);
  delay(10);

}

}

Fade an RGB LED with a for loop

int rPin = 3;
int gPin = 5;
int bPin = 6;

void setup(){

  Serial.begin(9600);  

pinMode(rPin, OUTPUT); pinMode(gPin, OUTPUT); pinMode(bPin, OUTPUT);

}

void loop(){

for(int i=0; i<255; i++){

  analogWrite(rPin, i);
  Serial.println(i);
  delay(10);

}

for(int i=255; i>0; i--){

  analogWrite(rPin, i);
  Serial.println(i);
  delay(10);

}

for(int i=0; i<255; i++){

  analogWrite(gPin, i);
  Serial.println(i);
  delay(10);

}

for(int i=255; i>0; i--){

  analogWrite(gPin, i);
  Serial.println(i);
  delay(10);

}

for(int i=0; i<255; i++){

  analogWrite(bPin, i);
  Serial.println(i);
  delay(10);

}

for(int i=255; i>0; i--){

  analogWrite(bPin, i);
  Serial.println(i);
  delay(10);

}

}

Fade an RGB LED with functions

int rPin = 3;
int gPin = 5;
int bPin = 6;

void setup(){

Serial.begin(9600);
pinMode(rPin, OUTPUT);
pinMode(gPin, OUTPUT);
pinMode(bPin, OUTPUT);

}

void loop(){

fade(rPin);
fade(gPin);
fade(bPin);

}

void fade(int pin){

  for(int i=0; i<255; i++){
  analogWrite(pin, i);
  Serial.println(i);
  delay(10);

}

for(int i=255; i>0; i--){

  analogWrite(pin, i);
  Serial.println(i);
  delay(10);

}

}

Fade an RGB LED with functions and also input a time to change the speed

int rPin = 3;
int gPin = 5;
int bPin = 6;

void setup(){

  Serial.begin(9600);  
  pinMode(rPin, OUTPUT);  
  pinMode(gPin, OUTPUT);  
  pinMode(bPin, OUTPUT);  

}

void loop(){

  fade(rPin, 1);
  fade(gPin, 10);
  fade(bPin, 30);

}

void fade(int pin, int time){

  for(int i=0; i<255; i++){
    analogWrite(pin, i);
    Serial.println(i);
    delay(time);
  }

  for(int i=255; i>0; i--){
    analogWrite(pin, i);
    Serial.println(i);
    delay(time);
  }

}

Some really nice code by Vivian Wu to fade between RGB colors //RGB cycler by Vivian Wu

int leds[ ] = {9,10,11};
int brightness[ ] = {0,80,160};
boolean state[ ] = {HIGH,HIGH,HIGH};
int rate = 5;

void setup(){

  for (int i =0; i<3; i++)
    pinMode(leds[i],OUTPUT);

}

void loop(){

  for (int i =0; i<3; i++){

    if (state[i]==HIGH)
      brightness[i]+=rate;         
    else
      brightness[i]-=rate;
    if ((brightness[i]>=240)||(brightness[i]<=0))
      state[i]=!(state[i]);
    analogWrite(leds[i], brightness[i]);

}

delay(50);

}

Edit - History - Print - Recent Changes - Search
Page last modified on April 08, 2014, at 09:20 AM