Site Tools


Sidebar

Content

pub:rgb_led

RGB_LED

what's RGB_LED?

The RGB LED consists of three LEDs. Each LED has a red, a green and a blue light. The three-color LEDs can produce any color; it contains the red, green and blue emitters, typically with a four-wire connection and a common wire (anode or cathode). These LEDs can have a common anode or a common cathode wire.


We used the common anode RGB LED in this experiment. Made it into a module, from left to right we can see the common anode, R, G, B, where R, G, B are connected respectively with three SMD resistors. The longest pin is the common anode of the three LEDs.


R Red channel
G Green channel
B Blue channel
+ 3.3V



Microbit

Components
1 * microbit
1 * microbit expansion board
1 * USB cable
1 * RGB LED module
- Several Jumper wires

connection diagram:


Code:
Makecode:

python code

#-----------------------------------------------------------
# File name   : RGB.py
# Description : RGB light switch colors.
# Author      : jason
# E-mail      : jason@adeept.com
# Website     : www.adeept.com
# Date        : 2018/01/17
#-----------------------------------------------------------
from microbit import *
 
 
def set_rgb(red, green, blue):
    pin0.write_analog(red)
    pin1.write_analog(green)
    pin2.write_analog(blue)
 
 
while True:
    set_rgb(1023, 0, 0)
    sleep(1000)
    set_rgb(0, 1023, 0)
    sleep(1000)
    set_rgb(0, 0, 1023)
    sleep(2000)

Click “flash” and download the code onto the micro:bit. Now we can see that the RGB LED is changing colors.
Effect Picture:



Arduino

Components
- 1 * Adeept Arduino UNO R3 Board
- 1 * RGB LED Module
- 1 * USB Cable
- 1 * 4-Pin Wires

Step 1: Build the circuit


Adeept UNO R3 Board RGB LED Module
D9 B
D10 G
D11 R
5V +

Step 2: Code

/***********************************************************
File name: _03_RGBLEDModule.ino
Description:Control the RGB LED emitting red, green, blue, yellow,
            white and purple light, then the RGB LED will be off,
            each state continues 1s, after repeating the above 
            procedure.   
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/01/17 
*************************************************************/
int redPin = 11;   // R petal on RGB LED module connected to digital pin 11 
int greenPin = 10; // G petal on RGB LED module connected to digital pin 9 
int bluePin = 9;   // B petal on RGB LED module connected to digital pin 10 
void setup()    
{   
   pinMode(redPin, OUTPUT);   // sets the redPin to be an output 
   pinMode(greenPin, OUTPUT); // sets the greenPin to be an output 
   pinMode(bluePin, OUTPUT);  // sets the bluePin to be an output 
}    
void loop()  // run over and over again  
{    
    // Basic colors:  
    color(255, 0, 0); // turn the RGB LED red  
    delay(1000);      // delay for 1 second  
    color(0,255, 0);  // turn the RGB LED green  
    delay(1000);      // delay for 1 second  
    color(0, 0, 255); // turn the RGB LED blue
    delay(1000);      // delay for 1 second  
 
    // Example blended colors:  
    color(255,255,0);   // turn the RGB LED yellow   
    delay(1000);        // delay for 1 second  
    color(255,255,255); // turn the RGB LED white  
    delay(1000);        // delay for 1 second  
    color(128,0,255);   // turn the RGB LED purple  
     delay(1000);       // delay for 1 second  
     color(0,0,0);      // turn the RGB LED off  
     delay(1000);       // delay for 1 second  
}     
 
void color (unsigned char red, unsigned char green, unsigned char blue)// the color generating function  
{    
     analogWrite(redPin, 255-red);     // PWM signal output   
     analogWrite(greenPin, 255-green); // PWM signal output
     analogWrite(bluePin, 255-blue);   // PWM signal output
}

Step 3: Compile and download the sketch to the UNO R3 board.



Now you can see the RGB LED flash different colors alternately.



Raspberry pi

Components
- 1 * Raspberry Pi
- 1 * GPIO Extension Board
- 1 * 40-Pin GPIO Cable
- 1 * Breadboard
- 1 * RGB LED Module
- 1 * 4-Pin Wires

Step 1: Build the circuit


