Display RGB Light With Arduino Mega 2560

Introduction:
  • In this article I will explain about displaying RGB Light With Arduino Mega 2560.
  • It can be used to change according to the colored paper.
  • It can work under the color sensor. 
Parts Of Lists:
  • Arduino Mega 2560
  • LED-3
  • Color sensor
  • Hookup Wires
 
Color Sensor: 
  • The color sensor can be used to convert the light to frequency converter.
  • It can be used to filter the RGB color.
  • We will calculate the RGB color value.


                     Figure 1: Color sensor.
Connection:

Step 1: Connection From Color Sensor To Arduino Mega 2560:
  • Connect the S0 pin to the GND of the Arduino board. 
  • Connect the S1 pin to the VCC of the 5V  Arduino board.
  • Connect the S2 pin to the 03  of the Arduino board.
  • Connect the S3 pin to the 04 of the Arduino board.
  • Connect the S0 pin to the 05 of the Arduino board.
  • Connect the VCC pin to the 5V of the Arduino board.
  • Connect the GND pin to the GND of the Arduino board. 
Step 2: Connection From LED To Arduino Mega 2560:
 
LED 1 : RED Color:
  • Postive pin to the 13.
  • Negative pin to the GND. 
LED 2 : GREEN Color:
  • Postive pin to the 12.
  • Negative pin to the GND. 
LED 3 : BLUE Color:
  • Postive pin to the 11.
  • Negative pin to the GND. 
Programming:
  1. int redPin=13; // red ping  
  2. int greenPin=12; // green pin  
  3. int bluePin=11; // blue pin  
  4.   
  5. int S2=03; // Color sensore pin S2 to Arduino pin 7  
  6. int S3=04; // Color sensor pin S3 to Arduino pin 8  
  7. int outPin=05; // color Sensor OUT to Arduino pin 4  
  8.   
  9. int rColorStrength;  
  10. int gColorStrength;  
  11. int bcolorStrength;  
  12.   
  13. unsigned int pulseWidth;  
  14.   
  15. void setup() {  
  16. Serial.begin (9600); //Turn on serial port  
  17.   
  18. pinMode(redPin, OUTPUT);  
  19. pinMode(greenPin, OUTPUT);  
  20. pinMode(bluePin, OUTPUT);  
  21.   
  22. pinMode(S2, OUTPUT);  
  23. pinMode(S3, OUTPUT);  
  24. pinMode(outPin, INPUT);  
  25.   
  26. }  
  27.   
  28. void loop() {  
  29. // Lets star by reading Red component of the color  
  30. // S2 and S3 should be set LOW  
  31.   
  32. digitalWrite(S2,LOW);  
  33. digitalWrite(S3,LOW);  
  34.   
  35. pulseWidth = pulseIn(outPin,LOW);  
  36.   
  37. rColorStrength = pulseWidth/400. -1;  
  38.   
  39. rColorStrength = (255- rColorStrength);  
  40.   
  41. // Lets read green component of the color  
  42. // S2 and S3 should be set LOW  
  43.   
  44. digitalWrite (S2,HIGH);  
  45. digitalWrite (S3,HIGH);  
  46.   
  47. pulseWidth = pulseIn(outPin,LOW);  
  48.   
  49. gColorStrength = pulseWidth/400. -1;  
  50.   
  51. gColorStrength = (255- gColorStrength);  
  52.   
  53. // Lets read blue component of the color  
  54. // S2 and S3 should be set LOW and HIGH Repectively  
  55.   
  56. digitalWrite(S2,LOW);  
  57. digitalWrite(S3,HIGH);  
  58.   
  59. pulseWidth = pulseIn(outPin,LOW);  
  60.   
  61. bcolorStrength = pulseWidth/400. -1;  
  62.   
  63. bcolorStrength = (255- bcolorStrength);  
  64.   
  65. Serial.print(rColorStrength);  
  66. Serial.print(“, “);  
  67. Serial.print(gColorStrength);  
  68. Serial.print(“, “);  
  69. Serial.println(bcolorStrength);  
  70. Serial.println(“ ”);  
  71. if (rcolorstrength>bcolorstrength &&rcolorstrength>gcolorstrength){  
  72. digitalwrite(redpin,HIGH);  
  73. digitalwrite(bluepin,LOW);  
  74. digitalwrite(greenpin,LOW);  
  75. }  
  76. if (gcolorstrength>bcolorstrength &&gcolorstrength>rcolorstrength){  
  77. digitalwrite(redpin,LOW);  
  78. digitalwrite(bluepin,LOW);  
  79. digitalwrite(greenpin,HIGH);  
  80. }  
  81. if (bcolorstrength>rcolorstrength &&bcolorstrength>gcolorstrength){  
  82. digitalwrite(redpin,LOW);  
  83. digitalwrite(bluepin,HIGH);  
  84. digitalwrite(greenpin,LOW);  
  85. }  
  86. delay(250);  
  87. }  
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. 
Output:

  
                                  Figure 2: Output
 
Read more articles on Arduino:

Up Next
    Ebook Download
    View all
    Learn
    View all