Get tutorials Arduino Super Kit V3.0 for Arduino

Lesson 5 RGB LED

Introduction

Previously we've used the PWM technology to control an LED brighten and dim. In this lesson, we will use it to control an RGB LED to flash various kinds of color. When different PWM values are set to the R, G, and B pins of the LED, its brightness will be different. When the three different colors are mixed, we can see that the RGB LED flashes different colors.


Components

- 1 * RGB LED

- 3 * Resistor (220Ω)

- 1 * Breadboard

- 1 * SunFounder Uno board

- 1 * USB cable

- Jumper wires


Principle

For details of RGB, please refer to the introduction of RGB LED in Components Introduction.

The schematic diagram



Principle: In this experiment, we will also use PWM which, if you’ve followed the lessons thus far, you already have a basic understanding of. Here we input a value between 0 and 255 to the three pins of the RGB LED to make it display different colors. After connecting the pins of R, G, and B to a current limiting resistor, connect them to the pin 9, pin 10, and pin 11 respectively. The longest pin (GND) of the LED connects to the GND of the Uno. When the three pins are given different PWM values, the RGB LED will display different colors.

Experimental Procedures

Step 1: Build the circuit



Step 2: Open the code file

Step 3: Select correct Board and Port

Step 4: Upload the sketch to the SunFounder Uno board


Here you should see the RGB LED flash circularly red, green, and blue first, then red, orange, yellow, green, blue, indigo, and purple.




Code

//RGB LED
//The RGB LED will appear red, green, and blue first, then red, orange, yellow, green, blue, indigo, and purple.
//Email:support@sunfounder.com
//Website:www.sunfounder.com
//2015.5.7
/*************************************************************************/
const int redPin = 11;  // R petal on RGB LED module connected to digital pin 11 
const int greenPin = 10;  // G petal on RGB LED module connected to digital pin 10 
const int bluePin = 9;  // B petal on RGB LED module connected to digital pin 9 
/**************************************************************************/
void setup()
{ 
  pinMode(redPin, OUTPUT); // sets the redPin to be an output 
  pinMode(greenPin, OUTPUT); // sets the greenPin to be an output 
  pinMode(bluePin, OUTPUT); // sets the bluePin to be an output 
}    
/***************************************************************************/
void loop()  // run over and over again  
{    
  // Basic colors:  
  color(255, 0, 0); // turn the RGB LED red 
  delay(1000); // delay for 1 second  
  color(0,255, 0); // turn the RGB LED green  
  delay(1000); // delay for 1 second  
  color(0, 0, 255); // turn the RGB LED blue  
  delay(1000); // delay for 1 second 
  // Example blended colors:  
  color(255,0,252); // turn the RGB LED red  
  delay(1000); // delay for 1 second  
  color(237,109,0); // turn the RGB LED orange  
  delay(1000); // delay for 1 second  
  color(255,215,0); // turn the RGB LED yellow  
  delay(1000); // delay for 1 second  
  color(34,139,34); // turn the RGB LED green  
  delay(1000); // delay for 1 second 
  color(0,112,255); // turn the RGB LED blue  
  delay(1000); // delay for 1 second
  color(0,46,90); // turn the RGB LED  indigo 
  delay(1000); // delay for 1 second
  color(128,0,128); // turn the RGB LED purple  
  delay(1000); // delay for 1 second
}     
/******************************************************/
void color (unsigned char red, unsigned char green, unsigned char blue)// the color generating function  
{    
  analogWrite(redPin, red);   
  analogWrite(greenPin, green); 
  analogWrite(bluePin, blue); 
}
/******************************************************/




Previous chapter: Lesson 4 Breathing LED

Next chapter: Lesson 6 Serial Monitor

SunFounder
May 20 2017 at 09:44 am



© Developed by CommerceLab