ESP32 TCS3200D/TCS230 컬러 센서

이 가이드는 TCS3200D/TCS230 컬러 센서를 ESP32에 연결하여 정확한 RGB 색상 감지 및 측정을 수행하는 방법을 안내합니다. 센서를 보정하고 ESP32를 프로그래밍하여 객체에서 색상 값을 읽는 방법을 알아보세요.

배울 내용:

ESP32와 tcs3200d tcs230 컬러 인식 센서 모듈 튜토리얼

필요한 하드웨어

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow 쿠팡 | 아마존
1×(또는) 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide 쿠팡 | 아마존
1×(또는) 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide 아마존
1×(또는) ESP32 Uno-form board 아마존
1×(또는) ESP32 S3 Uno-form board 아마존
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×(추천) ESP32용 스크루 터미널 확장 보드 쿠팡 | 아마존
1×(추천) Breakout Expansion Board for ESP32 쿠팡 | 아마존
1×(추천) ESP32용 전원 분배기 쿠팡 | 아마존
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

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에 연결합니다.
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 핀은 약 2 Hz에서 500 kHz 사이의 주파수를 출력합니다. 주파수 크기는 빛의 강도와 상관관계가 있습니다 — 빛이 많을수록 주파수가 높아집니다. pulseIn()을 사용하여 펄스 지속 시간을 측정하면 역수를 얻습니다 — 더 낮은 펄스 폭은 더 밝은 조명을 나타냅니다. 보정 후 이러한 측정값은 표준 0-255 RGB 값으로 변환됩니다.

측정 안정성 최적화

  • 일관된 방향으로 대상 표면에서 1-3 cm 떨어진 위치에 센서를 놓습니다.
  • 표준화된 조명을 위해 내장 백색 LED를 활성화합니다.
  • 측정 변동성을 줄이기 위해 외부 광원을 차단합니다.

배선 다이어그램

ESP32에서 TCS3200 컬러 센서로의 연결 레이아웃:

TCS3200 Color SensorESP32
VCC3.3V
GNDGND
S0GPIO 17
S1GPIO 16
S2GPIO 18
S3GPIO 5
OUTGPIO 19
핀 연결을 보여주는 ESP32와 tcs3200 컬러 센서 배선 다이어그램

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

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 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; }

빠른 시작

  • ESP32가 처음이라면 ESP32 - 소프트웨어 설치 튜토리얼을 참조하세요.
  • 보정 코드를 Arduino IDE에 복사합니다.
  • Arduino IDE에서 업로드 버튼을 클릭하여 ESP32에 코드를 업로드합니다.
  • 시리얼 모니터를 엽니다(보드 레이트 9600으로 설정).
  • 센서를 흰 종이, 검은 표면, 다양한 색상 객체 등 다양한 색상의 물체에 가리킵니다.
  • 자동으로 업데이트되는 최솟값/최댓값을 모니터링합니다.
  • 값이 안정된 후(10-20초), 6개의 보정 숫자를 모두 기록합니다.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
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 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
  • 6개의 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;
  • 위의 코드를 복사하여 Arduino IDE로 엽니다.
  • Arduino IDE에서 업로드 버튼을 클릭하여 ESP32에 코드를 업로드합니다.
  • 센서 앞에 색상 있는 객체를 놓습니다.
  • 시리얼 모니터에서 결과를 확인합니다.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev 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
ESP32 Dev Module on COM15
2

출력은 이제 표준 0-255 RGB 값을 표시합니다. 더 짧은 펄스 폭(더 밝은 조명)은 더 높은 RGB 숫자를 생성하며; 더 긴 펄스 폭(더 어두운 조건)은 더 낮은 값을 생성합니다.

프로젝트 응용

RGB 읽기 기능이 작동하면 다음 프로젝트 가능성을 탐색해 보세요:

  • 자동화된 색상 분류: 색조(빨강, 초록, 파랑 식별)로 물건 분류
  • 색상 비교 시스템: 물체 간 색상 일치 확인
  • 색상 선 추적: 색상 경로를 따라가는 로봇 제작
  • 제조 품질 검사: 색상 편차를 통한 결함 품목 식별
  • 색상 트리거 반응: 특정 색상 감지 시 부저 또는 표시기 활성화

동영상

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