Example Code for Arduino-RGB LED
Last revision 2026/01/08
This article provides example code and guidance for setting up and programming an Arduino RGB LED, including hardware and software preparation, wiring diagrams, and sample code to create dynamic color-changing effects.
Wiring Diagram

Sample Code
/*
RGB LED
*/
int redPin = 9; // the pin that the red LED is attached to
int greenPin = 10; // the pin that the green LED is attached to
int bluePin = 11; // the pin that the blue LED is attached to
void setup(){
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop(){
// call the function to change the colors of LED randomly.
colorRGB(random(0,255),random(0,255),random(0,255)); //R:0-255 G:0-255 B:0-255
delay(1000);
}
void colorRGB(int red, int green, int blue){
analogWrite(redPin,constrain(red,0,255));
analogWrite(greenPin,constrain(green,0,255));
analogWrite(bluePin,constrain(blue,0,255));
}
Was this article helpful?
