QC

Cảm biến màu Arduino

 Hướng dẫn cho cảm biến màu TCS230 / TCS3200 với Arduino


Cảm biến màu TCS3200 có thể phát hiện nhiều màu sắc khác nhau dựa trên bước sóng của chúng. Cảm biến này đặc biệt hữu ích cho các dự án nhận dạng màu sắc như kết hợp màu, phân loại màu, đọc dải thử nghiệm và hơn thế nữa.

Cảm biến màu TCS3200 - được hiển thị trong hình bên dưới - sử dụng chip cảm biến TAOS TCS3200 RGB để phát hiện màu sắc. Nó cũng chứa bốn đèn LED màu trắng làm sáng vật thể phía trước.

Thông số kỹ thuật

Dưới đây là thông số kỹ thuật của cảm biến:

Nguồn: 2.7V đến 5.5V

Kích thước: 28,4 x 28,4mm (1,12 x 1,12 ″)

Giao diện: kỹ thuật số TTL

Độ phân giải cao chuyển đổi cường độ ánh sáng thành tần số

Màu có thể lập trình và tần số đầu ra toàn quy mô

Giao tiếp trực tiếp với vi điều khiển

Cảm biến TCS3200 hoạt động như thế nào?

TCS3200 có một loạt các điốt quang với 4 bộ lọc khác nhau. Điốt quang đơn giản là một thiết bị bán dẫn chuyển đổi ánh sáng thành dòng điện. Cảm biến có:

16 điốt quang với bộ lọc màu đỏ - nhạy cảm với bước sóng màu đỏ

16 điốt quang với bộ lọc màu xanh lá cây - nhạy cảm với bước sóng màu xanh lá cây

16 điốt quang với bộ lọc màu xanh lam - nhạy cảm với bước sóng màu xanh lam

16 điốt quang không có bộ lọc

Nếu bạn xem xét kỹ hơn chip TCS3200, bạn có thể thấy các bộ lọc khác nhau.



Bằng cách chọn có chọn lọc các số đọc của bộ lọc điốt quang, bạn có thể phát hiện cường độ của các màu khác nhau. Cảm biến có bộ chuyển đổi dòng điện sang tần số để chuyển đổi số đọc của điốt quang thành sóng vuông có tần số tỷ lệ với cường độ ánh sáng của màu đã chọn. Tần số này sau đó, được đọc bởi Arduino - điều này được thể hiện trong hình bên dưới.



SƠ ĐỒ CHÂN.



Dưới đây là các kết nối giữa TCSP3200 và Arduino:

  • S0: digital pin 4
  • S1: digital pin 5
  • VCC: 5V
  • S3: digital pin 6
  • S4: digital pin 7
  • OUT: digital pin 8

Code

Bạn cần hai bản DA này:

-Đọc và hiển thị tần số đầu ra trên màn hình nối tiếp. Trong phần này, bạn cần ghi các giá trị tần số khi bạn đặt các màu khác nhau trước cảm biến.
-Phân biệt giữa các màu khác nhau. Trong phần này, bạn sẽ chèn các giá trị tần số đã chọn trước đó vào mã của mình để cảm biến của bạn có thể phân biệt giữa các màu khác nhau. Chúng tôi sẽ phát hiện các màu đỏ, xanh lục và xanh lam.

1. Reading the output frequency

BẠN LẤY CODE NẠP VÀO RỒI ĐƯA TỚI GẦN TẤM BÌA MÀU XANH NHÉ !




// TCS230 or TCS3200 pins wiring to Arduino
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

void setup() {
  // Setting the outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);
  
  // Setting frequency scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
   // Begins serial communication 
  Serial.begin(9600);
}
void loop() {
  // Setting RED (R) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  
  // Reading the output frequency
  redFrequency = pulseIn(sensorOut, LOW);
  
   // Printing the RED (R) value
  Serial.print("R = ");
  Serial.print(redFrequency);
  delay(100);
  
  // Setting GREEN (G) filtered photodiodes to be read
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  greenFrequency = pulseIn(sensorOut, LOW);
  
  // Printing the GREEN (G) value  
  Serial.print(" G = ");
  Serial.print(greenFrequency);
  delay(100);
 
  // Setting BLUE (B) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  blueFrequency = pulseIn(sensorOut, LOW);
  
  // Printing the BLUE (B) value 
  Serial.print(" B = ");
  Serial.println(blueFrequency);
  delay(100);
}

ĐƯA GẦN VÀ RA XA GIÁ TRỊ NÓ SẼ THAY ĐỔI NHƯ BẢNG SAU.



CODE PHÁT HIỆN 3 MÀU RGB.



// TCS230 or TCS3200 pins wiring to Arduino
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

// Stores the red. green and blue colors
int redColor = 0;
int greenColor = 0;
int blueColor = 0;

void setup() {
  // Setting the outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);
  
  // Setting frequency scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Begins serial communication
  Serial.begin(9600);
}

void loop() {
  // Setting RED (R) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  
  // Reading the output frequency
  redFrequency = pulseIn(sensorOut, LOW);
  // Remaping the value of the RED (R) frequency from 0 to 255
  // You must replace with your own values. Here's an example: 
  // redColor = map(redFrequency, 70, 120, 255,0);
  redColor = map(redFrequency, XX, XX, 255,0);
  
  // Printing the RED (R) value
  Serial.print("R = ");
  Serial.print(redColor);
  delay(100);
  
  // Setting GREEN (G) filtered photodiodes to be read
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  greenFrequency = pulseIn(sensorOut, LOW);
  // Remaping the value of the GREEN (G) frequency from 0 to 255
  // You must replace with your own values. Here's an example: 
  // greenColor = map(greenFrequency, 100, 199, 255, 0);
  greenColor = map(greenFrequency, XX, XX, 255, 0);
  
  // Printing the GREEN (G) value  
  Serial.print(" G = ");
  Serial.print(greenColor);
  delay(100);
 
  // Setting BLUE (B) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  blueFrequency = pulseIn(sensorOut, LOW);
  // Remaping the value of the BLUE (B) frequency from 0 to 255
  // You must replace with your own values. Here's an example: 
  // blueColor = map(blueFrequency, 38, 84, 255, 0);
  blueColor = map(blueFrequency, XX, XX, 255, 0);
  
  // Printing the BLUE (B) value 
  Serial.print(" B = ");
  Serial.print(blueColor);
  delay(100);

  // Checks the current detected color and prints
  // a message in the serial monitor
  if(redColor > greenColor && redColor > blueColor){
      Serial.println(" - RED detected!");
  }
  if(greenColor > redColor && greenColor > blueColor){
    Serial.println(" - GREEN detected!");
  }
  if(blueColor > redColor && blueColor > greenColor){
    Serial.println(" - BLUE detected!");
  }
}






Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...