top of page
Writer's picturejugaad club

Session 1 : LED And RGB LED

Updated: Jan 12, 2022

Light Emitting Diode (LED)



Definition: The LED is a PN-junction diode which emits light when an electric current passes through it in the forward direction. In the LED, the recombination of charge carrier takes place. The electron from the N-side and the hole from the P-side are combined and gives the energy in the form of heat and light. The LED is made of semiconductor material which is colourless, and the light is radiated through the junction of the diode.

The LEDs are extensively used in segmental and dot matrix displays of numeric and alphanumeric character. The several LEDs are used for making the single line segment while for making the decimal point single LED is used.



Construction of LED


The recombination of the charge carrier occurs in the P-type material, and hence P-material is the surface of the LED. For the maximum emission of light, the anode is deposited at the edge of the P-type material. The cathode is made of gold film, and it is usually placed at the bottom of the N-region. This gold layer of cathode helps in reflecting the light to the surface.

The gallium arsenide phosphide is used for the manufacturing of LED which emits red or yellow light for emission. The LED are also available in green, yellow amber and red in colour.

The simple transistor can be used for off/on of a LED as shown in the figure above. The base current IB conducts the transistor, and the transistor conducts heavily. The resistance RC limits the current of the LED.



Working of LED


The working of the LED depends on the quantum theory. The quantum theory states that when the energy of electrons decreases from the higher level to lower level, it emits energy in the form of photons. The energy of the photons is equal to the gap between the higher and lower level.

The LED is connected in the forward biased, which allows the current to flows in the forward direction. The flow of current is because of the movement of electrons in the opposite direction. The recombination shows that the electrons move from the conduction band to valence band and they emits electromagnetic energy in the form of photons. The energy of photons is equal to the gap between the valence and the conduction band.



Advantages of LED in electronic displays


The followings are the major advantages of the LED in an electronics displays:-

  1. The LED are smaller in sizes, and they can be stacked together to form numeric and alphanumeric display in the high-density matrix.

  2. The intensity of the light output of the LED depends on the current flows through it. The intensity of their light can be controlled smoothly.

  3. The LED are available which emits light in the different colours like red, yellow, green and amber.

  4. The on and off time or switching time of the LED is less than of 1 nanoseconds. Because of this, the LED are used for the dynamic operation.

  5. The LEDs are very economical and giving the high degree of reliability because they are manufactured with the same technology as that of the transistor.

  6. The LED are operated over a wide range of temperature say 0° – 70°. Also, it is very durable and can withstand shock and variation.

  7. The LED have a high efficiency, but they require moderate power for operation. Typically, the voltage of 1.2V and the current of 20mA is required for full brightness. Therefore, it is used in a place where less power are available.



Disadvantages of LED


The LED consume more power as compared to LCD, and their cost is high. Also, it is not used for making the large display.




LED AND ARDUINO:


If you want to lit an external LED with this sketch, you need to build this circuit, where you connect one end of the resistor to the digital pin correspondent to the LED_BUILTIN constant. Connect the long leg of the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short leg of the LED (the negative leg, called the cathode) to the GND. In the diagram below we show an UNO board that has D13 as the LED_BUILTIN value.

The value of the resistor in series with the LED may be of a different value than 220 ohm; the LED will lit up also with values up to 1K ohm.



Schematic:



Code:


After you build the circuit plug your Arduino board into your computer, start the Arduino Software (IDE) and enter the code below. You may also load it from the menu File/Examples/01.Basics/Blink . The first thing you do is to initialize LED_BUILTIN pin as an output pin with the line

pinMode(LED_BUILTIN, OUTPUT);

In the main loop, you turn the LED on with the line:

digitalWrite(LED_BUILTIN, HIGH);

This supplies 5 volts to the LED anode. That creates a voltage difference across the pins of the LED, and lights it up. Then you turn it off with the line:

digitalWrite(LED_BUILTIN, LOW);

That takes the LED_BUILTIN pin back to 0 volts, and turns the LED off. In between the on and the off, you want enough time for a person to see the change, so the delay() commands tell the board to do nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens for that amount of time. Once you've understood the basic examples, check out the BlinkWithoutDelay example to learn how to create a delay while doing other things.

void setup() {
 // initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second
} 



Fading LED using arduino:


Schematic:


Code:

int led = 3;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

void loop()
{
  // set the brightness of pin 9:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  delay(30);
}



LED Chaser using arduino:


Schematic:


Code:

int led1 = 3, led2 = 4, led3 = 5, led4 = 6;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
}

