Color Detection using Arduino (Includes: Materials, Schematics, Code, Pictures, and Risk Assessment)

Why did the robot with a color detection system go to therapy?

It was tired of seeing everything in black and white!

Color detection in the robotics world

In the robotics world, using color is another way to train your logistics system for various reasons.

  1. Quality Control: They ensure products meet color specifications, identifying defects or inconsistencies in materials or finished goods.
  2. Sorting and Classification: Robots use color to sort and classify items, which is important in manufacturing, packaging, and recycling processes.
  3. Assembly Verification: Ensuring the correct components, which may be color-coded, are assembled correctly in complex products.

Project overview

The primary reason for this project was to demonstrate capabilities in the color sensor and how it detects light/color levels at different distances with an added proximity sensor. The proximity sensor (photoresistor) was used to detect presence of a door, or object to signify that the enclosure was closed, to start reading the measurements. I added in a RGB LED element to provide a visual confirmation of the color reading.

I used a Risk Assessment.docx with a timeline and pre-planning to complete the device in the required time.

At an average range, the color intensity was accurate based on the read results. As the distance increased, the light filters/photodiodes were not able to provide an accurate measurement. I attempted to expand the measurement ranges for detecting color, but the sensor was not able to distinguish multiple colors.

Block Diagram


Fig 1. Block Diagram Source Arduino Color Detection : 5 Steps (with Pictures) - Instructables

Scheme-It Schematic / Wiring


Fig 2. Wiring instructions

Pictures


Fig 3. Photoresistor, RGB LED and start button



Fig 4. LCD screen



Fig 5. Arduino Nano Every wiring

Arduino Code

If you need assistance setting up Arduino, please review this DigiKey post: How to Get Started with Arduino.

original code source (has been altered for use with additional parts/logic): TCS3200_LCD · GitHub

// Arduino Color Detector 
// *   
// *   I2C LCD 2/16 Serial Display 
// *   - SDA ==> Nano Pin A4  
// *   - SCL ==> Nano Pin A5
// * 
// *   Color Sensor
// *    - frequency output scaling
// *      s0          s1         
// *       L           L         = power down no output
// *       L           H         = 2%
// *       H           L         = 20%
// *       H           H         = 100% 
// *   - EO = set low to enable 
// *   


 

/* LCD */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the LCD address to 0x27; 16 chars and 2 lines

 

//pins wiring to Arduino
#define s0 4
#define s1 5
#define s2 6
#define s3 7
#define outPin 8


 

boolean DEBUG = true;

 

// Variables
int red;
int grn;
int blu;

///LED PINS
int redPin= 3;
int greenPin = 10;
int bluePin = 9;

String color ="";
int count = 0;
long startTiming = 0;
long elapsedTime = 0;
int photoRes = 14;
int startBtn = 13;
int start = 1; // start btn state
int photoresState = 0; //inital state of photoresistor


 

void setup()
{
  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(outPin, INPUT); //out from sensor becomes input to arduino
  pinMode(photoRes, INPUT);
  pinMode(startBtn, INPUT);

  ///RGB LED PINS
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

 

//  frequency scaling to 100%
  digitalWrite(s0,HIGH);
  digitalWrite(s1,HIGH);

  Serial.begin(9600);
  lcd.init();
  //lcd.begin(16, 2); //could be 8, 1
  lcd.backlight();
  lcd.setCursor(4, 0); // set the cursor to column 3, line 0
  lcd.print("DigiKey"); // Print a message to the LCD
  lcd.setCursor(1, 1); // set the cursor to column 2, line 1
  lcd.print("ColorDetector");
  startTiming = millis();
  delay(3000);
  lcd.clear();
  lcd.setCursor(4,0);
  lcd.print("Press");
  lcd.setCursor(4,1);                                    
  lcd.print("Start");
}

 

void loop(){
start = digitalRead(startBtn);
photoresState = analogRead(photoRes);
Serial.println(photoresState);
delay(20);

  if (start == LOW && photoresState < 120) {
    int state = !state; // toggle the state each time the button is pressed
    while (!digitalRead(startBtn)); // wait for button release
      lcd.clear();
      while (state){
        getColor();
        if (DEBUG) printData(); 
        elapsedTime = millis()-startTiming; 
        if (elapsedTime > 1000){
          showDataLCD();
          startTiming = millis();
        }
      }
    
  }

}

 

 

/* Based on RGB components ==> Determine color. these will need to be adjusted based on ambience */
void getColor(){  
  readRGB();

  if(red > 5 && red < 10 && grn > 3 && grn < 11 && blu > 2 && blu < 9){ 
    color = "WHITE";
    setColor(20,20,4);
  }
  else if (red > 71 && red < 73 && grn > 68 && grn < 71 && blu > 51 && blu < 54){
    color = "BLACK";
    setColor(0,0,0);
  }
  else if (red > 16 && red < 19 && grn > 46 && grn < 52 && blu > 35 && blu < 38){
    color = "RED";
    setColor(0,30,0);
  }
  else if (red > 15 && red < 25 && grn > 10 && grn < 30 && blu > 5 && blu < 30){
    color = "GREEN";
    setColor(30,0,0);
  }
  else if (red > 7 && red < 9 && grn >= 8 && grn < 10 && blu > 9 && blu < 11){
    color = "YELLOW";
    setColor(30,100,0);
  }
  else if (red > 50 && red < 59 && grn > 33 && grn < 38 && blu > 15 && blu < 19){
    color = "BLUE";
    setColor(0,0,30);
  }
  else{
    color = "NO COLOR";
  }
}

 

/* read RGB components */
void readRGB(){
  red = 0;
  grn = 0;
  blu = 0;
  int n = 10;
  for (int i = 0; i < n; ++i){
    //read red component
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    red = red + pulseIn(outPin, LOW);

   //read green component
    digitalWrite(s2, HIGH);
    digitalWrite(s3, HIGH);
    grn = grn + pulseIn(outPin, LOW);

   //read blue component
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    blu = blu + pulseIn(outPin, LOW);
  }
  red = red/n;
  grn = grn/n;
  blu = blu/n;
}

 

/***************************************************
* Showing capured data at LCD
****************************************************/
void showDataLCD(void){ 
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("R");
  lcd.setCursor(1,0);
  lcd.print("    ");
  lcd.setCursor(1,0);
  lcd.print(red);
  lcd.setCursor(5,0);
  lcd.print(" G");
  lcd.setCursor(7,0);
  lcd.print("    ");
  lcd.setCursor(7,0);
  lcd.print(grn);
  lcd.setCursor(12,0);
  lcd.print("B");
  lcd.setCursor(13,0);
  lcd.print("    ");
  lcd.setCursor(13,0);
  lcd.print(blu);

  lcd.setCursor(0,1);
  lcd.print("Color: ");
  lcd.setCursor(7,1); 
  lcd.print("        ");
  lcd.setCursor(7,1);  
  lcd.print(color);

}

 

/***************************************************
* Showing captured data at Serial Monitor
****************************************************/
void printData(void){

  Serial.print("red = ");
  Serial.print(red);
  Serial.print("   green = ");
  Serial.print(grn);
  Serial.print("   blue = ");
  Serial.print(blu);
  Serial.print (" - ");
  Serial.print (color);
  Serial.println (" detected!");
}

void setColor(int redValue, int greenValue, int blueValue){
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

Part List

Files

Parts List (Bill of Materials):
Color Detector BoM.xlsx (7.2 KB)

Arduino IDE Code:
ColorDetect.ino (5.1 KB)

Risk Assessment:
Risk Assessment.docx (16.9 KB)

3D Printed Enclosure(STL):
Color Detector Enclosure.stl (84.4 KB)

Citations

2 Likes