Introduction: Using RGB LEDs With Arduino

Picture of Using RGB LEDs With Arduino

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

Step 1: Tools and Material

Picture of Tools and Material
  • Arduino 101 or Arduino Uno
  • Breadboard
  • RGB LED
  • 3 100Ω resistor
  • Jumper Wires

Step 2: Circuitry

Picture of Circuitry

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.

Step 3: Coding

Picture of Coding

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

Comments

Droxz (author)2017-07-10

Nice instructable, but where is the part explaining common anode or common cathode rgb leds? :)

Thanks for sharing.

About This Instructable

476views

15favorites

License:

Bio: Aerospace Engineer working in Software Development with a passion for Hardware.
Add instructable to: