ESP32 Cảm Biến Màu TCS3200D/TCS230

Hướng dẫn này sẽ giúp bạn kết nối cảm biến màu TCS3200D/TCS230 với ESP32 để phát hiện và đo màu RGB một cách chính xác. Học cách hiệu chuẩn cảm biến và lập trình ESP32 để đọc giá trị màu từ các vật thể.

Những gì bạn sẽ học được:

ESP32 với module cảm biến nhận dạng màu tcs3200d tcs230 tutorial

Phần Cứng Cần Thiết

1×mô-đun phát triển ESP-WROOM-32
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×Cáp USB Type-C
1×Module Cảm Biến Nhận Dạng Màu TCS3200D/TCS230
1×breadboard (Bo mạch thí nghiệm)
1×Dây Jumper
1×(Khuyến nghị) Screw Terminal Expansion Board for ESP32
1×(Khuyến nghị) Breakout Expansion Board for ESP32
1×(Khuyến nghị) Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)

Về Cảm Biến Màu TCS3200D/TCS230

TCS3200D/TCS230 sử dụng ma trận 64 photodiode được sắp xếp theo cấu hình 8×8 để nhận dạng màu sắc. Ma trận này bao gồm 16 photodiode được trang bị bộ lọc quang học màu đỏ, 16 với bộ lọc xanh lá cây, 16 với bộ lọc xanh dương, và 16 không có bộ lọc (trong suốt). Cảm biến hoạt động bằng cách chọn các loại bộ lọc cụ thể và chuyển đổi cường độ ánh sáng phát hiện được thành tín hiệu đầu ra sóng vuông điều chế tần số.

Hầu hết các module cảm biến đều có hệ thống chiếu sáng LED trắng tích hợp, giúp duy trì tính nhất quán của phép đo bằng cách cung cấp nguồn sáng được kiểm soát độc lập với điều kiện ánh sáng xung quanh.

Sơ Đồ Chân

Module cảm biến TCS3200D/TCS230 cung cấp các điểm kết nối sau:

  • Chân VCC: Kết nối với nguồn cung cấp 3.3V hoặc 5V.
  • Chân GND: Kết nối với mass (0V).
  • Chân S0, S1: Điều khiển tỉ lệ tần số đầu ra.
  • Chân S2, S3: Bộ chọn bộ lọc màu.
  • Chân OUT: Tín hiệu đầu ra tần số sóng vuông.
  • Chân OE: Kích hoạt đầu ra (active LOW). Thường được nối sẵn với GND trên các module thương mại. Nếu có tiếp cận được, hãy nối với GND.
sơ đồ chân module cảm biến màu tcs3200 tcs230 hiển thị các chân vcc gnd s0 s1 s2 s3 out

Cách Hoạt Động

Hoạt động của cảm biến phụ thuộc vào hai tham số có thể cấu hình: bộ lọc màu hoạt động và cường độ tín hiệu đầu ra. Hai cặp chân điều khiển xác định các cài đặt này:

Chân S0/S1 cấu hình tỉ lệ tần số đầu ra:

  • S0=LOW, S1=LOW: Cảm biến tắt nguồn
  • S0=LOW, S1=HIGH: Đầu ra ở 2% tần số cơ bản
  • S0=HIGH, S1=LOW: Đầu ra ở 20% tần số cơ bản
  • S0=HIGH, S1=HIGH: Đầu ra ở 100% tần số cơ bản

Chân S2/S3 chọn bộ lọc màu hoạt động:

  • S2=LOW, S3=LOW: Kích hoạt bộ lọc đỏ
  • S2=LOW, S3=HIGH: Kích hoạt bộ lọc xanh dương
  • S2=HIGH, S3=LOW: Không có bộ lọc (trong suốt/không lọc)
  • S2=HIGH, S3=HIGH: Kích hoạt bộ lọc xanh lá cây

Chân OUT tạo ra tần số đầu ra từ khoảng 2 Hz đến 500 kHz. Độ lớn tần số tương quan với cường độ ánh sáng—nhiều ánh sáng hơn cho tần số cao hơn. Sử dụng pulseIn() để đo độ rộng xung cho kết quả ngược lại—độ rộng xung thấp hơn cho thấy ánh sáng sáng hơn. Sau khi hiệu chuẩn, các phép đo này chuyển đổi thành giá trị RGB tiêu chuẩn 0-255.

Tối Ưu Hóa Độ Ổn Định Đo Lường

  • Đặt cảm biến cách bề mặt đích 1-3 cm với hướng nhất quán.
  • Bật LED trắng tích hợp để có ánh sáng tiêu chuẩn hóa.
  • Chặn các nguồn sáng bên ngoài để giảm biến đổi đo lường.

Sơ Đồ Đấu Nối

Sơ đồ kết nối ESP32 với cảm biến màu TCS3200:

Cảm Biến Màu TCS3200ESP32
VCC3.3V
GNDGND
S0GPIO 17
S1GPIO 16
S2GPIO 18
S3GPIO 5
OUTGPIO 19
sơ đồ đấu nối ESP32 và cảm biến màu tcs3200 hiển thị kết nối giữa các chân

This image is created using Fritzing. Click to enlarge image

Nếu bạn chưa rõ cách cấp nguồn cho ESP32 và các linh kiện khác, xem: Cách Cung Cấp Nguồn Điện Cho ESP32.

Code ESP32 - Hiệu Chuẩn Cảm Biến Qua Độ Rộng Xung

Hiệu chuẩn bù trừ các biến số môi trường ảnh hưởng đến kết quả đọc thô của cảm biến: biến động độ sáng LED, khoảng cách vật thể, sự khác biệt về độ phản xạ bề mặt, và điều kiện ánh sáng xung quanh. Những yếu tố này tạo ra lỗi đo lường cần được định lượng. Quá trình hiệu chuẩn ghi lại độ rộng xung tối thiểu và tối đa cho mỗi kênh màu, tạo ra các ranh giới để ánh xạ RGB 0–255 chính xác trong môi trường cụ thể của bạn.

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-tcs3200d-tcs230-color-sensor */ // Define color sensor pins #define PIN_S0 17 // The ESP32 pin connected to the S0 of the color module #define PIN_S1 16 // The ESP32 pin connected to the S1 of the color module #define PIN_S2 18 // The ESP32 pin connected to the S2 of the color module #define PIN_S3 5 // The ESP32 pin connected to the S3 of the color module #define PIN_sensorOut 19 // The ESP32 pin connected to the OUT of the color module // Variables for Color Pulse Width Measurements int redPW = 0; int greenPW = 0; int bluePW = 0; // Variables to track min and max pulse widths for calibration int redMin = 10000, redMax = 0; int greenMin = 10000, greenMax = 0; int blueMin = 10000, blueMax = 0; void setup() { // Set S0 - S3 as outputs pinMode(PIN_S0, OUTPUT); pinMode(PIN_S1, OUTPUT); pinMode(PIN_S2, OUTPUT); pinMode(PIN_S3, OUTPUT); // Set Pulse Width scaling to 20% digitalWrite(PIN_S0, HIGH); digitalWrite(PIN_S1, LOW); // Set Sensor output as input pinMode(PIN_sensorOut, INPUT); // Setup Serial Monitor Serial.begin(9600); Serial.println("=== TCS3200 Calibration ==="); Serial.println("Point the sensor at different objects (white, black, colors)."); Serial.println("Min and Max values are tracked automatically."); Serial.println("When values look stable, note them down for the next code."); Serial.println("------------------------------------------"); } void loop() { // Read Red Pulse Width redPW = getRedPW(); // Delay to stabilize sensor delay(200); // Read Green Pulse Width greenPW = getGreenPW(); // Delay to stabilize sensor delay(200); // Read Blue Pulse Width bluePW = getBluePW(); // Delay to stabilize sensor delay(200); // Update min and max values if (redPW < redMin) redMin = redPW; if (redPW > redMax) redMax = redPW; if (greenPW < greenMin) greenMin = greenPW; if (greenPW > greenMax) greenMax = greenPW; if (bluePW < blueMin) blueMin = bluePW; if (bluePW > blueMax) blueMax = bluePW; // Print the pulse width values with min/max Serial.print("Red PW = "); Serial.print(redPW); Serial.print(" - Green PW = "); Serial.print(greenPW); Serial.print(" - Blue PW = "); Serial.println(bluePW); Serial.print(" Min -> R:"); Serial.print(redMin); Serial.print(" G:"); Serial.print(greenMin); Serial.print(" B:"); Serial.println(blueMin); Serial.print(" Max -> R:"); Serial.print(redMax); Serial.print(" G:"); Serial.print(greenMax); Serial.print(" B:"); Serial.println(blueMax); Serial.println("------------------------------------------"); delay(1000); } // Function to read Red Pulse Widths int getRedPW() { // Set sensor to read Red only digitalWrite(PIN_S2, LOW); digitalWrite(PIN_S3, LOW); // Read the Pulse Width int PW = pulseIn(PIN_sensorOut, LOW); // Return the value return PW; } // Function to read Green Pulse Widths int getGreenPW() { // Set sensor to read Green only digitalWrite(PIN_S2, HIGH); digitalWrite(PIN_S3, HIGH); // Read the Pulse Width int PW = pulseIn(PIN_sensorOut, LOW); // Return the value return PW; } // Function to read Blue Pulse Widths int getBluePW() { // Set sensor to read Blue only digitalWrite(PIN_S2, LOW); digitalWrite(PIN_S3, HIGH); // Read the Pulse Width int PW = pulseIn(PIN_sensorOut, LOW); // Return the value return PW; }

Các Bước Nhanh

  • Nếu đây là lần đầu tiên bạn sử dụng ESP32, hãy xem ESP32 - Cài Đặt Phần Mềm.
  • Sao chép code hiệu chuẩn vào Arduino IDE
  • Nhấp vào nút Upload trên Arduino IDE để tải code lên ESP32
  • Mở Serial Monitor (đặt baud rate là 9600)
  • Hướng cảm biến về các vật thể có màu khác nhau: giấy trắng, bề mặt đen, và các màu khác nhau
  • Theo dõi các giá trị Min/Max khi chúng tự động cập nhật
  • Sau khi các giá trị ổn định (10-20 giây), ghi lại tất cả sáu số hiệu chuẩn
COM6
Send
=== TCS3200 Calibration === Point the sensor at different objects (white, black, colors). Min and Max values are tracked automatically. When values look stable, note them down for the next code. ------------------------------------------ Red PW = 42 - Green PW = 55 - Blue PW = 60 Min -> R:42 G:55 B:60 Max -> R:42 G:55 B:60 ------------------------------------------ Red PW = 210 - Green PW = 185 - Blue PW = 172 Min -> R:42 G:55 B:60 Max -> R:210 G:185 B:172 ------------------------------------------ Red PW = 44 - Green PW = 57 - Blue PW = 61 Min -> R:42 G:55 B:60 Max -> R:210 G:185 B:172 ------------------------------------------
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Kết quả hiệu chuẩn ví dụ từ đầu ra:

  • RedMin = 42, redMax = 210
  • GreenMin = 55, greenMax = 185
  • BlueMin = 60, blueMax = 172

Code ESP32 - Đọc Giá Trị Màu RGB

