공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
TCS3200D/TCS230 컬러 센서에 대하여
TCS3200D/TCS230 컬러 센서는 파장별 필터링을 통한 색상 감지를 위해 8×8 매트릭스로 배열된 64개의 포토다이오드 그리드를 활용합니다. 이 배열에서 16개의 포토다이오드는 적색 파장 필터를, 16개는 녹색 파장 필터를, 16개는 청색 파장 필터를 사용하며, 나머지 16개는 필터 없이 동작합니다(투명 응답). 색상 측정은 특정 필터 세트를 선택하고 출력 신호의 주파수를 평가하여 이루어집니다.
일반적인 TCS3200D 모듈에는 일관된 대상 조명을 제공하는 백색 LED 어레이가 내장되어 있어, 외부 조명 변화에 관계없이 측정 안정성을 보장하고 어두운 환경에서의 성능을 향상시킵니다.
핀아웃
TCS3200D/TCS230 센서 모듈의 핀 구성:
VCC 핀: 공급 전압 입력(+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 핀은 약 2Hz에서 500kHz의 구형파 주파수를 출력합니다. 빛의 강도가 높을수록 주파수가 높아집니다. ESP8266의 pulseIn() 함수로 펄스 지속 시간을 측정하며, 이는 반비례 관계입니다 — 밝은 조명일수록 지속 시간이 짧아집니다. 보정된 측정값을 통해 표준 0-255 RGB 형식으로 변환할 수 있습니다.
보정은 원시 측정값에서 환경적 간섭을 제거합니다. LED 출력 강도, 대상 거리, 재료 반사율, 실내 조명 등의 변수가 측정값에 영향을 줍니다. 보정 루틴은 모든 색상 채널에서 최소 및 최대 펄스 폭을 식별하여, 원시 데이터를 배포 환경에 맞는 정확한 0–255 RGB 값으로 변환하기 위한 기준 경계를 설정합니다.
/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-tcs3200d-tcs230-color-sensor */// Define color sensor pins#define PIN_S0 D6 // The ESP8266 pin GPIO12 connected to the S0 of the color module#define PIN_S1 D7 // The ESP8266 pin GPIO13 connected to the S1 of the color module#define PIN_S2 D2 // The ESP8266 pin GPIO4 connected to the S2 of the color module#define PIN_S3 D5 // The ESP8266 pin GPIO14 connected to the S3 of the color module#define PIN_sensorOut D1 // The ESP8266 pin GPIO5 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;}
올바른 ESP8266 보드(예: NodeMCU 1.0 (ESP-12E Module))와 해당 COM 포트를 선택합니다.
보정 코드를 복사하여 Arduino IDE에서 엽니다.
Upload 버튼을 클릭하여 ESP8266에 컴파일 및 업로드합니다.
시리얼 모니터를 시작합니다.
센서를 다양한 표면에 향하게 합니다: 흰색(종이), 검정색, 다양한 색상.
Min/Max 값이 자동으로 업데이트되는 것을 확인합니다.
값이 안정화되면(보통 10-20초), 여섯 개의 보정 매개변수를 기록합니다.
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E 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
Nodemcu 1.0 (ESP-12E Module) on COM15
2
위 출력에서 추출한 샘플 보정 매개변수:
RedMin = 42, redMax = 210
GreenMin = 55, greenMax = 185
BlueMin = 60, blueMax = 172
ESP8266 코드 - RGB 값 읽기
/* * 이 ESP8266 NodeMCU 코드는 newbiely.kr 에서 개발되었습니다 * 이 ESP8266 NodeMCU 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/esp8266/esp8266-tcs3200d-tcs230-color-sensor */// Define color sensor pins#define PIN_S0 D6 // The ESP8266 pin GPIO12 connected to the S0 of the color module#define PIN_S1 D7 // The ESP8266 pin GPIO13 connected to the S1 of the color module#define PIN_S2 D2 // The ESP8266 pin GPIO4 connected to the S2 of the color module#define PIN_S3 D5 // The ESP8266 pin GPIO14 connected to the S3 of the color module#define PIN_sensorOut D1 // The ESP8266 pin GPIO5 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
여섯 개의 0 자리 표시자를 측정된 보정 데이터로 교체합니다. 예: redMin = 42, redMax = 210, greenMin = 55, greenMax = 185, blueMin = 60, blueMax = 172를 사용하는 경우: