Advertisement
microrobotics

TCS3200D-TR Colour Sensor IC

Mar 31st, 2023
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The TCS3200 is a color sensor that can detect and measure the intensity of individual red, green, and blue (RGB) channels. Here's an example of how to use the TCS3200 with an Arduino to read the RGB values and display them on the Serial Monitor:
  3.  
  4. To wire the TCS3200 sensor, connect its VDD pin to the 5V pin on the Arduino and the GND pin to the ground. Connect the S0, S1, S2, S3, and OUT pins to the corresponding pins on the Arduino as defined in the code (S0 to pin 8, S1 to pin 9, S2 to pin 12, S3 to pin 11, and OUT to pin 10).
  5.  
  6. This code will continuously read the RGB values and print them on the Serial Monitor. Note that the TCS3200 measures the intensity of each color channel separately, so you'll need to do additional processing to convert these values into a standard RGB color representation or other color spaces like HSV.
  7.  
  8. Make sure the TCS3200 sensor is properly placed over the object whose color you want to detect. You can also adjust the scaling array according to the TCS3200 datasheet if you need different frequency scaling settings.
  9. */
  10.  
  11. // TCS3200 connections
  12. const int S0 = 8;
  13. const int S1 = 9;
  14. const int S2 = 12;
  15. const int S3 = 11;
  16. const int OUT = 10;
  17.  
  18. // Color frequency scaling
  19. const int scaling[] = {HIGH, LOW}; // 100% scaling, you can change it according to the datasheet
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.  
  24.   pinMode(S0, OUTPUT);
  25.   pinMode(S1, OUTPUT);
  26.   pinMode(S2, OUTPUT);
  27.   pinMode(S3, OUTPUT);
  28.   pinMode(OUT, INPUT);
  29.  
  30.   digitalWrite(S0, scaling[0]);
  31.   digitalWrite(S1, scaling[1]);
  32. }
  33.  
  34. void loop() {
  35.   unsigned int red = readColorFrequency(S2, S3, LOW, LOW);
  36.   unsigned int green = readColorFrequency(S2, S3, HIGH, HIGH);
  37.   unsigned int blue = readColorFrequency(S2, S3, LOW, HIGH);
  38.  
  39.   Serial.print("Red: ");
  40.   Serial.print(red);
  41.   Serial.print(" Green: ");
  42.   Serial.print(green);
  43.   Serial.print(" Blue: ");
  44.   Serial.println(blue);
  45.  
  46.   delay(1000); // Wait 1 second between readings
  47. }
  48.  
  49. unsigned int readColorFrequency(int s2, int s3, int s2State, int s3State) {
  50.   digitalWrite(s2, s2State);
  51.   digitalWrite(s3, s3State);
  52.   delay(100); // Wait for the
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement