In this post I will explain how to make a simple fading RGB LED.
The code smoothly fades an RGB LED using PWM through 3 different basic colors (red, green and blue).

Hardware

  • Arduino Board
  • RGB LED
  • 3x Resistors 220 Ohm

Schematic

The schematic is very simple. Just connect the common cathod (+) to the GND pin. Then connect the 3 other leads to 3 PWM pins on your Arduino with a 220 Ohm resistor between.






Code


#define RED_PIN 9      // where the red pin is connected to
#define GREEN_PIN 10 // where the green pin is connected to
#define BLUE_PIN 11 // where the blue pin is connected to
#define DELAY 20 // 20ms internal delay; increase for slower fades

void setup() {
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
}

void loop() {
// fade from green to red
for(int i=0; i<255; i++) {
analogWrite(RED_PIN, i);
analogWrite(GREEN_PIN, 255-i);
analogWrite(BLUE_PIN, 0);
delay(DELAY);
}

// fade from red to blue
for(int i=0; i<255; i++) {
analogWrite(RED_PIN, 255-i);
analogWrite(GREEN_PIN, 0);
analogWrite(BLUE_PIN, i);
delay(DELAY);
}

// fade from blue to green
for(int i=0; i<255; i++) {
analogWrite(RED_PIN, 0);
analogWrite(GREEN_PIN, i);
analogWrite(BLUE_PIN, 255-i);
delay(DELAY);
}
}

References

Fading RGB LED (Arduino)
Arduino ColorCrossfader
RGB LEDs
Make an RGB LED Fader

Post a Comment