• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeArduinoAdvanced Learning Kit for ArduinoProject 21: Tri-color LED RGB Module on Arduino
Previous Next

Project 21: Tri-color LED RGB Module on Arduino

Posted by: Alvin Jin , March 27, 2014

[vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Introduction” tab_id=”1395587944-1-93″][vc_column_text][bigcommerce link=”/triple-output-led-rgb/” target=”_blank”]  RGB LED has four pins: 1. R, red, 2. G, green , 3. B, blue, and 4, ground.

Three pins can be controlled by Arduino to achieve full-color effect.

[/vc_column_text][/vc_tab][vc_tab title=”Code” tab_id=”1395587944-2-8″][vc_column_text]

int ledPin = 13; / / LED is connected to digital pin 13
int redPin = 11; / / R petal on RGB LED module connected to digital pin 11
int greenPin = 9; / / G petal on RGB LED module connected to digital pin 9
int bluePin = 10; / / B petal on RGB LED module connected to digital pin 10
void setup ()
{
pinMode (ledPin, OUTPUT); / / sets the ledPin to be an output
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);
analogWrite (bluePin, 255-blue);
analogWrite (greenPin, 255-green);
}

[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: Advanced Learning Kit for Arduino

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors