Serial Communications and RGB LEDs

As we all know ;), LEDs are Light Emitting Diodes. When a voltage is applied to the anode (+ terminal) and ground is connected to the cathode (- terminal), electrons are able to recombine and emit light. RGB LEDs follow the same idea but they have the ability to emit Red, Green, Blue, or a combination of colors.
By the end of this example we are going to be able to connect an RGB LED that has a common cathode, use the serial monitor to communicate with the LED, and control the RGB LED with PWM.
There are two types of RGB LEDs: common Cathode, and Common Diode

Common Cathode:
has a common ground pin and light is emitted by supplying a voltage to the other pins. This is the configuration that is being used in this example

Common Anode:
has a common supply voltage and light is emitted when the other pins are connected to ground. Pretty simple?

Now, the goal is to control the brightness of each one of these lights using analogWrite (on a ~pin) and PWM (Pulse Width Modulation). PWM switches the voltage from HIGH to LOW with different amounts of on and off time. The result is an average voltage. For example: at 50% Duty Cycle the voltage will read 2.5Vs, at 75% the voltage will read 3.75Vs.


1 UNO Board

1 Breadboard

1 680 Ω Resistor

2 1k Ω Resistor

1  6” USB A/B cable


Jumper Cable



duino set up


  1. Connect the common pin (the longest lead) to ground (Common Cathode) or 5v(Common Anode)
  2. The Red pin, which is the single pin next to the common lead, gets connected to digital pin 11 through a 680Ω resistor
  3. Now, connect the blue lead to pin 10 and the green lead to pin 9. Both should have a 1kΩ resistor between them and their pins



/*This program controls a RGB LED through the Serial monitor.  The input is the the form 
 * 255,255,255 [R,G,B}; where the 255 erpresents any number from 0-255 seperated by a non 
 * digit character.

int LED9 = 11;//RED PWM pin
int LED10 = 10;//green PWM pin
int LED11 = 9;//BLUE  PWM pin

int colors[] = {0,0,0}; //starts with all of the LED off
int colorID=0; //will be used to define the locations is 'colors[colorID] 0 to 2

String inputString = "";         //a string to hold incoming data
int inChar;                      //stores each byte of incoming data

void setup() {

void loop() {

while (Serial.available() >0) { //checks for an available byte waiting on its internal storage
    inChar =;     //saves the information as a character 
    if (isDigit(inChar)){      //if the saved character is a digit DO THHIS......
      inputString+=(char)inChar;//adds character to the end of a string
    }else if(inChar=='\n' || colorID==2){      //checks to see if there is a line end or too many color calls
      colors[colorID]=inputString.toInt();    //converts string to an int and saves to color
      while(colorID<2){  //this loop turns off all colors not defined the the serial
      colorID=0;   //resets colorID
      Serial.println(inputString); //serial terminal for trouble shooting
      inputString="";//resets string
    }else{ //runs if colors have not been defined or a charcter besides a number was sent

    /* writes the PWM value for each color*/ 


Leave a Reply

Your email address will not be published. Required fields are marked *