Sketches: TCS230 Colour Sensor Module

HCMODU0069_800_600
Colour Sensing Module

 

Items you will need:

Arduino development board such as this one

Male to female dupont cables

Colour sensor module

What does it do?

This sketch will read the output of the sensor in the four different colour modes (red, green, blue, and clear) and output the readings as a value between 0 and 255 to the serial port. This example does not attempt to adjust or calibrate the raw  values read from the sensor.

Pinout:

GND……….0V
OE………..Output enable (low = enable)
S0 & S1….Output frequency scaling selection inputs
S2 & S3….Colour Selection inputs
OUT………Output frequency.
VCC………3V ~ 5.5V supply voltage

Code:

/* FILE:    Colour_Sensing_Module_Example
   DATE:    28/08/14
   VERSION: 0.1
   AUTHOR:  Andrew Davies

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute any part of 
this code. This software may not be used for the purpose of selling products
that directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/

enum Colours {RED,GREEN,BLUE,CLEAR};

/* Define the pin number used to select the colour to be measured */
#define S2_PIN 2
#define S3_PIN 3

/* Define the pin used to read the output of the sensor */
#define OUT_PIN 4


void setup()
{
  Serial.begin(9600);
  pinMode(S2_PIN, OUTPUT);
  pinMode(S3_PIN, OUTPUT);
  pinMode(OUT_PIN, INPUT);
}

/* Main program */
void loop()
{
  /* Read each colour value and output to the serial port */
  Serial.print(ReadColour(RED));
  Serial.print(" : "); 
  Serial.print(ReadColour(GREEN));
  Serial.print(" : "); 
  Serial.print(ReadColour(BLUE));
  Serial.print(" : "); 
  Serial.println(ReadColour(CLEAR));
}

/* Reads a colour and returns the a value betwenn 0 and 255 */
byte ReadColour(byte Colour)
{  
  switch(Colour)
  {
    case RED:
      digitalWrite(S2_PIN, LOW);
      digitalWrite(S3_PIN, LOW);
      break;
    
    case GREEN:
      digitalWrite(S2_PIN, HIGH);
      digitalWrite(S3_PIN, HIGH);
      break;    
      
    case BLUE:
      digitalWrite(S2_PIN, LOW);
      digitalWrite(S3_PIN, HIGH);
      break;
      
    case CLEAR:
      digitalWrite(S2_PIN, HIGH);
      digitalWrite(S3_PIN, LOW);
      break; 
  }
    
  return map(pulseIn(OUT_PIN, HIGH), 30, 2500, 255, 0);
}

Add a Comment

Your email address will not be published.