본문 바로가기
Real Cart 프로젝트

[EP.5] 라즈베리파이에서 컬러 센서(TCS230) 제어

by code_killer 2023. 2. 19.
728x90
반응형

1. 컬러 센서(TCS 230)

1.1  Terminal Functions

Terminal name I/O Description
GND   Power supply ground, All voltages are referenced to GND.
OE I Enable for fo (active low).
OUT O Output frequency(fo).
S0, S1 I Output frequency scailing selection inputs.
S2, S3 I Photodiode type selection inputs.
Vdd   supply voltage

 

1.2 Selectable Options

S0 S1 Output Frequency Scailing   S2 S3 Photodiode Type
L L Power Down   L L Red
L H 2%   L H Blue
H L 20%   H L Clear (no filter)
H H 100%   H H Green

 

1.3  회로 연결 (아두이노 기준)

 

2.  컬러 센서 테스트 코드

import RPi.GPIO as GPIO
import time

# GPIO 핀 번호 설정
S0 = 11
S1 = 12
S2 = 13
S3 = 15
OUT = 16

# GPIO 핀 모드 설정
GPIO.setmode(GPIO.BOARD)
GPIO.setup(S0, GPIO.OUT)
GPIO.setup(S1, GPIO.OUT)
GPIO.setup(S2, GPIO.OUT)
GPIO.setup(S3, GPIO.OUT)
GPIO.setup(OUT, GPIO.IN)

# 출력 주파수 설정
GPIO.output(S0, GPIO.LOW)
GPIO.output(S1, GPIO.HIGH)

# 색상 감지 함수
def read_color():
    # 색상 감지 주기 설정
    time.sleep(0.1)

    # RGB 값 읽기
    GPIO.output(S2, GPIO.LOW)
    GPIO.output(S3, GPIO.LOW)
    r = GPIO.input(OUT)

    GPIO.output(S2, GPIO.HIGH)
    GPIO.output(S3, GPIO.HIGH)
    g = GPIO.input(OUT)

    GPIO.output(S2, GPIO.LOW)
    GPIO.output(S3, GPIO.HIGH)
    b = GPIO.input(OUT)

    # RGB 값을 출력
    print("R: " + str(r) + " G: " + str(g) + " B: " + str(b))

# 색상 감지 함수 호출
while True:
    read_color()
728x90