Cảm Biến Màu Sắc TCS3200 với Arduino

Phần cứng cần thiết:

Sơ đồ kết nối:

Code:

#define S0 6
#define S1 5
#define S2 7
#define S3 8
#define Led_R 4
#define Led_G 3
#define Led_B 2
#define sensorOut 9
int frequency = 0, color = 0;
int R = 0, G = 0, B = 0;

void setup()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(Led_R, OUTPUT);
  pinMode(Led_G, OUTPUT);
  pinMode(Led_B, OUTPUT);
  pinMode(sensorOut, INPUT);
  // Setting frequency-scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  Serial.begin(9600);
}
void loop()
{
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  //Remaping the value of the frequency to the RGB Model of
  0 to 255
  frequency = map(frequency, 25, 72, 255, 0);
  // Printing the value on the serial monitor
  Serial.print("R= ");
  //printing name
  R = frequency;
  Serial.print(frequency); //printing RED color frequency
  Serial.print("  ");
  delay(100);
  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  //Remaping the value of the frequency to the RGB Model of
  0 to 255
  frequency = map(frequency, 30, 90, 255, 0);
  // Printing the value on the serial monitor
  G = frequency;
  Serial.print("G= ");
  //printing name
  Serial.print(frequency); //printing RED color frequency
  Serial.print("  ");
  delay(100);
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  //Remaping the value of the frequency to the RGB Model of
  0 to 255
  frequency = map(frequency, 25, 70, 255, 0);
  // Printing the value on the serial monitor
  B = frequency;
  Serial.print("B= ");
  //printing name
  Serial.print(frequency); //printing RED color frequency
  Serial.println("  ");
  delay(100);
  if
  ((R > G) && (R > B) && (R > 280) && (R < 310))
  {
    Serial.println("This is Red color");
    digitalWrite(Led_R, HIGH);
  }
  else
  {
    digitalWrite(Led_R, LOW);
  }
  if
  ((G > R) && (G > B) && (G > 50))
  {
    Serial.println("This is Green color");
    digitalWrite(Led_G, HIGH);
  }
  else
  {
    digitalWrite(Led_G, LOW);
  }
  if
  ((B > R) && (B > G) && (B > 50 && (B < 270)))
  {
    Serial.println("This is Blue color");
    digitalWrite(Led_B, HIGH);
  }
  else
  {
    digitalWrite(Led_B, LOW);
  }
  delay(300);
}
cam bien mau sac tcs3200, tlc47 truong cong ly, trương công lý

Video:

 

Nhận xét

Mới hơn Cũ hơn