1. Dual Color LED

../_images/dualled.jpg

A dual color LED is composed of two LEDs each emitting a different color of light. Two types of dual color LEDs can be found: common anode and common cathode.

As a reminder on LEDs, to turn a normal LED on, the anode (longer leg) is linked to the VCC and the cathode (shorter leg) is linked to the ground.

In the case of a dual color LED, the internal LEDs share their anode or cathode through one pin: the middle pin. So the wiring depends on whether it’s a common anode or common cathode.

Below are the internal connections of a common anode and of a common cathode dual color LED to understand better the placement of the colored LEDs:

Common Cathode:

../_images/common-cathode.jpg

Common Anode:

../_images/common-anode.jpg

In this example, we will be using a common anode composed of a green and a red LED.

1.1. Circuit Connection

As we mentioned previously, this is a common anode dual color LED. So to light both the green and the red internal LEDs, connect:

  1. The middle pin to 3.3V
  2. The right and left pin to a ground
../_images/dualled-on.jpg

However in this example, we want to control the intensity and the color of our LED, so connect:

  1. The middle pin to 3.3V (pin 1)
  2. The right pin to a GPIO (pin 11)
  3. The left pin to a GPIO (pin 12)
../_images/dualled-pwm.jpg

1.2. Python program to change the color of the LED

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import RPi.GPIO as GPIO
import time
pins = (11,12)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pins, GPIO.OUT)
GPIO.output(pins, GPIO.HIGH)

#We use pwm in this case to change the intensity of each internal LED
red = GPIO.PWM(pins[0], 2000)
green = GPIO.PWM(pins[1], 2000)

#We are lighting the red LED only at the beginning. To do that, we give an ouput of 0V for the cathode of the red LED since the anode pin has a stable 3.3V

red.start(0)
green.start(100)

try:
    while True:
        for i in range(0,125,25): #The ChangeDutyCycle of i takes a value of 0, 25, 50, 75 and 100

            red.ChangeDutyCycle(i)
            green.ChangeDutyCycle(100-i)#The intensity of the red LED decreases while the intensity of the green LED increases passing through and orange light (mixture of red and green) and ending up with a green LED

            time.sleep(0.5)
        
except KeyboardInterrupt:
    GPIO.cleanup()

After saving the code, you can run it by clicking on Run (or CTRL+F5).

You will then see the LED transition between multiple stages.

Here are three stages of the LED:

i=0 - Internal red LED on

../_images/dualled-0.jpg

i=50 - Internal red and green LEDs equally on

../_images/dualled-50.jpg

i=100 - Internal green LED on

../_images/dualled-100.jpg