Tri-color LED module made of a full-color LED, through the PWM voltage input of the three pins R, G, B can adjust the intensity of the three base colors (red/blue/green) so as to achieve the full-color color mixing effect. R, G, B correspond to the control of red, green, blue three LED pins.
Experimental purpose
LED to achieve from green to red, from red to blue, from blue to green.
Equipment
Four-legged three-color LED light module, breadboard, Stduino Uno/Nano, DuPont wire
Circuit connection
The LED tricolor light module generally has four pins, of which "-" is connected to GND.
The other three are connected to the pins on the Stduino Uno board that are capable of PWM output (i.e. the pins with wavy lines after the pin number).
Here A4 is connected to the red LED pins, A5 to the green LED pins, and A6 to the blue LED pins.
code
Code: Select all
int RedLED = A4;
int GreenLED =A5;
int BlueLED = A6;
void setup()
{
pinMode(RedLED,OUTPUT);
pinMode(GreenLED,OUTPUT);
pinMode(BlueLED,OUTPUT);
}
void setColor(int red,int green,int blue)
{
analogWrite(RedLED,red);
analogWrite(GreenLED,green);
analogWrite(BlueLED,blue);
}
void loop()
{
int i,j;
//Green to red gradient
for(i=0,j=255;i<256;i++)
{
setColor(i,j,0);
delay(4);
j--;
}
//Red to blue gradient
for(i=0,j=255;i<256;i++)
{
setColor(j,0,i);
delay(4);
j--;
}
//Blue to green gradient
for(i=0,j=255;i<256;i++)
{
setColor(0,i,j);
delay(4);
j--;
}
}