Adding TCS3200 color sensor to robot car

Hi everyone I am new to arduino uno. Me and my daughter are building this robot car and it functions great. It utilizes an Arduino uno board and Sensor Shield v5.0. We have now added a TCS3200 color sensor so it can stop when it senses a certain color but we cant get it to work. When install the code for the color sensor the sensor works great on the serial monitor, but when I combine the robot car code and the color sensor code the color readings on the serial monitor are not stable. I have tried disconnecting all the robot connectors and just leaving the color sensor connectors, but it does not stabilize, as soon as I add just the color sensor code, it is good. Is there a limitation on the arduino or sensor shield that can be causing this? TIA

Welcome, you sound like a great ateam. Please post an annotated schematic being sure to include all connections, power, ground and power sources. Add a link to your sensor giving technical information, I found several. At this point I do not think it is code but it would be best to post that using code tags as outlined in the forum guidelines.

#include <Servo.h>

int pinLB = 15;
int pinLF = 16;
int pinRB = 17;
int pinRF = 18;
int MotorLPWM = 5;
int MotorRPWM = 6;
int inputPin = 9;
int outputPin = 8;
Servo myservo;

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

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

int Fspeedd = 0;
int Rspeedd = 0;
int Lspeedd = 0;
int directionn = 0;

const int LOOK_FORWARD = 125;
const int LOOK_RIGHT = 185;
const int LOOK_LEFT = 65;

int Fgo = 8;
int Rgo = 6;
int Lgo = 4;
int Bgo = 2;

int delay_time = 200; // Move delay_time here