Code:
C code

/*
 * File name   : rgbLed.c
 * Description : control a rgb led 
 * Website     : www.adeept.com
 * E-mail      : support@adeept.com
 * Author      : Jason
 * Date        : 2019/01/17
 */
#include <wiringPi.h>  
#include <softPwm.h>  
#include <stdio.h>  
 
#define LedPinRed    0  
#define LedPinGreen  1  
#define LedPinBlue   2  
 
const int colors[] = {0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF, 0xFFFFFF, 0x9400D3};  
 
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_max - 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, 100);  
	softPwmCreate(LedPinBlue, 0, 100);  
}  
 
void ledColorSet(int color)        //set color, for example: 0xde3f47  
{  
	int r_val, g_val, b_val;  
 
	r_val = (color & 0xFF0000) >> 16;  //get red value  
	g_val = (color & 0x00FF00) >> 8;   //get green value  
	b_val = (color & 0x0000FF) >> 0;   //get blue value  
 
	r_val = map(r_val, 0, 255, 0, 100);    //change a num(0~255) to 0~100  
	g_val = map(g_val, 0, 255, 0, 100);  
	b_val = map(b_val, 0, 255, 0, 100);  
 
	softPwmWrite(LedPinRed,   100 - r_val);  //change duty cycle  
	softPwmWrite(LedPinGreen, 100 - g_val);  
	softPwmWrite(LedPinBlue,  100 - b_val);  
}  
 
int main(void)  
{  
	int i;  
 
	if(wiringPiSetup() < 0){ //when initialize wiringPi failed, print message to screen  
		printf("setup wiringPi failed !\n");  
		return -1;   
	}  
 
	ledInit();  
 
	while(1){  
		for(i = 0; i < sizeof(colors)/sizeof(int); i++){  
			ledColorSet(colors[i]);  
			delay(500);  
		}  
	}  
 
	return 0;  
}

python code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
 
colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF]
pins = {'pin_R':11, 'pin_G':12, 'pin_B':13}  # pins is a dict
 
GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
for i in pins:
	GPIO.setup(pins[i], GPIO.OUT)   # Set pins' mode is output
	GPIO.output(pins[i], GPIO.HIGH) # Set pins to high(+3.3V) to off led
 
p_R = GPIO.PWM(pins['pin_R'], 2000)  # set Frequece to 2KHz
p_G = GPIO.PWM(pins['pin_G'], 2000)
p_B = GPIO.PWM(pins['pin_B'], 5000)
 
p_R.start(0)      # Initial duty Cycle = 0(leds off)
p_G.start(0)
p_B.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 = 0x112233
	R_val = (col & 0x110000) >> 16
	G_val = (col & 0x001100) >> 8
	B_val = (col & 0x000011) >> 0
 
	R_val = map(R_val, 0, 255, 0, 100)
	G_val = map(G_val, 0, 255, 0, 100)
	B_val = map(B_val, 0, 255, 0, 100)
 
	p_R.ChangeDutyCycle(R_val)     # Change duty cycle
	p_G.ChangeDutyCycle(G_val)
	p_B.ChangeDutyCycle(B_val)
 
try:
	while True:
		for col in colors:
			setColor(col)
			time.sleep(0.5)
except KeyboardInterrupt:
	p_R.stop()
	p_G.stop()
	p_B.stop()
	for i in pins:
		GPIO.output(pins[i], GPIO.HIGH)    # Turn off all leds
	GPIO.cleanup()

For C language users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_C_Code/03_rgbLed/rgbLed.c)

Step 3: Compile

$ sudo gcc rgbLed.c -o rgbLed -lwiringPi -lpthread

Step 4: Run

$ sudo ./rgbLed

For Python users:

Step 2: Edit and save the code with vim or nano.

(code path: /home/Adeept_Sensor_Kit_for_RPi_Python_Code/03_rgbLed.py)

Step 3: Run

$ sudo ./03_rgbLed.py
 

Now you can see the RGB LED flash different colors alternately.


Link for code download:http://www.adeept.com/learn/ Download the kit information as needed.
Video link:http://www.adeept.com/video/

pub/rgb_led.txt · Last modified: 2019/01/17 13:51 by Adeept