void loop()
{
  digitalWrite(led1, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  delay(500);
  
  digitalWrite(led2, HIGH);
  delay(500);
  digitalWrite(led2, LOW);
  delay(500);
  
  digitalWrite(led3, HIGH);
  delay(500);
  digitalWrite(led3, LOW);
  delay(500);
  
  digitalWrite(led4, HIGH);
  delay(500);
  digitalWrite(led4, LOW);
  delay(500);
}




Controlling LED using potentiometer:


Schematic:


Code:

int pin_LED = 11;
int pot = 0;

void setup()
{
  pinMode(pin_LED, OUTPUT);
}
 
void loop()
{
  int temp = map(analogRead(pot), 0, 1023, 0, 255);
  analogWrite(pin_LED, temp);
  delay(10);
}


RGB LED


What is an RGB LED and how does it work?


An RGB LED is basically an LED package that can produce almost any color. It can be used in different applications such as outdoor decoration lighting, stage lighting designs, home decoration lighting, LED matrix display, and more.

RGB LEDs have three internal LEDs (Red, Green, and Blue) that can be combined to produce almost any color output. In order to produce different kinds of colors, we need to set the intensity of each internal LED and combine the three color outputs. In this tutorial, we are going to use PWM to adjust the intensity of the red, green, and blue LEDs individually and the trick here is that our eyes will see the combination of the colors, instead of the individual colors because the LEDs are very close to each other inside.



RGB LED Types and Structure:


As mentioned earlier, RGB LEDs have three LEDs inside them and usually, these three internal LEDs share either a common anode or a common cathode especially in a through-hole package. So basically, we can categorize RGB LEDs as either common anode or common cathode type just like in seven segment displays.

When you look at an RGB LED, you'll see that it has four leads. If you face it so that its longest lead is second from the left, the leads should be in the following order: red, anode or cathode, green, and blue.


  • Common Anode

In a common anode RGB LED, the anode of the internal LEDs are all connected to the external anode lead. To control each color, you need to apply a LOW signal or ground to the red, green, and blue leads and connect the anode lead to the positive terminal of the power supply.


Common Anode RGB



  • Common Cathode

In a common cathode RGB LED, the cathode of the internal LEDs are all connected to the external cathode lead. To control each color, you need to apply a HIGH signal or VCC to the red, green, and blue leads and connect the anode lead to the negative terminal of the power supply.


Common Cathode RGB



Setting the color of an RGB LED using an Arduino Uno


In order to get the color that we want from an RGB LED, we need to set the intensity of each internal LED. To do this, we can either use constant current reduction (CCR) or pulse width modulation (PWM). But in this example, we're going to use PWM since we are using an Arduino Uno to set the color of the RGB LED.



Parts Used:

  1. Arduino Uno

  2. Common Cathode RGB LED

  3. 3 x 100Ω Resistors

  4. 3 x 1kΩ Potentiometers/Trimmers

  5. Jumper Wires


The setup is very easy. We have here a common cathode RGB LED, an Arduino Uno board, 3 potentiometers/trimmers, and 3 resistors. They're all connected through the jumper wires that we have. Basically, what happens here is that we have 3 potentiometers/trimmers connected to the A0, A1, and A2 ADC channels of the Arduino Uno. The ADC of the Arduino Uno reads the analog voltage across the wiper terminal of the potentiometers/trimmers and based on that voltage, the Arduino Uno adjusts the duty cycle of the PWM signals generated at the PWM pins D9, D10, and D11. As you can see in the image above, the D9 pin controls the intensity of the red LED of the RGB LED. The D10 pin controls the intensity of the green LED and D11 controls the intensity of the blue LED. The cathode pin of the RGB LED is connected to the GND pin of the Arduino Uno. Note: In this example, we just used the same resistance value for the RGB LED series resistors. But if you have access to the datasheet of the RGB LED that you're using, check the forward voltages of internal LEDs and from that, you can calculate the right resistance of the resistors that you're going to use. That way, you can have a balance between the brightness of the internal LEDs.



Schematic Diagram


We have two schematic diagrams here, one is for the common anode wiring and the other one is for the common cathode. In our example, we are using the wiring for the common cathode RGB LED. But they're almost the same. The only difference is that the second lead of the RGB LED from the left is connected to the 5V pin of the Arduino Uno for the common anode while for the common cathode, the second lead is connected to GND.



Wiring for Common AnodeRGB LED



Schematic Diagram for Common AnodeRGB LED




Wiring for Common CathodeRGB LED



Schematic Diagram for Common CathodeRGB LED



Note: You need to change some parts of the code/sketch below if you're using a common anode RGB LED.



Arduino Code/Sketch

int RGBLED_RedPin = 9;
int RGBLED_GreenPin = 10;
int RGBLED_BluePin = 11; 
int Previous_PWMValue_RedPin = 0;
int Threshold_PWMValue_RedPin = 0;
int Previous_PWMValue_GreenPin = 0;
int Threshold_PWMValue_GreenPin = 0;
int Previous_PWMValue_BluePin = 0;
int Threshold_PWMValue_BluePin = 0;

void setup() 
{ Serial.begin(9600); // initialize serial communications at 9600 bps
pinMode(RGBLED_RedPin, OUTPUT);
pinMode(RGBLED_GreenPin, OUTPUT);
pinMode(RGBLED_BluePin, OUTPUT);
}

void loop() 
{ int Pot_RedPin = analogRead(A0);
 int PWMValue_RedPin = Pot_RedPin/4;
 int Pot_GreenPin = analogRead(A1);
 int PWMValue_GreenPin = Pot_GreenPin/4;
 int Pot_BluePin = analogRead(A2);
 int PWMValue_BluePin = Pot_BluePin/4;

//Use this code for RGB LED COMMON CATHODE and comment the code for COMMON ANODE
analogWrite(RGBLED_RedPin, PWMValue_RedPin);
analogWrite(RGBLED_GreenPin, PWMValue_GreenPin);
analogWrite(RGBLED_BluePin, PWMValue_BluePin);

//Use this code for RGB LED COMMON ANODE and comment the code for COMMON CATHODE
/*
analogWrite(RGBLED_RedPin, 255-PWMValue_RedPin);
analogWrite(RGBLED_GreenPin, 255-PWMValue_GreenPin);
analogWrite(RGBLED_BluePin, 255-PWMValue_BluePin);
 */

//This section is for serial printing the RGB decimal values. 
//Sometimes you get unstable output due to jumper wires not properly connected, poor quality breadboard, or poor quality potentiometers.
Threshold_PWMValue_RedPin = abs(Pot_RedPin - Previous_PWMValue_RedPin);
Threshold_PWMValue_GreenPin = abs(Pot_GreenPin - Previous_PWMValue_GreenPin);
Threshold_PWMValue_BluePin = abs(Pot_BluePin - Previous_PWMValue_BluePin);
 if (Threshold_PWMValue_RedPin>= 10 || Threshold_PWMValue_GreenPin>= 10 || Threshold_PWMValue_BluePin>= 10)

Serial.print("RGB (");
Serial.print(PWMValue_RedPin);
Serial.print(", ");
Serial.print(PWMValue_GreenPin);
Serial.print(", ");
Serial.print(PWMValue_BluePin);
Serial.println(") ");
Previous_PWMValue_RedPin = Pot_RedPin;
Previous_PWMValue_GreenPin = Pot_GreenPin;
Previous_PWMValue_BluePin = Pot_BluePin;
//This section is for serial printing the RGB decimal values. 
//Sometimes you get unstable output due to jumper wires not properly connected, poor quality breadboard, or poor quality potentiometers.

delay(2);
}



RGB LED interface using arduino:


Schematic:


Code:

#define RED 6
#define GREEN 5
#define BLUE 3

void setup()
{
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  
  digitalWrite(RED, LOW);
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);
}

