Object Recogniton using Colors - Arduino UNO


Tutorials Description: 

This is a simple tutorial and demonstration on how you can teach your Arduino boards to detect simple objects using its color properties. Please take note that this is not a Camera Recognition program since there are no cameras used in this project.

With this simple and less cost set-up your Arduino UNO can be used to do sorting or picking of objects by simply doing a computation of their color temperature and properties using RGB readings.


Materials used for this tutorial:

1x Arduino UNO or MEGA2560
1x DFRobot Color Sensor Breakboard/Module
1x LCD Screen (2liner)
1x I2C for LCD
1x Breadboard
1x PCB Prototyping Board
Set of Breadwires

Schematic / Layout:



/-----------------------------Calibration Code:--------------------------------/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define pin_S0 4
#define pin_S1 5
#define pin_S2 6
#define pin_S3 7
#define pin_Freq 8
#define led_Amb 9

int pwm_freq = 0;
int temp_R;
int temp_G;
int temp_B;

float colorTemp = 0;

void setup() {
  lcd.init();
  lcd.backlight();

  pinMode(led_Amb, OUTPUT);


  pinMode(pin_S0, OUTPUT);
  pinMode(pin_S1, OUTPUT);
  pinMode(pin_S2, OUTPUT);
  pinMode(pin_S3, OUTPUT);

  pinMode(pin_Freq, INPUT);

  //20% ang set-up ko
  digitalWrite(pin_S0, HIGH);
  digitalWrite(pin_S1, LOW);

  // bigyan ng warm-up time ang sensor
  lcd.setCursor(1, 0);
  lcd.print("Warming-up");
  lcd.setCursor(3, 1);
  lcd.print(" secs.....");

  for (int x = 9; x > 0; x--) {
    lcd.setCursor(1, 1);
    lcd.print(x);
    digitalWrite(led_Amb, HIGH);
    delay(500);
    digitalWrite(led_Amb, LOW);
    delay(500);
  }
  lcd.clear();
  digitalWrite(led_Amb, HIGH);

  lcd.setCursor(1, 0);
  lcd.print("R: ");
  lcd.setCursor(8, 0);
  lcd.print("G: ");
  lcd.setCursor(2, 1);
  lcd.print("B: ");
  lcd.setCursor(8, 1);
  lcd.print("A: ");

}

void loop() {
  // Yung Red Layer ang babasahin natin
  digitalWrite(pin_S2, LOW);
  digitalWrite(pin_S3, LOW);

  // kunin natin sa output pin yung red data
  pwm_freq = pulseIn(pin_Freq, LOW);
  temp_R = pwm_freq;
  delay(50);

  // Iprint sa LCD
  lcd.setCursor(4, 0);
  lcd.print(temp_R);

  // Yung Green Layer ang babasahin natin
  digitalWrite(pin_S2, HIGH);
  digitalWrite(pin_S3, HIGH);

  // kunin natin sa output pin yung green data
  pwm_freq = pulseIn(pin_Freq, LOW);
  temp_G = pwm_freq;
  delay(50);

  // Iprint sa LCD
  lcd.setCursor(12, 0);
  lcd.print(temp_G);


  // Yung Blue Layer ang babasahin natin
  digitalWrite(pin_S2, HIGH);
  digitalWrite(pin_S3, HIGH);

  // kunin natin sa output pin yung blue data
  pwm_freq = pulseIn(pin_Freq, LOW);
  temp_B = pwm_freq;
  delay(50);

  // Iprint sa LCD
  lcd.setCursor(4, 1);
  lcd.print(temp_B);

  colorTemp = (temp_R + temp_G + temp_B) / 3;
  delay(50);


  // Iprint sa LCD
  lcd.setCursor(12, 1);
  lcd.print(colorTemp);

}

/-------------------------Final Code after Calibration:-----------------------------/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define pin_S0 4
#define pin_S1 5
#define pin_S2 6
#define pin_S3 7
#define pin_Freq 8
#define led_Amb 9

