Lab 5 - RGB LED Lab Details

Objective:  to make an RGB LED change colors


Parts List

  Qty
RGB LED (common anode) 1
330ohm resistor 3
wire (red) 1*
wire (black) 1
wire (any color) 4

*this wire is not powering anything in the circuit, but it is standard to use this to power the 5V column


 Parts Set Up

Instructional Parts Set-Up

For this set-up, we are using the following numbers for the common cathode RGB:

Part Connection 1 Connection 2 Notes
RGB 1 (Red) A4 -  
RGB 2 (longest wire) A5 -  
RGB 3 (Green) A6 -  
RGB 4 (Blue) A7 -  
330 Ohm Resistor E4 G4 For the Red side
330 Ohm Resistor E6 G6 For the Green side
330 Ohm Resistor E7 G7 For the Blue side
Wire (any color) H4 Pin 9 This controls Red
Wire (any color) H6  Pin 10  This controls Green
Wire (any color) H7  Pin 11  This controls Blue
Wire (any color) E5 -ve column This is the "common" cathode (-ve or GND)
Wire (black) -ve column Arduino GND  
Wire (red) - optional, not used +ve column Arduino 5V  

Visual Parts Set-Up 


Lab 5 Code

In this lab, you are sending analog signals from digital pins 9, 10 and 11 to represent how much power you're sending into red, green and blue respectively.  

Remember that the following digital pins can be used for analogWrite:  3, 5, 6, 9, 10, and 11.

Without Comments

With Comments

Line Code Comments
1 const int RED_LED_PIN = 9; this creates a constant integer for the Red LED pin (pin 9)
2 const int GREEN_LED_PIN = 10; this creates a constant integer for the Green LED pin (pin 10)
3 const int BLUE_LED_PIN = 11; this creates a constant integer for the Blue LED Pin (pin 11)
4 int redIntensity = 0; this variable will track how much red light is generated
5 int greenIntensity = 0; this variable tracks how much green light is generated
6 int blueIntensity = 0; this variable tracks how much blue light is generated 
7 const int DISPLAY_TIME = 100; this controls the delay between changing lights (100ms)
8 void setup() void setup will be empty for this program
9 { there must be a void setup() for every Arduino program, even if empty!
10 }  
11 void loop()  
12 { this bracket starts void loop 
13 for (greenIntensity = 0; greenIntensity <= 255; greenIntesnsity +=5 this for-loop increases the value of green from 0 to 255, increasing by 5 each loop 
14  { this is the start bracket for the for-loop
15 redIntensity = 255 - greenIntensity;  the power of red will adjust as green changes (red = 255- green) 
16 analogWrite(GREEN_LED_PIN, greenIntensity); power is given to the green pin
17 analogWrite(RED_LED_PIN, redIntensity); power is given to the red pin
18 delay(DISPLAY_TIME); the program pauses before going to the top of this for-loop
19 } this ends the for-loop for greenIntensity
20 for (blueIntensity = 0; blueIntensity <= 255; blueIntensity +=5) this for-loop increases the value of blue from 0 to 255, increasing by 5 each loop
21 { this bracket starts the for-loop for blueIntensity
22 greenIntensity = 255 - blueIntensity; the power of green will adjust as blue changes (green = 255 -blue)
23 analogWrite(BLUE_LED_PIN, blueIntensity); power is given to the blue pin 
24 analogWrite(GREEN_LED_PIN, greenIntensity); power is given to the green pin
25 delay(DISPLAY_TIME); the program pauses before going to the top of this for-loop
26 this ends the for-loop for blueIntensity
27 for(redIntensity = 0; redIntensity <= 255; redIntensity +=5)  this is the for-loop for redIntensity
28 this bracket starts the for-loop for redIntensity
29 blueIntensity = 255 - redIntensity; the power of blue will adjust as red changes (blue = 255 - red)
30 analogWrite (RED_LED_PIN, redIntensity) power is given to the red pin
28 analogWrite (BLUE_LED_PIN, blueIntensity); power is given to the blue pin 
29 delay(DISPLAY_TIME); the program pauses before going to the top of this for-loop
30 } this ends the for-loop for redIntensity
31 } this is the closing bracket for the void loop()

**this code is originally from CIRC-12 from the Sparkfun Inventor's Kit.