Introduction
A color sensor module makes it easy to detect colors in your project application, that is, red, green and blue components. It has 4 white light LEDs to illuminate the surface of interest.
Module
This module is based on TCS3200 programmable color light-to-frequency converters that combine configurable silicon photodiodes and a current-to-frequency converter on a single monolithic CMOS integrated circuit. The output is a square wave (50%duty cycle) with frequency directly proportional to light intensity (irradiance).
TCS3200 Pinout
Configuration
Part List
We’ll learn how to connect the color sensor to Arduino Uno. Part list needed;
– Color sensor module, TCS3200
– Arduino Uno
– Mini breadboard
– Pack of jumper wires
Wiring
we are going to wire the color sensor to Arduino Uno using the following pin assignments;
Sensor Pin | Arduino Pin |
---|---|
S0 | 3 |
S1 | 4 |
S2 | 5 |
S3 | 6 |
OE | Not Connected |
OUT | 2 |
GND | GND |
VCC | 5V |
Breadboard Connections
Code
Arduino Code
Arduino code for the tutorial
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* * Connections * ------------------- * Sensor | Arduino * S0 | 3 * S1 | 4 * S2 | 5 * S3 | 6 * OUT | 2 * VCC | 5V * GND | GND * ------------------- * */ //macro definition pins #define S0 3 #define S1 4 #define S2 5 #define S3 6 #define output 2 //variables uint8_t flag = 0; volatile uint8_t f_counter = 0; uint8_t frequency_R = 0, frequency_G = 0, frequency_B = 0; //setup void setup() { Serial.begin(115200); pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); } //loop void loop() { TCS3200_init(); while (1); } //initialize void TCS3200_init() { flag = 0; digitalWrite(S1, HIGH); |
How The Code Works
In the macro definitions we assign arduino pins connected to the sensor
1 2 3 4 5 6 |
//macro definition pins #define S0 3 #define S1 4 #define S2 5 #define S3 6 #define output 2 |
In the setup functions we set the pins, S0, S1, S2, S3 and S6 as OUTPUTS also initializing serial port with a 115200 baudrate
1 2 3 4 5 |
Serial.begin(115200); pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); |
To count the frequency from the sensor, we attach the pin connected to sensor output, Arduino pin 2, to pin change interrupt using ‘attachInterrupt’ function
1 |
attachInterrupt(digitalPinToInterrupt(output), ISR_INTO, CHANGE); |
for the readings of Red, Blue and Green components, we use the configuration settings by either setting the S0, S1, S2, S3 HIGH or LOW using ‘digitalWrite’ function. e.g. To read Red color value you use;
1 2 |
digitalWrite(S2, LOW); digitalWrite(S3, LOW); |
And finally we use Timer 2 set at 10 milliseconds overflow interrupt to get and send readings through serial
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//interrupt, 10ms ISR(TIMER2_OVF_vect) { TCNT2 = 100; flag++; //get red component value if (flag == 1) { frequency_R = f_counter; Serial.print("red="); Serial.println(frequency_R, DEC); digitalWrite(S2, HIGH); digitalWrite(S3, HIGH); } ....... |
Running Code
click ‘Run on Arduino’ to upload code to the Arduino board
Serial Monitor
To view the RGB values use the serial monitor below
Feedback is closed.