int pwm_freq = 0;
int temp_R;
int temp_G;
int temp_B;

float colorTemp = 0;

int colorSwitch = 0;
int av_Color = 0;

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

  pinMode(led_Amb, OUTPUT);


  pinMode(pin_S0, OUTPUT);
  pinMode(pin_S1, OUTPUT);
  pinMode(pin_S2, OUTPUT);
  pinMode(pin_S3, OUTPUT);

  pinMode(pin_Freq, INPUT);

  //20% ang set-up ko
  digitalWrite(pin_S0, HIGH);
  digitalWrite(pin_S1, LOW);

  // bigyan ng warm-up time ang sensor
  lcd.setCursor(1, 0);
  lcd.print("Warming-up");
  lcd.setCursor(3, 1);
  lcd.print(" secs.....");

  for (int x = 9; x > 0; x--) {
    lcd.setCursor(1, 1);
    lcd.print(x);
    digitalWrite(led_Amb, HIGH);
    delay(500);
    digitalWrite(led_Amb, LOW);
    delay(500);
  }
  lcd.clear();
  digitalWrite(led_Amb, HIGH);
}

void loop() {
  colorSwitch = colTemp_Read();
  delay(10);

  switch (colorSwitch) {
    case 1:
      lcd.clear();
      lcd.setCursor(1, 0); // set the cursor to column 15, line 0
      lcd.print("Object Detected:");
      lcd.setCursor(1, 1);
      lcd.print("Red Props");
      delay(100);
      break;

    case 2:
      lcd.clear();
      lcd.setCursor(1, 0); // set the cursor to column 15, line 0
      lcd.print("Object Detected:");
      lcd.setCursor(1, 1);
      lcd.print("Light Green Prop");
      delay(100);
      break;

    case 3:
      lcd.clear();
      lcd.setCursor(1, 0); // set the cursor to column 15, line 0
      lcd.print("Object Detected:");
      lcd.setCursor(1, 1);
      lcd.print("Blue Prop");
      delay(100);
      break;

    default:
      lcd.clear();
      lcd.setCursor(3, 0); // set the cursor to column 15, line 0
      lcd.print("System is");
      lcd.setCursor(2, 1); // set the cursor to column 15, line 0
      lcd.print("Scanning....");
      delay(100);
      break;
  }
}

int colTemp_Read () {
  // Yung Red Layer ang babasahin natin
  digitalWrite(pin_S2, LOW);
  digitalWrite(pin_S3, LOW);

  // kunin natin sa output pin yung red data
  pwm_freq = pulseIn(pin_Freq, LOW);
  temp_R = pwm_freq;
  delay(50);

  // Yung Green Layer ang babasahin natin
  digitalWrite(pin_S2, HIGH);
  digitalWrite(pin_S3, HIGH);

  // kunin natin sa output pin yung green data
  pwm_freq = pulseIn(pin_Freq, LOW);
  temp_G = pwm_freq;
  delay(50);


  // Yung Blue Layer ang babasahin natin
  digitalWrite(pin_S2, HIGH);
  digitalWrite(pin_S3, HIGH);

  // kunin natin sa output pin yung blue data
  pwm_freq = pulseIn(pin_Freq, LOW);
  temp_B = pwm_freq;
  delay(50);

  colorTemp = (temp_R + temp_G + temp_B) / 3;
  delay(50);

  if (colorTemp > 50 && colorTemp < 70) {
    av_Color = 1; //Red Propeller
  }

  if (colorTemp > 10 && colorTemp < 20) {
    av_Color = 2; //Light Green Propeller
  }

  if (colorTemp > 70 && colorTemp < 130) {
    av_Color = 3;//Blue Propeller
  }

   if (colorTemp > 140) {
    av_Color = 0;//Nothing
  }

  return av_Color;

}



Comments

Popular Posts