Devices & Components
Arduino Leonardo with Headers
Resistor 220 ohm
Jumper wires (generic)
Breadboard (generic)
RGB Diffused Common Anode
Project description
Code
CA_RGB_LED
arduino
1// LEDs must be connected to arduino PWM pins - see board pinout 2#define 3 RED 6 // pin that red led is connected to 4#define GREEN 5 // pin that 5 green led is connected to 6#define BLUE 3 // pin that blue led is connected 7 to 8#define DELAY 20 // internal delay in ms 9 10void setup() { 11 12 // LED connection pins to be set as an output 13 pinMode(RED, OUTPUT); 14 pinMode(GREEN, 15 OUTPUT); 16 pinMode(BLUE, OUTPUT); 17} 18 19void loop() { 20 // fade from 21 red to green 22 for(int i=0; i<255; i++) { 23 analogWrite(RED, i); // red 24 initially ON 25 analogWrite(GREEN, 255-i); // green initially OFF 26 analogWrite(BLUE, 27 255); // blue OFF 28 delay(DELAY); // wait before next transition 29 } 30 31 32 // fade from green to blue 33 for(int i=0; i<255; i++) { 34 analogWrite(RED, 35 255); // red OFF 36 analogWrite(GREEN, i); // green initially ON 37 analogWrite(BLUE, 38 255-i); // blue initially OFF 39 delay(DELAY); 40 } 41 42 // fade from 43 blue to red 44 for(int i=0; i<255; i++) { 45 analogWrite(RED, 255-i); // red 46 initially OFF 47 analogWrite(GREEN, 255); // green OFF 48 analogWrite(BLUE, 49 i); // blue initially ON 50 delay(DELAY); 51 } 52} 53
Downloadable files
CA_RGB_LED
Breadboard diagram for LED circuit
CA_RGB_LED

CA_RGB_LED
Breadboard diagram for LED circuit
CA_RGB_LED

Comments
Only logged in users can leave comments