void setup() {
  Serial.begin(9600);
  pinMode(pinLB, OUTPUT);
  pinMode(pinLF, OUTPUT);
  pinMode(pinRB, OUTPUT);
  pinMode(pinRF, OUTPUT);
  pinMode(MotorLPWM, OUTPUT);
  pinMode(MotorRPWM, OUTPUT);
  pinMode(inputPin, INPUT);
  pinMode(outputPin, OUTPUT);
  myservo.attach(10);

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

void loop() {
  myservo.write(LOOK_FORWARD);
  delay(200);

  redPW = getRedPW();
  delay(200);
  greenPW = getGreenPW();
  delay(200);
  bluePW = getBluePW();
  delay(200);

  Serial.print("Red PW = ");
  Serial.print(redPW);
  Serial.print(" - Green PW = ");
  Serial.print(greenPW);
  Serial.print(" - Blue PW = ");
  Serial.println(bluePW);

  if (redPW > 1000 && greenPW < 500 && bluePW < 500) {
    stopp(1);
  } else {
    detection();
    if (directionn == 2) {
      back(8);
      turnL(2);
      Serial.print(" Reverse ");
    }
    if (directionn == 6) {
      back(1);
      turnR(6);
      Serial.print(" Right ");
    }
    if (directionn == 4) {
      back(1);
      turnL(6);
      Serial.print(" Left ");
    }
    if (directionn == 8) {
      advance(1);
      Serial.print(" Advance ");
      Serial.print(" ");
    }
  }
}

int getRedPW() {
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  int PW = pulseIn(sensorOut, LOW);
  return PW;
}

int getGreenPW() {
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  int PW = pulseIn(sensorOut, LOW);
  return PW;
}

int getBluePW() {
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  int PW = pulseIn(sensorOut, LOW);
  return PW;
}

void advance(int a) {
  digitalWrite(pinRB, HIGH);
  digitalWrite(pinRF, LOW);
  analogWrite(MotorRPWM, 180);
  digitalWrite(pinLB, HIGH);
  digitalWrite(pinLF, LOW);
  analogWrite(MotorLPWM, 180);
  delay(a * 1);
}

void right(int b) {
  digitalWrite(pinRB, LOW);
  digitalWrite(pinRF, HIGH);
  analogWrite(MotorRPWM, 250);
  digitalWrite(pinLB, LOW);
  digitalWrite(pinLF, LOW);
  delay(b * 100);
}

void left(int c) {
  digitalWrite(pinRB, LOW);
  digitalWrite(pinRF, LOW);
  digitalWrite(pinLB, LOW);
  digitalWrite(pinLF, HIGH);
  analogWrite(MotorLPWM, 250);
  delay(c * 100);
}

void turnR(int d) {
  digitalWrite(pinRB, HIGH);
  digitalWrite(pinRF, LOW);
  analogWrite(MotorRPWM, 250);
  digitalWrite(pinLB, LOW);
  digitalWrite(pinLF, HIGH);
  analogWrite(MotorLPWM, 250);
  delay(d * 50);
}

void turnL(int e) {
  digitalWrite(pinRB, LOW);
  digitalWrite(pinRF, HIGH);
  analogWrite(MotorRPWM, 220);
  digitalWrite(pinLB, HIGH);
  digitalWrite(pinLF, LOW);
  analogWrite(MotorLPWM, 220);
  delay(e * 50);
}

void stopp(int f) {
  digitalWrite(pinRB, LOW);
  digitalWrite(pinRF, LOW);
  digitalWrite(pinLB, LOW);
  digitalWrite(pinLF, LOW);
  delay(f * 100);
}

void back(int g) {
  digitalWrite(pinRB, LOW);
  digitalWrite(pinRF, HIGH);
  analogWrite(MotorRPWM, 110);
  digitalWrite(pinLB, LOW);
  digitalWrite(pinLF, HIGH);
  analogWrite(MotorLPWM, 230);
  delay(g * 500);
}

void detection() {
  ask_pin(LOOK_FORWARD, Fspeedd, "Front");
  if ((Fspeedd < 10) && (Fspeedd > 0)) {
    stopp(1);
    back(2);
  }
  if ((Fspeedd < 25) && (Fspeedd > 0)) {
    stopp(1);
    ask_pin(LOOK_LEFT, Lspeedd, "Left");
    delay(delay_time);
    ask_pin(LOOK_RIGHT, Rspeedd, "Right");
    delay(delay_time);
    if (Lspeedd > Rspeedd) {
      directionn = Lgo;
    }
    if (Lspeedd <= Rspeedd) {
      directionn = Rgo;
    }
    if (Lspeedd < 15 && Rspeedd < 15) {
      directionn = Bgo;
    }
  } else {
    directionn = Fgo;
  }
}

void ask_pin(int servo_pos, int &dir, const char* pinDir) {
  Serial.print("servo_pos passed::: ");
  Serial.println(servo_pos);
  myservo.write(servo_pos);
  delay(delay_time);
  digitalWrite(outputPin, LOW);
  delayMicroseconds(2);
  digitalWrite(outputPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(outputPin, LOW);
  float dist = pulseIn(inputPin, HIGH);
  dist = dist / 5.8 / 10;
  Serial.print(pinDir);
  Serial.print(" distance: ");
  Serial.println(dist);
  dir = dist;
}


Thanks. I have posted the info I have. I can post a better wiring diagram if needed.

The wiring diagram does not do it for me. Your daughter and her team do nice work!

1 Like

They won't be. Color and intensity readings depend on angle of view, ambient lighting, color and reflectivity of object surface, etc. It is not at all simple to detect a specific color -- you have to take the real life range of values into account.

Point the color sensor at a variety of test objects under a variety of lighting situations, and learn how it behaves and how to interpret the readings, before tasking it with control of the vehicle.




What I meant to say is even when standing still at the same distance the color sensor readings are not correct on the serial monitor. If I load the color sensor code only and not change anything on the wiring the sensor is stable. As soon as I install the code for the robot and color sensor combined the readings are unstable. This is with the color sensor at the same angle and distance.

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

// Variables for Color Pulse Width Measurements

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

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 Pulse Width scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Setup Serial Monitor
  Serial.begin(9600);
}

void loop() {
  
  // Read Red Pulse Width
  redPW = getRedPW();
  // Delay to stabilize sensor
  delay(200);
  
  // Read Green Pulse Width
  greenPW = getGreenPW();
  // Delay to stabilize sensor
  delay(200);
  
  // Read Blue Pulse Width
  bluePW = getBluePW();
  // Delay to stabilize sensor
  delay(200);
  
  // Print output to Serial Monitor
  Serial.print("Red PW = ");
  Serial.print(redPW);
  Serial.print(" - Green PW = ");
  Serial.print(greenPW);
  Serial.print(" - Blue PW = ");
  Serial.println(bluePW);
  
}


// 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;

}

This is the code for the color sensor ^^^

What do you mean by "readings are not correct"? What do you think would be correct?

Did you try this suggested experiment, and if so, what happens?

Point the color sensor at a variety of test objects under a variety of lighting situations, and learn how it behaves and how to interpret the readings, before tasking it with control of the vehicle.

Please post examples, and a photo of your setup, showing the sensor and the colored object(s).

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