SavarJR
Published

Color Identificator

A cool project which identifies the color you put in front of the color sensor and turns the RGB into that color.

IntermediateShowcase (no instructions)182
Color Identificator

Things used in this project

Hardware components

RGB Diffused Common Anode
RGB Diffused Common Anode
×1
Color Photo Sensor, RGB
Color Photo Sensor, RGB
×1
Arduino UNO
Arduino UNO
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 100k ohm
Resistor 100k ohm
×1

Story

Read more

Code

Color Identificator

Arduino
int S2 = 7;  //Setting up pins of the color sensor
int S3 = 8;  //Setting up pins of the color sensor
int outPin = 4;  //Setting up pins of the color sensor
unsigned int redPulseWidth;  //variable to measure the red component in the color shown to the color sensor
unsigned int greenPulseWidth;  //variable to measure the green component in the color shown to the color sensor
unsigned int bluePulseWidth;  //variable to measure the blue component in the color shown to the color sensor
float redVolt;  //variable to store the voltage to be written to red pin of the RGB led
float greenVolt;  //variable to store the voltage to be written to green pin of the RGB led
float blueVolt;  //variable to store the voltage to be written to blue pin of the RGB led

void setup() {
  pinMode(S2, OUTPUT);  //setting pins S2 AND S3 as output pins
  pinMode(S3, OUTPUT);
  pinMode(outPin, INPUT);  //setting outPin as input pin
  Serial.begin(9600);  //starting the serial port
}

void loop() {
  digitalWrite(S2, LOW);  //by setting S2 AND S3 low we are reading the red component of the color
  digitalWrite(S3, LOW);
  redPulseWidth = pulseIn(outPin, LOW); //we are storing the red component as a number between 1,02,400. P.S. THE BIGGER THE NUMBER SMALLER THE COLOR COMPONENT AND VICE VERSA

  digitalWrite(S2, LOW);  //by setting S2 low and S3 high we are reading the blue component of the color
  digitalWrite(S3, HIGH);  
  bluePulseWidth = pulseIn(outPin, LOW);  //we are storing the blue component as a number between 0 and 1,02,400

  digitalWrite(S2, HIGH);  //by setting S2 AND S3 high we are reading the green componet of the color
  digitalWrite(S3, HIGH);
  greenPulseWidth = pulseIn(outPin, LOW);  //we are storing the green component as a number between 0 and 1,02,400

  redVolt = redPulseWidth/400.-1; //turning the pulse widths into numbers between 0 and 255
  redVolt = (255 - redVolt);  //then reversing the smaller number into bigger one and bigger number into smaller one
  greenVolt = greenPulseWidth/400.-1;
  greenVolt = (255 - greenVolt);
  blueVolt = bluePulseWidth/400.-1;
  blueVolt = (255 - blueVolt);

  Serial.print(redVolt);
  Serial.print(", ");
  Serial.print(blueVolt);
  Serial.print(", ");
  Serial.println(greenVolt);
  delay(250);

  //writing all the conditions and setting the redVolt, greenVolt, and blueVolt accordingly because as you can see in the serial monitor the red, blue, and green components are similar 
  //and if we pass on these values straight to the RGB it won't work. Hence, we are writing the following conditions
  if(redVolt>greenVolt && greenVolt>blueVolt){
    redVolt = 255;
    greenVolt = greenVolt/2;
    blueVolt = 0;
    }
    
  if(redVolt>blueVolt && greenVolt<blueVolt){
    redVolt = 255;
    blueVolt = blueVolt/2;
    greenVolt = 0;
    }

      if(greenVolt>redVolt && blueVolt<redVolt){
    greenVolt = 255;
    redVolt = redVolt/2;
    blueVolt = 0;
    }

    
  if(greenVolt>blueVolt && blueVolt>redVolt){
    greenVolt = 255;
    blueVolt = blueVolt/2;
    redVolt = 0;
    }

    
  if(blueVolt>redVolt && redVolt>greenVolt){
    blueVolt = 255;
    redVolt = redVolt/2;
    greenVolt = 0;
    }

    
  if(blueVolt>greenVolt && greenVolt>redVolt){
    redVolt = 0;
    greenVolt = greenVolt/2;
    blueVolt = 255;
    }
    redVolt = redVolt*8;
    blueVolt = blueVolt/8;
    greenVolt = greenVolt/8;

    analogWrite(10, redVolt); //writing values to RGB pins of the colour   
    analogWrite(9, greenVolt);
    analogWrite(3, blueVolt);
    
}

Credits

SavarJR
3 projects • 1 follower

Comments