Using RGB LEDs With Arduino
by TechMartian in Circuits > Arduino
2639 Views, 17 Favorites, 0 Comments
Using RGB LEDs With Arduino
![2016-07-08 12.02.49.jpg](/proxy/?url=https://content.instructables.com/FI8/GVP8/J4SQF2V9/FI8GVP8J4SQF2V9.jpg&filename=2016-07-08 12.02.49.jpg)
![2016-07-08 11.58.42.jpg](/proxy/?url=https://content.instructables.com/F6E/7GNK/J4SQF2V5/F6E7GNKJ4SQF2V5.jpg&filename=2016-07-08 11.58.42.jpg)
This is the third module in a series of lessons for the Arduino 101 entered around the use of RGB LEDs and various applications with it.
RGB LEDs are very fun and useful things to use with an Arduino. They can be used as an indicator of certain sensor outputs or even cool light shows with enough RGB LEDs. RGB LEds can make virtually any colour since it has three different colour LEDs in one package, so turning on a combination of these individual colours can make a variety of colours
Tools and Material
![2016-07-08 11.46.49.jpg](/proxy/?url=https://content.instructables.com/F7I/LF42/J4SQF2U8/F7ILF42J4SQF2U8.jpg&filename=2016-07-08 11.46.49.jpg)
![2016-07-08 11.48.15.jpg](/proxy/?url=https://content.instructables.com/FJV/VQQ2/J4SQF2W0/FJVVQQ2J4SQF2W0.jpg&filename=2016-07-08 11.48.15.jpg)
![2016-07-08 11.48.33.jpg](/proxy/?url=https://content.instructables.com/FFG/OCRM/J4SQF2W1/FFGOCRMJ4SQF2W1.jpg&filename=2016-07-08 11.48.33.jpg)
- Arduino 101 or Arduino Uno
- Breadboard
- RGB LED
- 3 100Ω resistor
- Jumper Wires
Circuitry
![Screen Shot 2017-07-09 at 11.46.52 PM.png](/proxy/?url=https://content.instructables.com/FKG/R7NS/J4SQF36G/FKGR7NSJ4SQF36G.png&filename=Screen Shot 2017-07-09 at 11.46.52 PM.png)
![2016-07-08 11.52.18.jpg](/proxy/?url=https://content.instructables.com/FZ1/Z98Q/J4SQF2UH/FZ1Z98QJ4SQF2UH.jpg&filename=2016-07-08 11.52.18.jpg)
![2016-07-08 12.06.38.jpg](/proxy/?url=https://content.instructables.com/FFG/XKW5/J4SQF2VD/FFGXKW5J4SQF2VD.jpg&filename=2016-07-08 12.06.38.jpg)
![2016-07-08 11.59.14.jpg](/proxy/?url=https://content.instructables.com/FPH/REPH/J4SQF2V7/FPHREPHJ4SQF2V7.jpg&filename=2016-07-08 11.59.14.jpg)
![2016-07-08 11.58.39.jpg](/proxy/?url=https://content.instructables.com/FXM/9G29/J4SQF2V4/FXM9G29J4SQF2V4.jpg&filename=2016-07-08 11.58.39.jpg)
First, we will connect the Arduino power to the breadboard.
- Connect the 3.3V pin of the Arduino power to the red power rail in the breadboard
- Connect the GND pin of the Arduino to the black rail in the breadboard.
Then, we will wire the RGB LED on the breadboard to connect to the Arduino
- The longest leg of the RGB LED is ground, so connect this to the ground rail in the breadboard.
- Connect the rest of the legs to 100Ω resistors
- Each of these legs will be connected as a digital output from the Arduino. Starting from the leg closest to ground and the edge connect this to pin 5. In consecutive order up, connect the rest of these pins to pin 6 and 9 respectively.
Coding
![Screen Shot 2017-07-09 at 11.53.58 PM.png](/proxy/?url=https://content.instructables.com/F8U/SURY/J4SQF36E/F8USURYJ4SQF36E.png&filename=Screen Shot 2017-07-09 at 11.53.58 PM.png)
Each of the pin from the LED (apart from ground) are controlling individual colours, red green, and blue.
Turning on each of these pins will change the colour output of the LED. For example, the following code
digitalWrite (redPin, LOW);
digitalWrite (greenPin, LOW);
digitalWrite (bluePin, HIGH);
will output a blue colour and a combination of these will output a variety of colour mixes.
The full code is shown below:
//create variables for pin numbers. We are making them constants here, because they //never change.
const int RED_PIN = 5; const int GREEN_PIN = 6; const int BLUE_PIN = 9;
// How fast we plan to cycle through colors in milliseconds int DISPLAY_TIME = 10;
void setup() { //set the three pin variables as outputs pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); }
void loop() {
red();
delay(1000);
yellow();
delay(1000);
green();
delay(1000);
cyan();
delay(1000);
blue();
delay(1000);
purple();
delay(1000);
off();
delay(1000);
}
void blue (){ // Blue (turn just the blue LED on): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, HIGH);
}
void yellow(){ // Yellow (turn red and green on): digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, LOW);
}
void off(){
// Off (all LEDs off): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, LOW);
}
void green(){ // Green (turn just the green LED on): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, LOW); }
void red (){ // Red (turn just the red LED on): digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, LOW); }
void cyan() { // Cyan (turn green and blue on): digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, HIGH); }
void purple(){ // Purple (turn red and blue on): digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); digitalWrite(BLUE_PIN, HIGH); }
void white(){ // White (turn all the LEDs on): digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, HIGH); digitalWrite(BLUE_PIN, HIGH); }