TCS3200 Sensor Calibration

Hi all,

I am doing a project that is using Arduino UNO R3 and dfrobot TCS3200. The objective of this project is to able to display the detected color and RGB values to a GUI created with Python.

I have followed the guide from "dronebotworkshop" for the calibration. I fixed the sensors to the top of an enclosed cover and calibrated them by recording the lowest value of white paper and the highest value of black paper.

I have faced 2 issues with this:

  1. The RGB value of some detected objects will go below 0 or above 255 even though I have fixed the min and max values to 0 and 255 (For example, scanning yellow paper will shoot the RGB values to 265,240,185)

I am unable to constraint the values to 0 and 255 as it will give me funny numbers appreciate if someone can guide me on this.

Below is my code:

// Define color sensor pins

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Calibration Values
// Get these from Calibration Sketch

int redMin = 43; // Red minimum value
int redMax = 323; // Red maximum value
int greenMin = 36; // Green minimum value
int greenMax = 351; // Green maximum value
int blueMin = 28; // Blue minimum value
int blueMax = 250; // Blue maximum value

// Variables for Color Pulse Width Measurements

int redPW = 0;
int greenPW = 0;
int bluePW = 0;

// Variables for final Color values

int redValue;
int greenValue;
int blueValue;

void setup() {

  // Set S0 - S3 as outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Set Sensor output as input
  pinMode(sensorOut, INPUT);
  
  // Set Frequency scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Setup Serial Monitor
  Serial.begin(9600);
}

void loop() {
  
  // Read Red value
  redPW = getRedPW();
  // Map to value from 0-255
  redValue = map(redPW, redMin,redMax,255,0);
  // Delay to stabilize sensor
  delay(200);
  
  // Read Green value
  greenPW = getGreenPW();
  // Map to value from 0-255
  greenValue = map(greenPW, greenMin,greenMax,255,0);
  // Delay to stabilize sensor
  delay(200);
  
  // Read Blue value
  bluePW = getBluePW();
  // Map to value from 0-255
  blueValue = map(bluePW, blueMin,blueMax,255,0);
  // Delay to stabilize sensor
  delay(200);
  
  // Print output to Serial Monitor

  Serial.print(redValue);
    Serial.print(",");

  Serial.print(greenValue);
    Serial.print(",");
  
  Serial.println(blueValue);
  Serial.print("");
  
}


// Function to read Red Pulse Widths
int getRedPW() {

  // Set sensor to read Red only
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

// Function to read Green Pulse Widths
int getGreenPW() {

  // Set sensor to read Green only
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

// Function to read Blue Pulse Widths
int getBluePW() {

  // Set sensor to read Blue only
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

The map function does not constrain the output values, it just sets up a correspondence between input and output. To constrain the result to be within bounds, use constrain() on the output of the map function.

Sorry, I don't know what you mean by this:

I am unable to constraint the values to 0 and 255 as it will give me funny numbers

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.