공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
TCS3200D/TCS230 컬러 센서에 대하여
TCS3200D/TCS230은 색상 식별을 위해 8×8 구성으로 구성된 64개의 광다이오드 매트릭스를 사용합니다. 이 배열은 빨간 광학 필터가 장착된 16개의 광다이오드, 녹색 필터가 장착된 16개, 파란 필터가 장착된 16개, 그리고 필터 없는(클리어) 16개로 구성됩니다. 센서는 특정 필터 유형을 선택하고 감지된 빛의 강도를 주파수 변조된 구형파 출력으로 변환하여 작동합니다.
대부분의 센서 모듈에는 통합된 백색 LED 조명이 있어 주변 조건에 독립적인 제어된 광원을 제공하여 측정 일관성을 유지합니다.
핀아웃
TCS3200D/TCS230 센서 모듈은 다음과 같은 연결 포인트를 제공합니다:
VCC 핀: 3.3V 또는 5V 전원에 연결합니다.
GND 핀: 접지(0V)에 연결합니다.
S0, S1 핀: 출력 주파수 스케일링 제어.
S2, S3 핀: 색상 필터 선택기.
OUT 핀: 구형파 주파수 출력 신호.
OE 핀: 출력 활성화(액티브 LOW). 일반적으로 상업용 모듈에서 GND에 미리 연결되어 있습니다. 노출된 경우 GND에 연결합니다.
작동 원리
센서 작동은 두 가지 구성 가능한 매개변수에 따라 달라집니다: 활성 색상 필터와 출력 신호 강도. 두 개의 제어 핀 쌍이 이 설정을 결정합니다:
S0/S1 핀은 출력 주파수 스케일링을 설정합니다:
S0=LOW, S1=LOW: 센서 전원 꺼짐
S0=LOW, S1=HIGH: 기본 주파수의 2% 출력
S0=HIGH, S1=LOW: 기본 주파수의 20% 출력
S0=HIGH, S1=HIGH: 기본 주파수의 100% 출력
S2/S3 핀은 활성 색상 필터를 선택합니다:
S2=LOW, S3=LOW: 빨간 필터 활성화
S2=LOW, S3=HIGH: 파란 필터 활성화
S2=HIGH, S3=LOW: 필터 없음(클리어/필터링 없음)
S2=HIGH, S3=HIGH: 녹색 필터 활성화
OUT 핀은 약 2 Hz에서 500 kHz 사이의 주파수를 출력합니다. 주파수 크기는 빛의 강도와 상관관계가 있습니다 — 빛이 많을수록 주파수가 높아집니다. pulseIn()을 사용하여 펄스 지속 시간을 측정하면 역수를 얻습니다 — 더 낮은 펄스 폭은 더 밝은 조명을 나타냅니다. 보정 후 이러한 측정값은 표준 0-255 RGB 값으로 변환됩니다.
ESP32 및 다른 구성 요소에 전원을 공급하는 방법에 대해 잘 알지 못하는 경우, 다음 튜토리얼에서 안내를 찾을 수 있습니다: ESP32 전원 공급 방법.
ESP32 코드 - 펄스 폭을 통한 센서 보정
보정은 원시 센서 판독값에 영향을 미치는 환경 변수를 보정합니다: LED 밝기 변동, 객체 거리, 표면 반사율 차이, 주변 조명 조건. 이러한 요인들은 측정 오류를 발생시키므로 정량화해야 합니다. 보정 과정은 각 색상 채널의 최솟값과 최댓값 펄스 폭을 기록하여 특정 환경에서 정확한 0–255 RGB 매핑을 위한 경계를 만듭니다.
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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 Measurementsint redPW = 0;int greenPW = 0;int bluePW = 0;// Variables to track min and max pulse widths for calibrationint redMin = 10000, redMax = 0;int greenMin = 10000, greenMax = 0;int blueMin = 10000, blueMax = 0;voidsetup() {// Set S0 - S3 as outputspinMode(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 inputpinMode(PIN_sensorOut, INPUT);// Setup Serial MonitorSerial.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("------------------------------------------");}voidloop() {// Read Red Pulse Width redPW = getRedPW();// Delay to stabilize sensordelay(200);// Read Green Pulse Width greenPW = getGreenPW();// Delay to stabilize sensordelay(200);// Read Blue Pulse Width bluePW = getBluePW();// Delay to stabilize sensordelay(200);// Update min and max valuesif (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/maxSerial.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 Widthsint getRedPW() {// Set sensor to read Red onlydigitalWrite(PIN_S2, LOW);digitalWrite(PIN_S3, LOW);// Read the Pulse Widthint PW = pulseIn(PIN_sensorOut, LOW);// Return the valuereturn PW;}// Function to read Green Pulse Widthsint getGreenPW() {// Set sensor to read Green onlydigitalWrite(PIN_S2, HIGH);digitalWrite(PIN_S3, HIGH);// Read the Pulse Widthint PW = pulseIn(PIN_sensorOut, LOW);// Return the valuereturn PW;}// Function to read Blue Pulse Widthsint getBluePW() {// Set sensor to read Blue onlydigitalWrite(PIN_S2, LOW);digitalWrite(PIN_S3, HIGH);// Read the Pulse Widthint PW = pulseIn(PIN_sensorOut, LOW);// Return the valuereturn PW;}
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
=== 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
------------------------------------------
Ln 11, Col 1
ESP32 Dev Module on COM15
2
출력에서 보정 결과 예시:
RedMin = 42, redMax = 210
GreenMin = 55, greenMax = 185
BlueMin = 60, blueMax = 172
ESP32 코드 - RGB 색상 값 읽기
/* * 이 ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/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 stepint redMin = 0; // Red minimum pulse widthint redMax = 0; // Red maximum pulse widthint greenMin = 0; // Green minimum pulse widthint greenMax = 0; // Green maximum pulse widthint blueMin = 0; // Blue minimum pulse widthint blueMax = 0; // Blue maximum pulse width// Variables for Color Pulse Width Measurementsint redPW = 0;int greenPW = 0;int bluePW = 0;// Variables for final Color valuesint redValue;int greenValue;int blueValue;voidsetup() {// Set S0 - S3 as outputspinMode(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 inputpinMode(PIN_sensorOut, INPUT);// Setup Serial MonitorSerial.begin(9600);}voidloop() {// Read Red value redPW = getRedPW();// Map to value from 0-255 redValue = map(redPW, redMin, redMax, 255, 0);// Delay to stabilize sensordelay(200);// Read Green value greenPW = getGreenPW();// Map to value from 0-255 greenValue = map(greenPW, greenMin, greenMax, 255, 0);// Delay to stabilize sensordelay(200);// Read Blue value bluePW = getBluePW();// Map to value from 0-255 blueValue = map(bluePW, blueMin, blueMax, 255, 0);// Delay to stabilize sensordelay(200);// Print output to Serial MonitorSerial.print("Red = ");Serial.print(redValue);Serial.print(" - Green = ");Serial.print(greenValue);Serial.print(" - Blue = ");Serial.println(blueValue);}// Function to read Red Pulse Widthsint getRedPW() {// Set sensor to read Red onlydigitalWrite(PIN_S2, LOW);digitalWrite(PIN_S3, LOW);// Read the Pulse Widthint PW = pulseIn(PIN_sensorOut, LOW);// Return the valuereturn PW;}// Function to read Green Pulse Widthsint getGreenPW() {// Set sensor to read Green onlydigitalWrite(PIN_S2, HIGH);digitalWrite(PIN_S3, HIGH);// Read the Pulse Widthint PW = pulseIn(PIN_sensorOut, LOW);// Return the valuereturn PW;}// Function to read Blue Pulse Widthsint getBluePW() {// Set sensor to read Blue onlydigitalWrite(PIN_S2, LOW);digitalWrite(PIN_S3, HIGH);// Read the Pulse Widthint PW = pulseIn(PIN_sensorOut, LOW);// Return the valuereturn PW;}
빠른 시작
코드 상단의 보정 상수를 찾습니다:
int redMin = 0; // Red minimum pulse widthint redMax = 0; // Red maximum pulse widthint greenMin = 0; // Green minimum pulse widthint greenMax = 0; // Green maximum pulse widthint blueMin = 0; // Blue minimum pulse widthint blueMax = 0; // Blue maximum pulse width
6개의 0을 실제 보정 값으로 교체합니다. 예를 들어(redMin = 42, redMax = 210, greenMin = 55, greenMax = 185, blueMin = 60, blueMax = 172 사용):