Defining colors with Arduino

The example shows how we can detect the color using the module color sensor in combination with the Arduino microcontroller.

 

In this example we used the following elements:

 

Connect the sensor with Arduino:

Connect to Arduino as follows:

 

The module TCS3200D Pin Arduino
VCC 5 V
GND GND
OUT 2
S0 3
S1 4
S2 5
S3 6

 

 

 

Diagram of connecting the module to Arduino Uno.

 

Program for Arduino

In the example, we used the following code:

 

  1. int s0 = 3, s1 = 4, s2 = 5, s3 = 6; //pins of the module in combination with Arduino
  2. int out = 2; //Arduino pin 2 and OUT from the sensor
  3. int flag = 0;
  4. byte counter = 0;
  5. byte countR = 0, countG = 0, countB = 0;
  6. void setup()
  7. {
  8. Serial.begin(9600); //initialize serial monitor
  9. pinMode(s0, OUTPUT); //setting the pins as output
  10. pinMode(s1, OUTPUT);
  11. pinMode(s2, OUTPUT);
  12. pinMode(s3, OUTPUT);
  13.  
  14. }
  15. void TCS()
  16. {
  17. flag = 0;
  18. digitalWrite(s1, HIGH);
  19. digitalWrite(s0, HIGH);
  20. digitalWrite(s2, LOW);
  21. digitalWrite(s3, LOW);
  22. attachInterrupt(0, ISR_INTO, LOW);
  23. timer0_init();
  24.  
  25. }
  26. void ISR_INTO()
  27. {
  28. counter++;
  29. }
  30. void timer0_init(void)
  31. {
  32. TCCR2A = 0x00;
  33. TCCR2B = 0x07; //clock speed - 1024
  34. TCNT2 = 100; //10 MS overflow
  35. TIMSK2 = 0x01; //enable interrupts
  36. }
  37. int i = 0;
  38. ISR(TIMER2_OVF_vect) //the timer 2, 10ms overflow 10 MS. the function of the interrupt overflow wewnętrzengo
  39. {
  40. TCNT2 = 100;
  41. flag++;
  42. if (flag == 1) //load and display RGB colors
  43. {
  44. countR = counter;
  45. Serial.print("red=");
  46. Serial.print(countR, DEC);
  47. Serial.print("TT");
  48. digitalWrite(s2, HIGH);
  49. digitalWrite(s3, HIGH);
  50. }
  51. else if (flag == 2)
  52. {
  53. countG = counter;
  54. Serial.print("green=");
  55. Serial.print(countG, DEC);
  56. Serial.print("t");
  57. digitalWrite(s2, LOW);
  58. digitalWrite(s3, HIGH);
  59. }
  60. else if (flag == 3)
  61. {
  62. countB = counter;
  63. Serial.print("blue=");
  64. Serial.print(countB, DEC);
  65. Serial.print("t");
  66. Serial.println("n");
  67. digitalWrite(s2, LOW);
  68. digitalWrite(s3, LOW);
  69.  
  70. }
  71. else if (flag == 4)
  72. {
  73. flag = 0;
  74. }
  75. counter = 0;
  76. }
  77. void loop() //main program loop
  78. {
  79. TCS();
  80. delay(500);
  81. while (1);
  82.  
  83. }

 

The program shows the RGB color value for a detected color. The results of the program you can see below:

 

A screenshot of the serial monitor.

Botland.store - shop for makers!