0% found this document useful (0 votes)
9 views

RGB Led

Uploaded by

areyouokbusy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

RGB Led

Uploaded by

areyouokbusy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

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.

-The code controls an RGB LED connected to pins 3 (green), 5 (blue),


and 7 (red).
-It cycles through different colors (green, blue, red, cyan, yellow,
magenta, white, and off) by turning on different combinations of the
RGB pins using digitalWrite() with a 500 ms delay between each color
change.
-This creates a simple light show that displays basic colors and
transitions smoothly through them.
PROGRAM #2
int redPin= 5; delay(1000);
int greenPin = 6; setColor(127, 127, 127); // Light Blue
int bluePin = 7; delay(1000);
}
void setup() { void setColor(int redValue, int
pinMode(redPin, OUTPUT); greenValue, int blueValue)
pinMode(greenPin, OUTPUT); {
pinMode(bluePin, OUTPUT); analogWrite(redPin, redValue);
} analogWrite(greenPin, greenValue);
void loop() { analogWrite(bluePin, blueValue);
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
This Arduino code controls an RGB LED, which consists of three individual
LEDs (Red, Green, and Blue) connected to different pins on the Arduino.
By combining these colors at different brightness levels using PWM
(Pulse Width Modulation), you can produce various colors of light. The
code cycles through different colors, changing every second.

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)):

-Red ( setColor(255, 0, 0) ): Maximum red value, no green, no blue.


-Green ( setColor(0, 255, 0) ): Maximum green value, no red, no blue.
-Blue ( setColor(0, 0, 255) ): Maximum blue value, no red, no green.
-White ( setColor(255, 255, 255) ): Maximum values for all three
colors, which produces white light.
-Purple ( setColor(170, 0, 255) ): A mix of red and blue to create
purple.
-Light Blue ( setColor(127, 127, 127) ): A low intensity of all three
colors, producing a soft, light blue.
setColor() Function:
void setColor(int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
-This function takes three arguments: redValue, greenValue, and
blueValue, which represent the brightness of each color component
(values between 0 and 255).
-analogWrite() is used to control the brightness of each color by
adjusting the PWM signal on the specified pin. The higher the value
(closer to 255), the brighter the color.
The code controls an RGB LED using three digital pins connected to
the red, green, and blue components of the LED.

The setColor() function is called with different RGB values to


generate different colors.

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

BLUE_PIN, GREEN_PIN, and RED_PIN are defined to correspond to


digital pins 3, 5, and 6, respectively. These pins will control the blue,
green, and red LEDs.
setup() Function:
void setup() {
pinMode(BLUE_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
}
pinMode() is used to set the three defined pins as outputs, so the
Arduino can control the LEDs connected to them.
loop() Function:
void loop() {
increaseLight(BLUE_PIN);
decreaseLight(BLUE_PIN);
increaseLight(GREEN_PIN);
decreaseLight(GREEN_PIN);
increaseLight(RED_PIN);
decreaseLight(RED_PIN);
}
-The loop() function runs continuously, cycling through each LED pin.
-For each LED, it first calls the increaseLight() function to gradually brighten
the LED, then decreaseLight() to dim it back down. It repeats this process for
the blue, green, and red LEDs in sequence.
increaseLight() Function:
void increaseLight(int ledPin) {
for (int i = 0; i <= 255; i++) {
analogWrite(ledPin, i);
delay(10);
}
}
-This function takes an ledPin as an argument and gradually increases the
brightness of the LED.
-The for loop starts at 0 and increments to 255. The analogWrite(ledPin, i)
command adjusts the LED brightness by varying the PWM signal, with 0 being
completely off and 255 being fully bright.
-The delay(10) function introduces a 10 ms delay between each brightness
step, creating a smooth fade-in effect.
decreaseLight() Function:
void decreaseLight(int ledPin) {
for (int i = 255; i >= 0; i--) {
analogWrite(ledPin, i);
delay(10);
}
}
-This function works similarly to increaseLight(), but instead of
increasing brightness, it gradually dims the LED from full brightness
(255) down to off (0).
-The for loop decrements from 255 to 0, decreasing the brightness in
the same smooth manner.
The code gradually increases and decreases the brightness of three
LEDs (blue, green, and red) connected to PWM-capable pins (3, 5,
6).

The increaseLight() and decreaseLight() functions use PWM


(analogWrite()) to fade the LEDs in and out.

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.

You might also like