Lesson 6 RGB LED

Share for us

Introduction

RGB LEDs can emit various colors of light. They are manufactured by packaging three LEDs of red, green, and blue into a transparent or semitransparent plastic shell and lead out four pins. The three primary colors of red, green, and blue can be mixed to all kinds of colors by brightness, so you can make RGB LEDs emit light with all kinds of colors by controlling the circuit.

Components

-1*Raspberry Pi

– 1*Network cable(or USB wireless network adapter)

– 1*RGB LED

– 3*Resistor(220Ω)

– Several jumper wires

Experimental Principle

The three primary color red, green and blue of  a RGB LED can compose various colors by brightness. We can adjust the brightness of LED with PWM technology. Raspberry Pi has only one channel hardware PWM output, but we need three channels to control the RGB LED. As a result, it is difficult to realize with the hardware PWM of Raspberry Pi. Do not worry! Fortunately the softPwm library simulates PWM (softPwm) for us with software method. Based on this, we only need to include the header file softPwm.h, then call the API it provided to easily achieve multi-channel PWM output to control the RGB LED to display all kinds of colors.

There are two types of package for RGB LEDs. One is patch type, and the other is dual-in-line type. The difference between them is color resolution. The former has better color resolution.

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 1Connect the circuit as shown in the following diagram

Step 2Edit and save the code with vim (see path/Rpi_BasicKit/06_rgb/rgb.c)

Step 3Compile the code         

gcc  rgb.c  -lwiringPi  -lpthread

Step 4:Run the program

 ./a.out

Press Enter, you will see the RGB LED light up, and display different colors in turn.

Further Exploration

I hope you can modify the parameters of the function ledColorSet( ) by yourself, then compile and run the program to see the color changes of the RGB LED.

Experimental Summary

You have learnt how to control RGB LEDs with softPwm of the Raspberry Pi through this lesson. I hope you can continue to explore softPwm application in DC motor speed regulation.

C Code

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

#define uchar unsigned char

#define LedPinRed 0
#define LedPinGreen 1
#define LedPinBlue 2

void ledInit(void)

{
softPwmCreate(LedPinRed, 0, 100);
softPwmCreate(LedPinGreen,0, 100);
softPwmCreate(LedPinBlue, 0, 100);
}

void ledColorSet(uchar r_val, uchar g_val, uchar b_val)

{
softPwmWrite(LedPinRed, r_val);
softPwmWrite(LedPinGreen, g_val);
softPwmWrite(LedPinBlue, b_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,0x00); //red
delay(500);
ledColorSet(0x00,0xff,0x00); //green
delay(500);
ledColorSet(0x00,0x00,0xff); //blue
delay(500);

ledColorSet(0xff,0xff,0x00); //yellow
delay(500);
ledColorSet(0xff,0x00,0xff); //pick
delay(500);
ledColorSet(0xc0,0xff,0x3e);
delay(500);

ledColorSet(0x94,0x00,0xd3);
delay(500);
ledColorSet(0x76,0xee,0x00);
delay(500);
ledColorSet(0x00,0xc5,0xcd);
delay(500);

}

return 0;
}