void loop()
{
  digitalWrite(RED, HIGH);
  delay(3000);
  digitalWrite(RED, LOW);
  delay(500);
  digitalWrite(GREEN, HIGH);
  delay(3000);
  digitalWrite(GREEN, LOW);
  delay(500);
  digitalWrite(BLUE, HIGH);
  delay(3000);
  digitalWrite(BLUE, LOW);
  delay(500);
}



Controlling RGB LED using potentiometer:


Schematic:


Code:

static int red = 11;
static int blue = 10;
static int green = 9;

static int potenRed = 14;
static int potenBlue = 15;
static int potenGreen = 16;


void setup(){
  pinMode(red, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(green, OUTPUT);
}


void loop(){
  int valRed = map(analogRead(potenRed), 0, 1023, 0, 255);
  int valBlue = map(analogRead(potenBlue), 0, 1023, 0, 255);
  int valGreen = map(analogRead(potenGreen), 0, 1023, 0, 255);
 
  analogWrite(red, valRed);
  analogWrite(blue, valBlue);
  analogWrite(green, valGreen);
  
  delay(100);
}



Check out the session on our youtube:






Written by-


Palkin Gupta

University Institute of Engineering and Technology, Panjab University





199 views0 comments

Recent Posts

See All

Comments


bottom of page