How to use Raspberry Pi to drive a Bi-Color LED

Background

A Bi-color LED is the kind of light that capable of emitting two different colors, typically red and green, rather than only one color. It is housed in a 3mm or 5mm epoxy package. It has 3 leads. Common cathode or common anode is available for two LED terminals or pins, arranged in the circuit in anti-parallel and connected by a cathode/anode. Positive voltage can be directed towards one of the LED terminals, causing that terminal to emit light of the corresponding color; when the direction of the voltage is reversed, the light of the other color is emitted. In a Bi-color LED, only one of the pins can receive voltage at a time.

As a result, this type of LED mostly be used as an indicator lights for a variety of devices, like televisions, digital cameras, and remote controls.

Project Principle

Connect pin R and G to GPIOs of Raspberry Pi, program the Raspberry Pi to change the color of the LED from red to green, and then use PWM to mix into other colors.

What you need

  • 1x Raspberry Pi
  • 1x Breadboard
  • 4 x Jumper wires
  • 1 x Network cable (or USB wireless network adapter)
  • 1 x Dual-color LED module
  • 1 x 3-Pin anti-reverse cable

Step 1: Connect the circuit

circuit table

Raspberry Pi T-Cobbler Dual-Color LED Module
GPIO0 GPIO17 R
GND GND GND
GPIO1 GPIO18 G

Step 2: Run the C/Python programm

Create or upload the C programm to your Raspberry Pi and enter the path, Please refer the below C source code for reference.

In our case, I create the C file as 01_dule_color_led at /home/pi/projects/ 01_dule_color_led/
cd /home/pi/projects/01_dule_color_led/

Compile it,
gcc dule_color_led.c -o dual_color_led –lwiringPi -lpthread

Run it,
sudo ./dual_color_led

For the Python program, you dont need complie it, but just run it. below steps,

Enter the directory which stored your python code, please refere the attached python source code for your reference.
cd /home/pi/projects/01_dule_color_led/

Run the Python code,
sudo python 01_dule_color_led.py

Application Show

When running the C or python program for this project, You will see the dual-color LED render green, red, and mixed colors.

Appendix

C source code: dule_color_led.c

#include <wiringPi.h>
#include <softPwm.h>
#include <stdio.h>

#define uchar unsigned char

#define LedPinRed    0
#define LedPinGreen  1

void ledInit(void)
{
	softPwmCreate(LedPinRed,  0, 100);
	softPwmCreate(LedPinGreen,0, 100);
}

void ledColorSet(uchar r_val, uchar g_val)
{
	softPwmWrite(LedPinRed,   r_val);
	softPwmWrite(LedPinGreen, g_val);
}

int main(void)
{
	int i;

	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}
	//printf("linker LedPin : GPIO %d(wiringPi pin)\n",LedPin); //when initialize wiring successfully,print message to screen

	ledInit();

	while(1){
		ledColorSet(0xff,0x00);   //red	
		delay(500);
		ledColorSet(0x00,0xff);   //green
		delay(500);
		ledColorSet(0xff,0x45);	
		delay(500);
		ledColorSet(0xff,0xff);	
		delay(500);
		ledColorSet(0x7c,0xfc);	
		delay(500);
	}

	return 0;
}

Python source code: 01_dule_color_led.py

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

colors = [0xFF00, 0x00FF, 0x0FF0, 0xF00F]
pins = (11, 12)  # pins is a dict

GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
GPIO.setup(pins, GPIO.OUT)   # Set pins' mode is output
GPIO.output(pins, GPIO.LOW)  # Set pins to LOW(0V) to off led

p_R = GPIO.PWM(pins[0], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins[1], 2000)

p_R.start(0)      # Initial duty Cycle = 0(leds off)
p_G.start(0)

def map(x, in_min, in_max, out_min, out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def setColor(col):   # For example : col = 0x1122
	R_val = col  >> 8
	G_val = col & 0x00FF
	
	R_val = map(R_val, 0, 255, 0, 100)
	G_val = map(G_val, 0, 255, 0, 100)
	
	p_R.ChangeDutyCycle(R_val)     # Change duty cycle
	p_G.ChangeDutyCycle(G_val)

def loop():
	while True:
		for col in colors:
			setColor(col)
			time.sleep(0.5)

def destroy():
	p_R.stop()
	p_G.stop()
	GPIO.output(pins, GPIO.LOW)    # Turn off all leds
	GPIO.cleanup()

if __name__ == "__main__":
	try:
		loop()
	except KeyboardInterrupt:
		destroy()

About the author

Leave a Reply

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