Color control for RGB LEDs with Raspberry Pi--c language version number

Source: Internet
Author: User

Color control for RGB LEDs with Raspberry Pi

    RGBColor Modeis a color standard for industry. is through theRed (R),Green (G),Blue (B)Three ColorsChanneland they overlap each other to get a variety of colors, RGB is the representativeRed,Green,Bluethree channels of color. This standard almost contains all the colors that human vision can perceive, and is now the most widely usedColor System one of them. The RGB color mode uses the RGB model to assign an intensity value within a 0~255 range for the RGB component of each pixel in the image .

RGB images use only three colors. So that they can be mixed in different proportions to get a variety of colors.

in the actual control. The control of the LED brightness (color depth) is often achieved by PWM.

The Raspberry Pi has only one wayHardware PWM output (GPIO1), but to achieve control of the RGB LEDs. Requires 3-way PWM. In fact. The WIRINGPI Library provides us with a PWM output that is implemented with software multithreading , and we are able to use the functions provided by this library to conveniently place arbitrary gpio configured as a PWM output. In this example. I will gpio0,gpio1. The GPIO2 is configured as a soft PWM output. The PIN assignment table for the Raspberry Pi is for example with what you see:


The RGB LEDs I use are common cathode, with the Raspberry Pi connection method such as the following:

Raspberry Pi RGB LED module

GPIO0 -------------------------------------- /c6>R

GPIO1 ------------------------------------- G

                    and nbsp                 gpio2   --------------------------------------   B

GND ---------------------------------------- /c4>'-'

The physical map is for example the following:


Source:

#include <wiringPi.h> #include <softPwm.h> #include <stdio.h> #define UCHAR unsigned char#define ledpinred 0#define ledpingreen 1#define ledpinblue 2void ledinit (void) {softpwmcreate (ledpinred, 0, +); SoftPwmCrea Te (ledpingreen,0, +); 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 message to screenprintf ("Setup WIRINGPI failed! "); return 1;   }ledinit (); while (1) {ledcolorset (0xff,0x00,0x00);   Reddelay (+); Ledcolorset (0x00,0xff,0x00);   Greendelay (+); Ledcolorset (0X00,0X00,0XFF);   Bluedelay (+); Ledcolorset (0xff,0xff,0x00);   Yellowdelay (+); Ledcolorset (0XFF,0X00,0XFF); Pickdelay, Ledcolorset (0xc0,0xff,0x3e);d Elay, Ledcolorset (0x94,0x00,0xd3);d Elay, Ledcolorset ( 0x76,0xee,0x00);d Elay, Ledcolorset (0x00,0xc5, 0xCD);d Elay (500);} return 0;}

Save this code as RGB.C.

Compile Code:

gcc rgb.c-o rgb-lwiringpi-lpthread

Execute code:

./rgb

Attention:

1,-LWIRINGPI option: Indicates the link to the WIRINGPI library, as the implementation of SOFTPWM is in this library;

2. -lpthread option: Because of the implementation of the SOFTPWM Linux multithreading mechanism. So add this compilation option.


The Code and demo video has been shared to 360 cloud disks:

Click I download code interview passworde0da

Click I download video Interview Password d6b1


The following is the improved code. Compiled in the same way.

#include <wiringPi.h> #include <softPwm.h> #include <stdio.h> #define ledpinred 0#define Ledpingreen 1 #define Ledpinblue 2int colors[] = {0xFF0000, 0x00FF00, 0x0000FF, 0xffff00, 0x00ffff, 0xff00ff, 0xFFFFFF, 0x9400d3};/*** A number is linearly mapped from one interval to another interval. For example, map a number between 0~100 to 0~255 ************************************************************************************** /int map (int x, int in_min, int in_max, int out_min, int out_max) {return (x-in_min) * (out_max-out_min)/(In_ma x-in_min) + out_min;}  void Ledinit (void) {softpwmcreate (ledpinred, 0, 100); Create a soft PWM, original duty cycle is 0Hz, range is 0~100 softpwmcreate (ledpingreen,0, +); Softpwmcreate (Ledpinblue , 0, 100);} void ledcolorset (int color)//set color, for Example:0xde3f47{int r_val, g_val, B_val;r_val = (color & 0xff000  0) >> 16;   Get red Valueg_val = (color & 0x00FF00) >> 8; Get Green Valueb_val = (color & 0x0000FF) >> 0;    Get Blue Valuer_val = map (r_val, 0, 255, 0, 100); Change a num (0~255) to 0~100g_val = map (g_val, 0, 255, 0, +), B_val = map (b_val, 0, 255, 0, +); Softpwmwrite (ledpinred  , 100-r_val); Change Duty Cyclesoftpwmwrite (Ledpingreen, 100-g_val); Softpwmwrite (Ledpinblue, 100-b_val);} int main (void) {int i;if (wiringpisetup () = =-1) {//when Initialize WIRINGPI failed, print message to screenprintf ("set Up Wiringpi failed!\n "); return 1; }ledinit (); while (1) {for (i = 0; i < sizeof (colors)/sizeof (int); i++) {Ledcolorset (colors[i]);d Elay (500);}} return 0;}

Let's say you want RGB control in the Python language, see another blog post " color control of RGB LEDs with Raspberry Pi--python version number "


Color control for RGB LEDs with Raspberry Pi--c language version number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.