Using an RGB led we can generate any color by controlling the brightness of the individual Red, Green and Blue LEDs. The following image shows a few possible colors of light that can be generated using an RGB LED.
An important point is that you should make sure that you connect the Red, Green and Blue Anodes of the RGB LED to three PWM supported pins of Arduino since we are using a common cathode rgb led
Circuit Diagram:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | int redPin=11; int greenPin=10; int bluePin=9; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(redPin,OUTPUT); pinMode(greenPin,OUTPUT); pinMode(bluePin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: //red analogWrite(redPin,255); analogWrite(greenPin,0); analogWrite(bluePin,0); //green analogWrite(redPin,0); analogWrite(greenPin,255); analogWrite(bluePin,0); //blue analogWrite(redPin,0); analogWrite(greenPin,0); analogWrite(bluePin,255); //magenta analogWrite(redPin,255); analogWrite(greenPin,0); analogWrite(bluePin,255); //yellow analogWrite(redPin,255); analogWrite(greenPin,255); analogWrite(bluePin,0); //cyan analogWrite(redPin,0); analogWrite(greenPin,255); analogWrite(bluePin,255); //white analogWrite(redPin,255); analogWrite(greenPin,255); analogWrite(bluePin,255); } |
0 comments:
Post a Comment
Ask Something