ESP8266 TCS3200D/TCS230 컬러 센서

이 튜토리얼은 TCS3200D/TCS230 컬러 센서를 ESP8266과 연결하여 정밀한 색상 감지 및 RGB 측정을 수행하는 방법을 안내합니다.

구체적으로 배우는 내용:

필요한 하드웨어

1×ESP8266 NodeMCU 쿠팡 | 아마존
1×USB 케이블 타입-A to 타입-C (USB-A PC용) 쿠팡 | 아마존
1×USB 케이블 타입-C to 타입-C (USB-C PC용) 아마존
1×TCS3200D/TCS230 Color Recognition Sensor Module 아마존
1×브레드보드 쿠팡 | 아마존
1×점퍼케이블 쿠팡 | 아마존
1×(옵션) DC 커넥터 전원 연결 잭 플러그 소켓 쿠팡 | 아마존
1×(추천) ESP8266용 스크루 터미널 확장 보드 쿠팡 | 아마존
1×(추천) ESP8266 Type-C용 전원 분배기 쿠팡 | 아마존
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

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에 연결합니다.
tcs3200 tcs230 컬러 센서 모듈 핀아웃 다이어그램

작동 원리

센서 동작은 두 가지 주요 제어에 의존합니다: 어떤 포토다이오드 필터 그룹을 활성화할지와 어떤 출력 주파수 범위를 생성할지입니다. 두 쌍의 제어 핀이 이 기능을 관리합니다:

주파수 스케일링 제어(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 형식으로 변환할 수 있습니다.

최적 정밀도 달성

  • 센서를 측정 대상에서 1-3cm 거리, 안정적인 각도로 유지합니다.
  • 반복 가능한 조명을 위해 내장 백색 LED 조명을 사용합니다.
  • 센서를 차폐하여 외부 주변광의 간섭을 줄입니다.

배선도

TCS3200 컬러 센서를 ESP8266에 다음과 같이 연결합니다:

TCS3200 Color SensorESP8266
VCC3V3
GNDGND
S0D6 (GPIO12)
S1D7 (GPIO13)
S2D2 (GPIO4)
S3D5 (GPIO14)
OUTD1 (GPIO5)
ESP8266 NodeMCU과 tcs3200 컬러 센서 배선도

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

ESP8266 핀배열ESP8266 전원을 켜는 방법에 대해 더 많이 보십시오.

ESP8266 코드 - 센서 보정

보정은 원시 측정값에서 환경적 간섭을 제거합니다. 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 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; }

빠른 시작

Arduino IDE에서 ESP8266을 시작하는 방법:

  • ESP8266이 처음이라면 ESP8266 - 소프트웨어 설치 튜토리얼을 확인하세요.
  • 배선도에 따라 부품을 연결합니다.
  • USB 케이블로 ESP8266 보드를 컴퓨터에 연결합니다.
  • 컴퓨터에서 Arduino IDE를 엽니다.
  • 올바른 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
···
8 Serial.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 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; }

빠른 시작

  • 코드 시작 부분의 보정 변수를 찾습니다:
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
  • 여섯 개의 0 자리 표시자를 측정된 보정 데이터로 교체합니다. 예: 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;
  • 업데이트된 코드를 ESP8266에 업로드합니다.
  • 센서 앞에 색상이 있는 물체를 놓습니다.
  • 시리얼 모니터에서 RGB 측정값을 확인합니다.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
Red = 210 - Green = 35 - Blue = 20 Red = 25 - Green = 200 - Blue = 40 Red = 30 - Green = 45 - Blue = 215
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

표시된 RGB 값은 표준 0-255 스케일을 따릅니다. 펄스 폭이 짧을수록(밝은 반사를 나타냄) RGB 출력이 높아지고, 펄스 폭이 길수록(어두운 반사) 낮은 값이 나옵니다.

RGB 값을 이용하여:

  • RGB 비율을 비교하여 특정 색상 식별
  • 색상 감지로 물체 분류
  • 서로 다른 샘플 간 색상 일치
  • 감지된 색상을 기반으로 동작 트리거

프로젝트 응용

RGB 측정 기능이 작동하면 다음을 개발할 수 있습니다:

  • 색상 분류 시스템: 색상별 물체 분류(적색/녹색/청색 구분)
  • 색상 검증 장치: 샘플 간 색상 일관성 확인
  • 색상 경로 추적 로봇: 색상 마커를 따라 이동하는 로봇
  • 시각적 품질 검사: 색상 분석으로 생산 결함 감지
  • 색상 트리거 자동화: 특정 색조가 감지될 때 동작 실행

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

관련 튜토리얼