아두이노 나노 TCS3200D/TCS230 컬러 센서

이 튜토리얼은 TCS3200D/TCS230 색상 인식 센서를 Arduino Nano와 연결하여 다양한 물체의 RGB 값을 정밀하게 측정하고 식별하는 방법을 시연합니다. 센서 보정 기법과 색상 감지 프로그래밍을 마스터할 수 있습니다.

다루는 주제:

아두이노 나노와 tcs3200d tcs230 색상 인식 센서 모듈 튜토리얼

필요한 하드웨어

1×아두이노 나노 쿠팡 | 아마존
1×USB A to Mini-B USB 케이블 쿠팡 | 아마존
1×TCS3200D/TCS230 Color Recognition Sensor Module 아마존
1×브레드보드 쿠팡 | 아마존
1×점퍼케이블 쿠팡 | 아마존
1×(추천) 아두이노 나노용 스크루 터미널 확장 보드 쿠팡 | 아마존
1×(추천) 아두이노 나노용 브레이크아웃 확장 보드 쿠팡 | 아마존
1×(추천) 아두이노 나노용 전원 분배기 쿠팡 | 아마존
공개: 이 포스팅 에 제공된 일부 링크는 아마존 제휴 링크입니다. 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

TCS3200D/TCS230 컬러 센서에 대하여

8×8 포토다이오드 배열을 기반으로 한 TCS3200D/TCS230은 특수 광학 필터링을 통해 색상을 식별합니다. 64개의 다이오드 격자에는 빨간색 필터 포토다이오드 16개, 녹색 필터 16개, 파란색 필터 센서 16개, 필터 없음(투명) 포토다이오드 16개가 포함됩니다. 색상 감지는 필터 유형을 전환하고 빛의 강도를 나타내는 구형파 주파수 출력을 분석하여 이루어집니다.

대부분의 모듈에 내장된 흰색 LED는 일관된 대상 조명을 제공하여 다양한 주변 조건에서 안정적인 측정을 보장하고 저조도 성능을 향상시킵니다.

핀아웃

TCS3200D/TCS230 모듈의 연결 핀:

  • VCC 핀: 전원 공급 연결(+5V).
  • GND 핀: 접지 연결(0V).
  • S0, S1 핀: 주파수 스케일링 제어 입력.
  • S2, S3 핀: 색상 필터 선택 입력.
  • OUT 핀: 주파수 출력(구형파).
  • OE 핀: 출력 활성화 제어(LOW일 때 활성). 대부분의 상용 모듈은 이 핀을 내부적으로 접지시켜 외부 배선을 생략합니다. 미리 연결되어 있지 않은 경우 GND에 수동으로 연결합니다.
tcs3200 tcs230 컬러 센서 모듈 핀아웃 다이어그램

작동 원리

센서를 작동하려면 두 가지 핵심 매개변수를 구성해야 합니다: 활성 색상 채널과 신호 강도. 제어 핀 쌍이 이 설정을 관리합니다:

S0과 S1을 통한 출력 스케일링:

  • 둘 다 LOW: 전원 끄기 모드
  • S0 LOW, S1 HIGH: 2% 주파수 스케일링
  • S0 HIGH, S1 LOW: 20% 주파수 스케일링
  • 둘 다 HIGH: 전체 100% 스케일링

S2와 S3을 통한 필터 선택:

  • 둘 다 LOW: 빨간색 채널 활성
  • S2 LOW, S3 HIGH: 파란색 채널 활성
  • S2 HIGH, S3 LOW: 투명 채널(필터 없음)
  • 둘 다 HIGH: 녹색 채널 활성

OUT 핀은 약 2Hz에서 500kHz 범위의 구형파 신호를 생성합니다. 주파수가 높을수록 선택된 채널의 빛 강도가 강합니다. pulseIn()으로 펄스 폭을 측정하면 역 관계를 얻습니다 — 펄스가 짧을수록 색상이 밝습니다. 보정 후 변환하면 표준 RGB 값을 얻을 수 있습니다.

