'TCS230'에 해당되는 글 1건

  1. 2018.04.03 Hardware | TCS230/TCS3200 color sensor 를 사용해 보자

Hardware | TCS230/TCS3200 color sensor 를 사용해 보자

|

1. Color Sensor


색을 인식하는 센서가 있다고 해서 주문해 봤습니다.


* Color sensor TCS230 TCS3200 Color Recognition Sensor Detector Module DC 3-5V Input

https://ko.aliexpress.com/item/Free-Shipping-Color-sensor-TCS230-TCS3200-Color-Recognition-Sensor-Detector-Module-DC-3-5V-Input/32630370831.html





2. 도착


한 2주 걸렸습니다.

잘 쌓여서 왔습니다.



뒷면입니다.



저 가운데 있는 센서가 컬러 센서 인듯 하군요.



디지털 현미경으로 한번 확인해 봤습니다.



RGB 소자가 격자로 자리잡고 있네요.

소자 연결 부분은 금선으로 연결되어 있는 것이 보입니다.


센서의 스팩은 다음과 같습니다.


* TCS230

tcs230-e33.pdf





3. Pinout


Arduino 와의 pin 연결은 다음과 같습니다.

   TCS230   | Arduino Nano
---------------------------
     GND    |     GND
      OE    |      -
      S1    |     D4
      S0    |     D3
            |
      S3    |     D6
      S2    |     D5
     OUT    |     D2
     VCC    |     5V
---------------------------

Layout 은 다음과 같습니다.






4. Sketch


* Arduino Color Sensing Tutorial – TCS230 TCS3200 Color Sensor

https://howtomechatronics.com/tutorials/arduino/arduino-color-sensing-tutorial-tcs230-tcs3200-color-sensor/


아래는 color 센서에서 가장 기초가 되는 sketch 입니다.

소스를 보면 S2 / S3 를 high / low 를 가지고 RGB 를 구분하여 입력을 받는 구조 입니다.




/*     Arduino Color Sensing Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  
 */
 
#define S0 3
#define S1 4
#define S2 5
#define S3 6
#define sensorOut 2
int frequency = 0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  
  // Setting frequency-scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  Serial.begin(9600);
}

void loop() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(100);

  // Setting Green filtered photodiodes to be read
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.print("  ");
  delay(100);

  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  // Reading the output frequency
  frequency = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency);//printing RED color frequency
  Serial.println("  ");
  delay(100);
}


위의 소스는 센서의 주파수 특성을 그대로 표현해 줄 것입니다.






5. 결과


아래 무지개 색을 빨강부터 차례대로 비추어본 결과 입니다.



값이 주르륵 변하는게 보이네요.



값의 변화를 그래프로 표시해보니 다음과 같습니다.

빨간색일 경우는 빨간색이 가장 높고, 파란색일 경우는 파란색 파장이 가장 높습니다.



위의 결과는 단순히 frequency - 주파수의 값을 나타내므로, 0~255 까지를 보여주는 color decimal value 는 아닙니다.




6. color code 형식으로 표시


color code 방식으로 표시를 하기 위해선, 아래 사이트에서 소개된 TCS3200 sample 을 사용하면 됩니다.


* ARDUINO COLOR SENSOR / COLOR RECOGNITION SENSOR TCS230

http://www.instructables.com/id/Arduino-COLOR-Sensor-Color-Recognition-Sensor-TCS2/


* Sample library

TCS3200.zip


Library 에 중요한 코드가 들어가 있어서 sample sketch 는 너무 간단합니다.

심심한 부분도 있고 해서 color OLED 를 함께 사용한 code 를 짜 봅니다.


목표는 color OLED 에 현재 인식되는 color 도 표시하고 RGP decimal value 도 표시해 주는 것입니다.


#include "TCS3200.h"
uint8_t RGBvalue[3];
TCS3200 colSens;

// for color OLED
#define sclk 13
#define mosi 11
#define cs   10
#define rst  9
#define dc   8

#include "Adafruit_GFX.h"
#include "Adafruit_SSD1331.h"
#include "SPI.h"

// Option 1: use any pins but a little slower
Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst); 

// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0 
#define WHITE           0xFFFF

void setup() {
	Serial.begin(115200);
	Serial.println("BEGIN");
	colSens.begin();
	
	display.begin();
	display.fillScreen(BLACK);
}

void loop() {
	colSens.loop();
	colSens.getRGB (RGBvalue);
	//colSens.getRGBtoMaxCorrection (RGBvalue);
	
	// clear previous text
	display.fillRect(2, 2*(display.height())/12, (2.5*(display.width()))/6-1, 9*(display.height())/12, BLACK);
	
	
	display.setCursor(2, 2*(display.height())/12);
	display.setTextColor(RED);
	display.print("R "); //printing name
	display.print(RGBvalue[0]);
	
	display.setCursor(2, 5.5*(display.height())/12);
	display.setTextColor(GREEN);
	display.print("G "); //printing name
	display.print(RGBvalue[1]); //printing RED color frequency
	
	display.setCursor(2, 9*(display.height())/12);
	display.setTextColor(BLUE);
	display.print("B "); //printing name
	display.print(RGBvalue[2]); //printing RED color frequency
	
	// check what color is sensing
	if (RGBvalue[0] == 255 && (70 < RGBvalue[1] && RGBvalue[1] < 80) && (80 < RGBvalue[2] && RGBvalue[2] < 90))
		display.fillRect(2.5*(display.width())/6, (display.height())/12, 5*(display.width())/6, 11*(display.height())/12, RED);
	else if (RGBvalue[0] == 255 && RGBvalue[1] == 255 && (60 < RGBvalue[2] && RGBvalue[2] < 70))
		display.fillRect(2.5*(display.width())/6, (display.height())/12, 5*(display.width())/6, 11*(display.height())/12, YELLOW);
	else if ((110 < RGBvalue[0] && RGBvalue[0] < 120) && (160 < RGBvalue[1] && RGBvalue[1] < 170) && (100 < RGBvalue[2] && RGBvalue[2] < 110))
		display.fillRect(2.5*(display.width())/6, (display.height())/12, 5*(display.width())/6, 11*(display.height())/12, GREEN);
	else if ((70 < RGBvalue[0] && RGBvalue[0] < 80) && (98 < RGBvalue[1] && RGBvalue[1] < 108) && (200 < RGBvalue[2] && RGBvalue[2] < 210))
		display.fillRect(2.5*(display.width())/6, (display.height())/12, 5*(display.width())/6, 11*(display.height())/12, BLUE);
}


if 문이을 간결하게 하거나 변수화 시킬 수 있는 부분은 많으나, 그냥 발로 짰습니다.

또한, text update 를 하는 방법을 몰라 그냥 fillRect() 함수로 문대 버리다 보니, 인터럽트가 걸려서 색 인식 update 가 늦습니다.


판별 루틴에서 프린트된 빨간색과 센서가 인식되는 값이 달라서, 센서 인식 값 기준으로 +5/-5 로 값의 범위를 맞췄습니다.

그냥 보여주기식 꼼수...



이하 동영상 입니다.



동영상에서는 노란색 인식이 똥망이네요.





FIN


색을 인식하는 모듈을 어디에 쓸꺼냐고 물어보면... 딱히 적절한 대답이 생각나지 않습니다 만...

언젠가는 한번 있을지도... (아니면 없을지도...)

And
prev | 1 | next