Lesson 2 RGB LED

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp


There are two kinds of packages for RGB LED (as shown below) in this kit. One is Surface Mount Device (SMD) type, and the other is Dual In-line Package (DIP) type.  


– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * RGB LED module

– Several jumper wires

Experimental Principle

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, red, green, and blue, can be mixed into various kinds of color by brightness, so you can make an RGB LED emit colorful light by controlling the circuit.

In this experiment, we will also use PWM technology to control the brightness of RGB.

Before we talk about PWM, let’s take a look at the applications of PWM first. PWM has been successfully applied in motor speed regulation, steering angle control, light intensity control and signal output. For example, when PWM is applied to a horn, it will make sounds. After we know about its special functions, let’s find out what PWM really is.

Pulse Width Modulation commonly refers to PWM. Pulse Width Modulation (PWM) is a digital coding method for analog signal levels. Since a computer cannot output an analog voltage but digital voltage value 0V or 5V, we modulate the duty cycle of square waves to encode a specific level of analog signal by using a high-resolution counter. PWM signals are essentially digital signals, for the full amplitude DC power supply is either 5V (ON) or 0V (OFF) at any given time. Voltage or current source is applied to an analog load in the form of ON or OFF repetitive pulse sequence. When it is on, DC power supply will be applied to the load; when it is off, DC power supply will be disconnected. If only the bandwidth is wide enough, any analog value can be encoded by PWM. The output voltage value is calculated by the on and off time. Vout = (Ton/ T)*Vmax.

We can see from the top oscillogram that the amplitude of DC voltage output is 5V. However, the actual voltage output is only 3.75V through PWM, for the high level only takes up 75% of the total voltage within a period.

Here is the introduction to three basic parameters of PWM:

  1. The term duty cycle describes the proportion of ‘on’ time to the regular interval or ‘period’ of time
  2. Period describes the reciprocal of pulses in one second
  3.  Upwm describes the mean value of output voltage (e.g. 0V-5V)

Here we input any value between 0 and 255 to the three pins of the RGB LED to make it flash different colors.

RGB LEDs can be categorized into common anode type and common cathode type. In this experiment, we use common cathode RGB LED.

Experimental Procedures

Step 1: Build the circuit

                       RGB LED Module          SunFounder Uno

                                    R ————————————-D8

                                    G ———————————— D10

                                    B ————————————- D9

                                    –  ———————————– GND

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

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno

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

C Code

const int redPin = 8; // 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 9
const int bluePin = 9; // 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);