RGB Led
RGB Led
RGB LED
PROGRAM #1
int green = 3; digitalWrite(blue, LOW); digitalWrite(red, LOW);
int blue = 5; delay(500); digitalWrite(green, LOW);
int red = 7; delay(500);
digitalWrite(green, HIGH); }
void setup() { digitalWrite(blue, HIGH);
pinMode(green, OUTPUT); digitalWrite(red, LOW);
pinMode(blue, OUTPUT); delay(500);
pinMode(red, OUTPUT); digitalWrite(green, HIGH);
} digitalWrite(red, HIGH);
void loop(){ digitalWrite(blue, LOW);
digitalWrite(green, HIGH); delay(500);
digitalWrite(blue, LOW);
digitalWrite(red, LOW); digitalWrite(blue, HIGH);
delay(500); digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(green, LOW); delay(500);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW); digitalWrite(blue, HIGH);
delay(500); digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(red, HIGH); delay(500);
digitalWrite(green, LOW); digitalWrite(blue, LOW);
This Arduino code controls an RGB LED by turning on and off different
combinations of the red, green, and blue pins. The code cycles through
various colors by setting different combinations of these pins to HIGH
(on) or LOW (off), with a 500 ms delay between each change.
int green = 3;
int blue = 5;
int red = 7;
Three integer variables green, blue, and red store the pin numbers that
control the green, blue, and red components of the RGB LED.
setup() Function:
void setup() {
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
}
In the setup() function, pinMode() sets the pins connected to the RGB
LED as output so that the Arduino can send signals to them to control
the light.
loop() Function:
void loop() {
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(500);
The loop() function continuously cycles through different colors by
setting combinations of red, green, and blue to either HIGH (on) or
LOW (off).
Color Changes: The LED changes color by turning on different
combinations of the RGB components:
Green Color:
digitalWrite(green, HIGH);
digitalWrite(blue, LOW);
digitalWrite(red, LOW);
delay(500);
Turns on the green component while the blue and red components are
off.
Blue Color:
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
delay(500);
Turns on the blue component while the green and red components are off.
Red Color:
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, LOW);
delay(500);
Turns on the red component while the green and blue components are off.
Cyan Color:
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
digitalWrite(red, LOW);
delay(500);
Turns on both green and blue components while the red component is off, resulting in
cyan.
Yellow Color:
digitalWrite(green, HIGH);
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
delay(500);
Turns on both red and green components while the blue component is off, resulting in
yellow.
Magenta Color:
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
delay(500);
Turns on both red and blue components while the green component is off,
resulting in magenta.
White Color:
digitalWrite(blue, HIGH);
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
delay(500);
Turns on all three components (red, green, and blue), producing white light.
Turn off all colors:
digitalWrite(blue, LOW);digitalWrite(red, LOW);digitalWrite(green,
LOW);delay(500);
Turns off all components, leaving the LED off.
int redPin = 5;
int greenPin = 6;
int bluePin = 7;
Three variables redPin, greenPin, and bluePin are declared to store the
pin numbers that control the red, green, and blue components of the
RGB LED, respectively.
setup() Function:
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
pinMode() is used to set the three pins (5, 6, and 7) as outputs so the
Arduino can control the brightness of each color in the RGB LED.
loop() Function:
void loop() {
setColor(255, 0, 0); // Red Color
delay(1000);
setColor(0, 255, 0); // Green Color
delay(1000);
setColor(0, 0, 255); // Blue Color
delay(1000);
setColor(255, 255, 255); // White Color
delay(1000);
setColor(170, 0, 255); // Purple Color
delay(1000);
setColor(127, 127, 127); // Light Blue
delay(1000);
}
The loop() function continuously cycles through six different color
combinations, each displayed for one second (delay(1000)):
The loop() function cycles through red, green, blue, white, purple,
and light blue, holding each color for one second.
You can modify the RGB values in setColor() to create any color you
want by mixing different intensities of red, green, and blue.
PROGRAM #3
#define BLUE_PIN 3
#define GREEN_PIN 5
#define RED_PIN 6
void setup() {
pinMode(BLUE_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
}
void loop() {
increaseLight(BLUE_PIN);
decreaseLight(BLUE_PIN);
increaseLight(GREEN_PIN);
decreaseLight(GREEN_PIN);
increaseLight(RED_PIN);
decreaseLight(RED_PIN);
}
void increaseLight(int ledPin) {
for (int i = 0; i <= 255; i++) {
analogWrite(ledPin, i);
delay(10); }
}
void decreaseLight(int ledPin) {
for (int i = 255; i >= 0; i--) {
analogWrite(ledPin, i);
delay(10); }
}
This Arduino code controls three LEDs connected to digital pins using
Pulse Width Modulation (PWM) to gradually increase and decrease the
brightness of the LEDs. Each LED is assigned to a pin, and the code
cycles through blue, green, and red LEDs to smoothly fade in and fade
out the light.
#define BLUE_PIN 3
#define GREEN_PIN 5
#define RED_PIN 6
The process continuously cycles through the blue, green, and red
LEDs, creating a smooth lighting effect for each color.
You can modify the speed of the fading effect by adjusting the
delay(10) values or change the pins to control different LEDs.