Lesson 19 Rainbow LED

Share for us

Introduction

RGB LED modules can emit various colors of light. Three LEDs of red, green, and blue are packaged into a transparent or semitransparent plastic shell with four pins led out. The three primary colors of red, green, and blue can be mixed and compose all kinds of colors by brightness, so you can make an RGB LED emit colorful light by controlling the circuit.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * RGB LED module

– 1 * 4-Pin anti-reverse cable

Experimental Principle

In this experiment, we will use PWM technology to control the brightness of RGB. We’ve described the concept in Lesson 2 of this kit. You may check the information, if necessary.

Each of the three color channels: red, green, and blue, has 255 stages of brightness. When the three primary colors are all 0, the LED light is turned off. When the colors are all 255, the LED gets brightest.  Here we input any value between 0 and 255 to the three pins of the RGB LED to make it display different colors.

RGB LEDs can be categorized into common anode LED and common cathode LED. In this experiment, we use a common cathode RGB LED. See the schematic diagram as below:

Experimental Procedures

Step 1: Build the circuit

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, you can see RGB LED flash red, green and blue first, and then change to red, orange, yellow, green, blue, indigo and purple.

 Code

/*************************************************************************/
const int redPin = 10; // R petal on RGB LED module connected to digital pin 11 
const int greenPin = 9; // G petal on RGB LED module connected to digital pin 9 
const int bluePin = 8; // B petal on RGB LED module connected to digital pin 10 
/**************************************************************************/ 
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,0); // 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(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
  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(bluePin, blue); 
  analogWrite(greenPin, green); 
}
/******************************************************/