Arduino:How to use color sensor tcs230

Arduino Color Sensing Tutorial – TCS230 TCS3200 Color Sensor


In this Arduino Tutorial we will learn how to detect colors using Arduino and the TCS230 / TCS3200 Color Sensor. You can watch the following video or read the written tutorial below for more details.

How TCS230 Color Sensor Works

The TCS230 senses color light with the help of an 8 x 8 array of photodiodes. Then using a Current-to-Frequency Converter the readings from the photodiodes are converted into a square wave with a frequency directly proportional to the light intensity. Finally, using the Arduino Board we can read the square wave output and get the results for the color.



If we take a closer look at the sensor we can see how it detects various colors. The photodiodes have three different color filters. Sixteen of them have red filters, another 16 have green filters, another 16 have blue filters and the other 16 photodiodes are clear with no filters.


Each 16 photodiodes are connected in parallel, so using the two control pins S2 and S3 we can select which of them will be read. So for example, if we want to detect red color, we can just use the 16 red filtered photodiodes by setting the two pins to low logic level according to the table.




The sensor has two more control pins, S0 and S1 which are used for scaling the output frequency. The frequency can be scaled to three different preset values of 100 %, 20 % or 2%. This frequency-scaling function allows the output of the sensor to be optimized for various frequency counters or microcontrollers.

Now we are ready to move on and connect the TCS230 sensor to the Arduino board. Here’s the circuit schematics.




TCS230 Color Sensor Source Code


Description: First we need to define the pins to which the sensor is connected and define a variable for reading the frequency. In the setup section we need to define the four control pins as outputs and the sensor output as an Arduino input. Here we also need to set the frequency-scaling, for this example I will set it to 20%, and start the serial communication for displaying the results in the Serial Monitor.
In the loop section, we will start with reading the red filtered photodiodes. For that purpose we will set the two control pins S2 and S3 to low logic level. Then using the “pulseIn()” function we will read the output frequency and put it into the variable “frequency”. Using the Serial.print() function we will print the result on the serial monitor. The same procedure goes for the two other colors, we just need to adjust the control pins for the appropriate color.
  1. /* Arduino Color Sensing Tutorial
  2. *
  3. * by Dejan Nedelkovski, www.HowToMechatronics.com
  4. *
  5. */
  6. #define S0 4
  7. #define S1 5
  8. #define S2 6
  9. #define S3 7
  10. #define sensorOut 8
  11. int frequency = 0;
  12. void setup() {
  13. pinMode(S0, OUTPUT);
  14. pinMode(S1, OUTPUT);
  15. pinMode(S2, OUTPUT);
  16. pinMode(S3, OUTPUT);
  17. pinMode(sensorOut, INPUT);
  18. // Setting frequency-scaling to 20%
  19. digitalWrite(S0,HIGH);
  20. digitalWrite(S1,LOW);
  21. Serial.begin(9600);
  22. }
  23. void loop() {
  24. // Setting red filtered photodiodes to be read
  25. digitalWrite(S2,LOW);
  26. digitalWrite(S3,LOW);
  27. // Reading the output frequency
  28. frequency = pulseIn(sensorOut, LOW);
  29. // Printing the value on the serial monitor
  30. Serial.print("R= ");//printing name
  31. Serial.print(frequency);//printing RED color frequency
  32. Serial.print(" ");
  33. delay(100);
  34. // Setting Green filtered photodiodes to be read
  35. digitalWrite(S2,HIGH);
  36. digitalWrite(S3,HIGH);
  37. // Reading the output frequency
  38. frequency = pulseIn(sensorOut, LOW);
  39. // Printing the value on the serial monitor
  40. Serial.print("G= ");//printing name
  41. Serial.print(frequency);//printing RED color frequency
  42. Serial.print(" ");
  43. delay(100);
  44. // Setting Blue filtered photodiodes to be read
  45. digitalWrite(S2,LOW);
  46. digitalWrite(S3,HIGH);
  47. // Reading the output frequency
  48. frequency = pulseIn(sensorOut, LOW);
  49. // Printing the value on the serial monitor
  50. Serial.print("B= ");//printing name
  51. Serial.print(frequency);//printing RED color frequency
  52. Serial.println(" ");
  53. delay(100);
  54. }

Now if we run the Serial Monitor we will start getting some values. These values depend on the selected frequency-scaling, as well as from the surrounding lighting.


So now if we want to represent the detected colors with the RGB Model which has values from 0 to 255, we will use the map() function to map or convert the readings to the values from 0 to 255.

Here’s the final source code for this example:
  1. /* Arduino Color Sensing Tutorial
  2. *
  3. * by Dejan Nedelkovski, www.HowToMechatronics.com
  4. *
  5. */
  6. #define S0 4
  7. #define S1 5
  8. #define S2 6
  9. #define S3 7
  10. #define sensorOut 8
  11. int frequency = 0;
  12. void setup() {
  13. pinMode(S0, OUTPUT);
  14. pinMode(S1, OUTPUT);
  15. pinMode(S2, OUTPUT);
  16. pinMode(S3, OUTPUT);
  17. pinMode(sensorOut, INPUT);
  18. // Setting frequency-scaling to 20%
  19. digitalWrite(S0,HIGH);
  20. digitalWrite(S1,LOW);
  21. Serial.begin(9600);
  22. }
  23. void loop() {
  24. // Setting red filtered photodiodes to be read
  25. digitalWrite(S2,LOW);
  26. digitalWrite(S3,LOW);
  27. // Reading the output frequency
  28. frequency = pulseIn(sensorOut, LOW);
  29. //Remaping the value of the frequency to the RGB Model of 0 to 255
  30. frequency = map(frequency, 25,72,255,0);
  31. // Printing the value on the serial monitor
  32. Serial.print("R= ");//printing name
  33. Serial.print(frequency);//printing RED color frequency
  34. Serial.print(" ");
  35. delay(100);
  36. // Setting Green filtered photodiodes to be read
  37. digitalWrite(S2,HIGH);
  38. digitalWrite(S3,HIGH);
  39. // Reading the output frequency
  40. frequency = pulseIn(sensorOut, LOW);
  41. //Remaping the value of the frequency to the RGB Model of 0 to 255
  42. frequency = map(frequency, 30,90,255,0);
  43. // Printing the value on the serial monitor
  44. Serial.print("G= ");//printing name
  45. Serial.print(frequency);//printing RED color frequency
  46. Serial.print(" ");
  47. delay(100);
  48. // Setting Blue filtered photodiodes to be read
  49. digitalWrite(S2,LOW);
  50. digitalWrite(S3,HIGH);
  51. // Reading the output frequency
  52. frequency = pulseIn(sensorOut, LOW);
  53. //Remaping the value of the frequency to the RGB Model of 0 to 255
  54. frequency = map(frequency, 25,70,255,0);
  55. // Printing the value on the serial monitor
  56. Serial.print("B= ");//printing name
  57. Serial.print(frequency);//printing RED color frequency
  58. Serial.println(" ");
  59. delay(100);
  60. }

Comments

Popular posts from this blog

Arduino:How to program an arduino using loop statements

Automatic Door Locking with Arduino

Arduino:How to make a RC car with arduino