Pages: [1]   Go Down
Author Topic: Color sensor for Arduino?  (Read 7018 times)
0 Members and 1 Guest are viewing this topic.
Sulaymaniyah-Kurdistan-Iraq
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
stay foolish - stay hungry
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
i'm not very good at electronic and Arduino
i want to connect my arduino to a color sensor that can detect the 7 basic colors
so what is the best color sensor for arduino to work with it??
Please someone answer me
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8913
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Google has lots of answers:

TSC230 Color Sensor Demo For Arduino:
http://www.elecfreaks.com/1666.html

Sensing color with the ADJD-S311 + Arduino
http://bildr.org/2012/01/adjd-s311_arduino/

Sensing color with the ADJD-S371 + Arduino
http://bildr.org/2011/01/adjd-s371-tutorial/
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Jr. Member
**
Karma: 2
Posts: 86
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
i'm not very good at electronic and Arduino
i want to connect my arduino to a color sensor that can detect the 7 basic colors
so what is the best color sensor for arduino to work with it??
Please someone answer me


no need buying you can make your own, place a photoresistor next to a rbg leds or 3 leds and then measure which is higher, then you will find out ! here code:

EDIT: sorry didn't realize its 2 years old !

Code:
int CN[3]; // holds each color result
int avarage;
int blue = 11, green = 10, red = 9; // just to dont get messy !
int GV = 36 ,RV = 67 ,BV = 97;

int ARsure();

void setup(){
  Serial.begin(9600);
  
  pinMode(blue,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  
   //Starts Blue Calibrating
  BV = 100;
  analogWrite(blue,BV);
  while(ARsure() != 100){
    if(avarage > 100){
      BV--;
    } else if (avarage < 100) {
      BV++;
    }
    analogWrite(blue,BV);
  }
  
  digitalWrite(blue,LOW);
  //Blue end
  GV = 100;
  analogWrite(green,GV);
  while(ARsure() != 100){
    if(avarage > 100){
      GV--;
    } else if (avarage < 100) {
      GV++;
    }
    analogWrite(green,GV);
  }
  
   digitalWrite(green,LOW);
  //Green end
  
  RV = 100;
  analogWrite(red,RV);
  while(ARsure() != 100){
    if(avarage > 100){
      RV--;
    } else if (avarage < 100) {
      RV++;
    }
    analogWrite(red,RV);
  }
  
   digitalWrite(red,LOW);
  //Red end
  
}

void loop(){
  analogWrite(green,GV);
  delay(15);
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[0] = avarage/10;
  digitalWrite(green,LOW);
  
  //end
  
  analogWrite(blue,BV);
  delay(15); // delay for RBG LED to get its full brightness
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[1] = avarage/10; // reads photoresistor while the blue led is on (must see more light if object is blue);
  digitalWrite(blue,LOW); // turn off for next color
  
  //end
  
  analogWrite(red,RV);
  delay(15);
  
  avarage = 0;
  for(int i = 0;i<10;i++){
    avarage += analogRead(0);
    delay(15);
  }
  CN[2] = avarage/10;
  digitalWrite(red,LOW);
  
  if(CN[0] > CN[1] && CN[0] > CN[2]){
    Serial.println("Green!");
  } else
    if(CN[1] > CN[0] && CN[1] > CN[2]){
     Serial.println("Blue!");
    } else {
    if(CN[2] > CN[0] && CN[2] > CN[1]){
      Serial.println("Red!");
    }
  }
  Serial.print("Green: ");
  Serial.println(CN[0]);
  Serial.print("Blue: ");
  Serial.println(CN[1]);
  Serial.print("Red: ");
  Serial.println(CN[2]);
  Serial.println(" ------------------- END");
  
  
  delay(2000); // time to read and think !
}

int ARsure(){
  avarage = 0;
  for(int i=0;i<8;i++){
      delay(1);
      avarage += analogRead(0);
    }
    
    avarage = avarage / 8;
    //Serial.println(avarage);
    return avarage;
}
Logged

Pages: [1]   Go Up
Jump to: