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
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
2016-07-08 11.48.15.jpg
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
2016-07-08 11.52.18.jpg
2016-07-08 12.06.38.jpg
2016-07-08 11.59.14.jpg
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

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); }