/* * Mã ESP32 này được phát triển bởi newbiely.vn * Mã ESP32 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/esp32/esp32-tcs3200d-tcs230-color-sensor */ // Define color sensor pins #define PIN_S0 17 // The ESP32 pin connected to the S0 of the color module #define PIN_S1 16 // The ESP32 pin connected to the S1 of the color module #define PIN_S2 18 // The ESP32 pin connected to the S2 of the color module #define PIN_S3 5 // The ESP32 pin connected to the S3 of the color module #define PIN_sensorOut 19 // The ESP32 pin connected to the OUT of the color module // Calibration Values // Replace these values with your actual calibration data from the previous step int redMin = 0; // Red minimum pulse width int redMax = 0; // Red maximum pulse width int greenMin = 0; // Green minimum pulse width int greenMax = 0; // Green maximum pulse width int blueMin = 0; // Blue minimum pulse width int blueMax = 0; // Blue maximum pulse width // Variables for Color Pulse Width Measurements int redPW = 0; int greenPW = 0; int bluePW = 0; // Variables for final Color values int redValue; int greenValue; int blueValue; void setup() { // Set S0 - S3 as outputs pinMode(PIN_S0, OUTPUT); pinMode(PIN_S1, OUTPUT); pinMode(PIN_S2, OUTPUT); pinMode(PIN_S3, OUTPUT); // Set Pulse Width scaling to 20% digitalWrite(PIN_S0, HIGH); digitalWrite(PIN_S1, LOW); // Set Sensor output as input pinMode(PIN_sensorOut, INPUT); // Setup Serial Monitor Serial.begin(9600); } void loop() { // Read Red value redPW = getRedPW(); // Map to value from 0-255 redValue = map(redPW, redMin, redMax, 255, 0); // Delay to stabilize sensor delay(200); // Read Green value greenPW = getGreenPW(); // Map to value from 0-255 greenValue = map(greenPW, greenMin, greenMax, 255, 0); // Delay to stabilize sensor delay(200); // Read Blue value bluePW = getBluePW(); // Map to value from 0-255 blueValue = map(bluePW, blueMin, blueMax, 255, 0); // Delay to stabilize sensor delay(200); // Print output to Serial Monitor Serial.print("Red = "); Serial.print(redValue); Serial.print(" - Green = "); Serial.print(greenValue); Serial.print(" - Blue = "); Serial.println(blueValue); } // Function to read Red Pulse Widths int getRedPW() { // Set sensor to read Red only digitalWrite(PIN_S2, LOW); digitalWrite(PIN_S3, LOW); // Read the Pulse Width int PW = pulseIn(PIN_sensorOut, LOW); // Return the value return PW; } // Function to read Green Pulse Widths int getGreenPW() { // Set sensor to read Green only digitalWrite(PIN_S2, HIGH); digitalWrite(PIN_S3, HIGH); // Read the Pulse Width int PW = pulseIn(PIN_sensorOut, LOW); // Return the value return PW; } // Function to read Blue Pulse Widths int getBluePW() { // Set sensor to read Blue only digitalWrite(PIN_S2, LOW); digitalWrite(PIN_S3, HIGH); // Read the Pulse Width int PW = pulseIn(PIN_sensorOut, LOW); // Return the value return PW; }

Các Bước Nhanh

  • Tìm các hằng số hiệu chuẩn ở đầu code:
int redMin = 0; // Red minimum pulse width int redMax = 0; // Red maximum pulse width int greenMin = 0; // Green minimum pulse width int greenMax = 0; // Green maximum pulse width int blueMin = 0; // Blue minimum pulse width int blueMax = 0; // Blue maximum pulse width
  • Thay thế tất cả sáu số 0 bằng các giá trị hiệu chuẩn thực tế của bạn. Ví dụ (sử dụng redMin = 42, redMax = 210, greenMin = 55, greenMax = 185, blueMin = 60, blueMax = 172):
int redMin = 42; int redMax = 210; int greenMin = 55; int greenMax = 185; int blueMin = 60; int blueMax = 172;
  • Sao chép code trên và mở bằng Arduino IDE
  • Nhấp nút Upload trên Arduino IDE để tải code lên ESP32
  • Đặt một vật thể có màu phía trước cảm biến
  • Xem kết quả trên Serial Monitor
COM6
Send
Red = 210 - Green = 35 - Blue = 20 Red = 25 - Green = 200 - Blue = 40 Red = 30 - Green = 45 - Blue = 215
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Đầu ra bây giờ hiển thị giá trị RGB tiêu chuẩn 0-255. Độ rộng xung ngắn hơn (ánh sáng sáng hơn) cho số RGB cao hơn; độ rộng xung dài hơn (điều kiện tối hơn) tạo ra giá trị thấp hơn.

Ý Tưởng Ứng Dụng Dự Án

Với khả năng đọc RGB hoạt động, hãy khám phá những khả năng dự án sau:

  • Hệ thống phân loại màu tự động: Phân loại vật phẩm theo màu sắc (nhận dạng đỏ, xanh lá, xanh dương)
  • Hệ thống so sánh màu: Xác minh sự phù hợp màu sắc giữa các vật thể
  • Theo dõi đường màu: Xây dựng robot theo dõi các đường có màu
  • Kiểm tra chất lượng sản xuất: Nhận dạng các đơn vị lỗi thông qua độ lệch màu
  • Phản hồi kích hoạt màu: Kích hoạt buzzer hoặc chỉ báo khi phát hiện màu cụ thể

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Bài hướng dẫn liên quan