Electronics Champ
Published

Color Controlled Car using TCS3200 Color Sensor

The color sensor is programmed to detect three colors (RGB). Based on the color, the car moves in different paths.

BeginnerShowcase (no instructions)5,125
Color Controlled Car using TCS3200 Color Sensor

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
TCS3200 Color Sensor
×1
L298N Motor Driver
×1
DC Motor, 12 V
DC Motor, 12 V
×2
Breadboard mini
×1
Jumper wires (generic)
Jumper wires (generic)
×1
9V battery (generic)
9V battery (generic)
×1
5V Cell
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic

Code

Code

Arduino
/*
   This program moves a car in three
   different patterns based on the
   colour sensed by the colour sensor.
   The colour sensor is mounted on the
   car.
   This program is created by Shreyas for
   Electronics Champ YouTube Channel.
   Please subscribe to this channel
   Thank You
*/

//Include the library
#include <tcs3200.h>

/*
   Initialize the variables
   for colour sensor
*/
int red;
int green;
int blue;
String sensedColour = "none";
int delayTime = 0;

/*
   Initialize the variables
   for motor driver
*/
int leftMotorForward = 9;
int leftMotorBackward = 10;
int rightMotorForward = 11;
int rightMotorBackward = 12;

tcs3200 sensor(4, 5, 6, 7, 8); //S0, S1, S2, S3, output pin

void setup() {

  /*
    Set pin 9, 10, 11
    and 12 as output
  */
  for (int i = 9; i < 13; i = i + 1) {

    pinMode(i, OUTPUT);

  }

  red = sensor.colorRead('r', 20);   //Scaling the sensor to 20%

  Serial.begin(9600);

}

void loop() {

  readColour();
  checkColour();

  if (sensedColour == "red") {  //If the colour is red…..

    delayTime = 2000;
    delay(delayTime);
    front();     //A function to move the car front
    sensedColour = "none";  //Sets the sensed colour as ‘None’

  }

  else if (sensedColour == "green") {  //If the colour is green…..

    delayTime = 2000;
    delay(delayTime);
    circle();     //Function to move the car in circular path
    sensedColour = "none";  //Sets the sensed colour as ‘None’

  }

  else if (sensedColour == "blue") {   //If the colour is blue…..
    //move the car in a triangular path
    delayTime = 2000;
    delay(delayTime);
    delayTime = 1000;
    front();
    delayTime = 500;
    delay(50);
    right();
    delayTime = 1000;
    delay(50);
    front();
    delayTime = 500;
    delay(50);
    right();
    delayTime = 1000;
    front();
    delayTime = 500;
    right();
    sensedColour = "none";  //Sets the sensed colour as ‘None’

  }

  if (red < 8 and green < 8 and blue < 8) {  //If the colour sensor is not sensing any colour…..

    digitalWrite(leftMotorForward, LOW);
    digitalWrite(leftMotorBackward, LOW);
    digitalWrite(rightMotorForward, LOW);
    digitalWrite(rightMotorBackward, LOW);  //Stops the car
    sensedColour = "none";

  }

}

//function to move the car forward
void front() {

  digitalWrite(leftMotorForward, HIGH);
  digitalWrite(rightMotorForward, HIGH);
  delay(delayTime);
  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, LOW);

}


//function to turn the car left
void left() {

  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, HIGH);
  delay(delayTime);
  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, LOW);

}

//function to turn the car right
void right() {

  digitalWrite(leftMotorForward, HIGH);
  digitalWrite(rightMotorForward, LOW);
  delay(delayTime);
  digitalWrite(leftMotorForward, LOW);
  digitalWrite(rightMotorForward, LOW);

}

//car moves in a circular path
void circle() {

  left();
  left();

}

//function to read the colour
void readColour() {

  red = sensor.colorRead('r');   //reads colour value for red
  green = sensor.colorRead('g');   //reads colour value for green
  blue = sensor.colorRead('b');    //reads colour value for blue
  
  //Prints the values on the serial monitor
  Serial.print("R = ");
  Serial.println(red);
  Serial.print("G = ");
  Serial.println(green);
  Serial.print("B = ");
  Serial.println(blue);
  Serial.println(" ");
  delay(10);     

}

//sets the colour that is sensed
void checkColour() {

  if (red > green and red > blue and red > 8) {   //If the colour is red......

    sensedColour = "red";     //Sets the value of this variable to “red”

  }

  else if (green > red and green > blue and green > 8) {   //If the colour is green......

    sensedColour = "green";     //Sets the value of this variable to “green”

  }

  else if (blue > green and blue > red and blue > 8) {   //If the colour is blue......

    sensedColour = "blue";     //Sets the value of this variable to “blue”

  }

}

Credits

Electronics Champ
5 projects • 11 followers
Projects based on breadboard electronics and Arduino with clear step-by-step instructions, circuit diagrams, schematics, and source code.

Comments