판독 정확도 최대화

  • 센서와 대상 사이의 거리를 1~3cm로 유지하고 위치를 일정하게 합니다.
  • 온보드 흰색 LED를 활성화하여 균일한 조명 조건을 만듭니다.
  • 더 높은 정밀도를 위해 센서를 차폐하여 주변광 간섭을 최소화합니다.

배선도

TCS3200 컬러 센서와 Arduino Nano의 연결 회로도:

TCS3200 컬러 센서 Arduino Nano
VCC 5V
GND GND
S0 D4
S1 D3
S2 D6
S3 D5
OUT D7
아두이노 나노와 tcs3200 컬러 센서 배선도

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

Arduino Nano와 기타 부품에 전원을 공급하는 가장 효과적인 방법은 다음 링크를 참조하세요: 아두이노 나노 전원 공급 방법.

Arduino Nano 코드 - 펄스 폭을 통한 센서 보정

환경 보정은 원시 센서 출력이 여러 요소(온보드 LED 강도, 대상 거리, 표면 반사 특성, 주변 조명)에 따라 달라지기 때문에 필수적입니다. 이러한 변수를 측정 간섭으로 생각할 수 있습니다. 보정은 각 색상 채널의 최소 및 최대 경계를 설정하여 이 간섭을 정량화하고, 특정 설정에 맞는 표준 0~255 RGB 스케일로 정확한 변환을 가능하게 합니다.

/* * 이 아두이노 나노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 나노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano/arduino-nano-tcs3200d-tcs230-color-sensor */ // Define color sensor pins #define PIN_S0 4 #define PIN_S1 3 #define PIN_S2 6 #define PIN_S3 5 #define PIN_sensorOut 7 // 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에서 보정 코드를 엽니다
  • USB 케이블로 Arduino Nano를 연결하고 Upload 버튼을 사용하여 업로드합니다
  • 시리얼 모니터를 열어 Min/Max 추적이 포함된 실시간 판독값을 확인합니다
  • 센서로 다양한 물체를 스캔합니다: 흰색 표면(종이), 검은색 물체, 다양한 색상 물체
  • 극값이 기록되면서 자동으로 Min/Max 값이 업데이트되는 것을 관찰합니다
  • 숫자가 안정화되면(일반적으로 10~20초), 여섯 가지 보정 상수를 모두 기록합니다
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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
Arduino Nano on COM15
2

예를 들어 위의 출력에서 보정 값은 다음과 같습니다:

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

Arduino Nano 코드 - RGB 색상 값 읽기

/* * 이 아두이노 나노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 나노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano/arduino-nano-tcs3200d-tcs230-color-sensor */ // Define color sensor pins #define PIN_S0 4 #define PIN_S1 3 #define PIN_S2 6 #define PIN_S3 5 #define PIN_sensorOut 7 // 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;
  • 수정된 코드를 Arduino Nano에 업로드합니다
  • 센서 앞에 색상 대상을 놓습니다
  • 시리얼 모니터에서 RGB 출력을 모니터링합니다
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano' 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
Arduino Nano on COM15
2

RGB 값은 이제 표준 0~255 범위로 매핑됩니다. 짧은 펄스 폭(더 많은 빛)은 더 높은 RGB 값을 생성하고, 긴 펄스 폭(적은 빛)은 더 낮은 RGB 값을 생성합니다.

응용 분야

이제 RGB 값을 읽을 수 있으므로 다음과 같은 프로젝트를 만들 수 있습니다:

  • 색상 분류기: 색상(빨강, 녹색, 파랑)으로 물체 분류
  • 색상 매칭 게임: 두 물체의 색상이 같은지 확인
  • 라인 팔로워 로봇: 바닥의 색상 선 따라가기
  • 품질 관리: 색상으로 불량 제품 감지
  • 색상 활성화 경보: 특정 색상이 감지될 때 버저 또는 LED 트리거

동영상

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

관련 튜토리얼