Step 2: Connection From LED To Arduino Mega 2560:
LED 1: RED Color:
- Positive pin to the 13.
- Negative pin to the GND.
LED 2: GREEN Color:
- Positive pin to the 12.
- Negative pin to the GND.
LED 3: BLUE Color:
- Positive pin to the 11.
- Negative pin to the GND.
Programming:
- int redPin=13;
- int greenPin=12;
- int bluePin=11;
-
- int S2=03;
- int S3=04;
- int outPin=05;
-
- int rColorStrength;
- int gColorStrength;
- int bcolorStrength;
-
- unsigned int pulseWidth;
-
- void setup() {
- Serial.begin (9600);
-
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
-
- pinMode(S2, OUTPUT);
- pinMode(S3, OUTPUT);
- pinMode(outPin, INPUT);
-
- }
-
- void loop() {
-
-
-
- digitalWrite(S2,LOW);
- digitalWrite(S3,LOW);
-
- pulseWidth = pulseIn(outPin,LOW);
-
- rColorStrength = pulseWidth/400. -1;
-
- rColorStrength = (255- rColorStrength);
-
-
-
-
- digitalWrite (S2,HIGH);
- digitalWrite (S3,HIGH);
-
- pulseWidth = pulseIn(outPin,LOW);
-
- gColorStrength = pulseWidth/400. -1;
-
- gColorStrength = (255- gColorStrength);
-
-
-
-
- digitalWrite(S2,LOW);
- digitalWrite(S3,HIGH);
-
- pulseWidth = pulseIn(outPin,LOW);
-
- bcolorStrength = pulseWidth/400. -1;
-
- bcolorStrength = (255- bcolorStrength);
-
- Serial.print(rColorStrength);
- Serial.print(“, “);
- Serial.print(gColorStrength);
- Serial.print(“, “);
- Serial.println(bcolorStrength);
- Serial.println(“ ”);
- if (rcolorstrength>bcolorstrength &&rcolorstrength>gcolorstrength){
- digitalwrite(redpin,HIGH);
- digitalwrite(bluepin,LOW);
- digitalwrite(greenpin,LOW);
- }
- if (gcolorstrength>bcolorstrength &&gcolorstrength>rcolorstrength){
- digitalwrite(redpin,LOW);
- digitalwrite(bluepin,LOW);
- digitalwrite(greenpin,HIGH);
- }
- if (bcolorstrength>rcolorstrength &&bcolorstrength>gcolorstrength){
- digitalwrite(redpin,LOW);
- digitalwrite(bluepin,HIGH);
- digitalwrite(greenpin,LOW);
- }
- delay(250);
- }
Explanation:
- In this article I have explained about displaying RGB color in the Arduinomega2560.
- It changes when the color is shown near the sensor and reflected in the LED.
- The color value can be seen in the serial monitor.
- For red, it will turn ON the red LED color.
- For blue, it will turn ON the blue LED color.
- For green, it will turn ON the green LED color.
Read more articles on Arduino: