With a color sensor you teach your Arduino to distinguish between different colors. Most sensors can’t detect subtle differences – but they’re enough to detect the colors of M&Ms or Skittles, for example!

Here you will learn how to connect a color sensor, use it to detect colors and convert them to the familiar RGB model. In this tutorial we will use the popular TC3200 sensor.

AZDelivery Farbsensor TCS230 TCS3200 Farben Sensor Modul kompatibel mit Arduino
✔️ Chip: TAOS TCS3200 RGB Sensor; ✔️ Eingangsspannung: DC 3 ~ 5V; ✔️...

Connect the color sensor to the Arduino

On the sensor you will find a total of 8 pins, 7 of which you can connect to your Arduino as follows. The choice of the digital pins is of course up to you.

Color sensorArduino (Digital pin)
VCC5V
GNDGND
S06
S17
S28
S39
OUT10

A short explanation about the individual pins on the color sensor: With the pins S0 and S1 you can control the output frequency of the sensor. This way you can optimize your results, which we will leave aside for now in this tutorial.

The pins S2 and S3 control the individual photodiodes of the sensor. There are 16 of each for the colors red, green, blue and clear (without color filter). You can set the two pins of your Arduino either to HIGH or LOW – the combination then determines which diodes are addressed and which color is to be “read”:

S2S3Color
LOWLOWRed
LOWHIGHBlue
HIGHLOWClear (No filter)
HIGHHIGHGreen

We will take a closer look at these combinations in the sketch. However, the pin OUT is still missing – this is where your Arduino reads the measurement results.

The sketch to detect colors

So let’s get to the code. You don’t need a separate library for the color sensor, you can do the measurements yourself with a few simple functions. First, define the connectors used at the beginning of the sketch:

#define S0 6
#define S1 7
#define S2 8
#define S3 9
#define sensorOut 10

Then you need three variables each for the raw and RGB values of the individual colors red, green and blue:

int frequencyR = 0;
int frequencyG = 0;
int frequencyB = 0;

int red = 0;
int green = 0;
int blue = 0;

The setup function

Here you start your Serial Monitor and define the individual pinModes:

Serial.begin(115200);

pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);

As you can see, you address the pins S1 to S3 from your Arduino (OUTPUT) and want to receive measurement data from the pin sensorOut(INPUT). There is still one thing missing: As mentioned above, you control the output frequency with pins S1 and S2. You set this to 20% with the following two lines:

digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);

There are fixed values and combinations for the output frequency. If you want to go a little deeper and optimize the results of your sensor, use this table as a guide:

S0S1Output frequency
LOWLOWOff
LOWHIGH2%
HIGHLOW20%
HIGHHIGH100%

The Loop

So let’s move on to the measurements. In the loop of your sketch, take a separate measurement for each of the three colors red, green and blue and convert your results to the RGB color space. First, the code for red:

digitalWrite(S2, LOW);
digitalWrite(S3, LOW);

frequencyR = pulseIn(sensorOut, LOW);
Serial.print("Red = ");
Serial.print(frequencyR);

First you set both pins S2 and S3 to LOW to address only the photodiodes with red color filter. Then you read the value with the function pulseIn() and store the value in the variable frequencyR. Finally you output it to the Serial Monitor. In the Arduino reference you will learn more about the pulseIn() function.

You can’t do much with these measurement results yet – it’s better if you get a well-known value like the red component in RGB mode. This way, together with the results of the green and blue colors, you can determine the color in front of your sensor more or less exactly and display it on a TFT display for control purposes, for example.

To achieve this, the function map() is useful:

map(frequencyR, 20, 120, 255, 0)

In this function you need five arguments: First, the measured value frequencyR and the measuring range of the sensor. For this, it is best to make a few test measurements with different colored objects in front of your color sensor and look at the minimum and maximum values in the Serial Monitor. In our case, the values were roughly in the range of 20 to 120 – but that may be different for you.

Once you have determined your appropriate range of values, the last two arguments come into play. These specify the maximum (255) and minimum red value (0) in RGB mode. The “more” red your color sensor detects, the lower its measured value (in our case towards 20). However, this corresponds to a high value in the RGB color space – therefore you “map” the 20 to the 255 and the other way around the 120 to the 0.

All colors together

And that was it basically. For the colors blue and green you simply repeat the procedure for the color red. In your Serial Monitor you will now see the RGB values of each color, which together should approximate the color of the object in front of the sensor.

Here now the whole sketch to copy out:

//pins for color sensor
#define S0 6
#define S1 7
#define S2 8
#define S3 9
#define sensorOut 10

//raw and RGB values for each color
int frequencyR = 0;
int frequencyG = 0;
int frequencyB = 0;

int red = 0;
int green = 0;
int blue = 0;

void setup() {
  Serial.begin(115200);

  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
}

void loop() {
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);

  frequencyR = pulseIn(sensorOut, LOW);
  Serial.print("Red = ");
  //Serial.print(frequencyR);
  red = map(frequencyR, 20, 150, 255, 0);
  Serial.print(red);


  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);

  frequencyB = pulseIn(sensorOut, LOW);
  Serial.print(" Blue = ");
  //Serial.print(frequencyB);
  blue = map(frequencyB, 20, 150, 255, 0);
  Serial.print(blue);


  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);

  frequencyG = pulseIn(sensorOut, LOW);
  Serial.print(" Green = ");
  //Serial.println(frequencyG);
  green = map(frequencyG, 20, 150, 255, 0);
  Serial.println(green);

  delay(100);
}

What’s next?

You can now detect colors with a color sensor and transfer them to the RGB color space. How about creating your own color pickerto determine the RGB values of individual objects? A TFT display is ideal for this purpose, on which you can also immediately display the determined color.

Letzte Aktualisierung am 2021-02-28 / Affiliate Links / Bilder von der Amazon Product Advertising API