You are on page 1of 11

POST-LAB ESPUA (4109) ASSIGNMENT

Experiment – 9
Aim of the Experiment:
Colour Recognition by using RGB LED and Colour Sensor with Arduino.
Objective:

1) Familiarization with RGB LED and controlling RGB LED with Arduino.
2) To make a colour mixing lamp using conditional statement
3) To feel the rainbow sensing colour with Arduino.
3.1 Calibrating the sensor
3.2 Read RGB values from the TCS230 colour sensor.
4) Colour Recognition by using RGB LED and Colour Sensor with Arduino.

Components/Equipment Required:

Sl No. Name of the Specification Quantity


Component/Equipement
1 Arduino UNO R3 16MHz 1
2 Arduino UNO cable USB Type A to B 1
3 Resistors (carbon type) ¼ watt (330Ω) 3
4 RGB LED Common Cathode, 1
3mm
5 Colour recognition sensor TCS 230/ TCS 3200 1
module
5 Breadboard 840 Tie points 1
6 Jumper Wire ----------------------- As per requirement

Circuit/Schematic Diagram:

Objective 1

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9

Code:
Objective 1
int redPin=11;
int greenPin=10;
int bluePin=9;
int redBrightness=255;
int greenBrightness=100;
int blueBrightness=50;

void setup() {
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}
void loop() {
analogWrite(redPin,redBrightness);
analogWrite(greenPin,greenBrightness);
analogWrite(bluePin,blueBrightness);

}
Objective 2
int redPin=11;
int greenPin=10;
int bluePin=9;

int brightness=255;
//int greenBrightness=0;
//int blueBrightness=0;
String colourChoice;

void setup() {
Serial.begin(9600);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(bluePin,OUTPUT);
}
void loop() {
Serial.println(" What colour do you want in LED ??? (red, green, blue) ");
while(Serial.available()==0)
{
}
colourChoice=Serial.readString();
if(colourChoice=="red")
{
analogWrite(redPin,brightness);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
}
if(colourChoice=="green")
{
analogWrite(redPin,0);
analogWrite(greenPin,brightness);
analogWrite(bluePin,0);
}
if(colourChoice=="blue")
{
analogWrite(redPin,0);

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
analogWrite(greenPin,0);
analogWrite(bluePin,brightness);
}
if(colourChoice!="red"&&colourChoice!="green"&&colourChoice!="blue")

{
Serial.println(" Please input a valid colour ");
}
}
Objectiv 3.1
// Define color sensor pins
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
// Variables for Color Pulse Width Measurements
int redPW = 0;
int greenPW = 0;
int bluePW = 0;
void setup() {
// Set S0 - S3 as outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Set Pulse Width scaling to 20%
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
// Set Sensor output as input
pinMode(sensorOut, INPUT);
// Setup Serial Monitor

Serial.begin(9600);
}
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);
// Print output to Serial Monitor
Serial.print("Red PW = ");
Serial.print(redPW);
Serial.print(" - Green PW = ");
Serial.print(greenPW);
Serial.print(" - Blue PW = ");
Serial.println(bluePW);
}

// Function to read Red Pulse Widths


int getRedPW() {
// Set sensor to read Red only
digitalWrite(S2,LOW);
EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)
Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
digitalWrite(S3,LOW);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;

}
// Function to read Green Pulse Widths
int getGreenPW() {
// Set sensor to read Green only
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
// Function to read Blue Pulse Widths
int getBluePW() {
// Set sensor to read Blue only
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
Objectiv 3.2
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Calibration Values
int redMin = 0; // Red minimum value
int redMax = 0; // Red maximum value
int greenMin = 0; // Green minimum value
int greenMax = 0; // Green maximum value
int blueMin = 0; // Blue minimum value
int blueMax = 0; // Blue maximum value
// 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(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)
Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
pinMode(S3, OUTPUT);
// Set Sensor output as input
pinMode(sensorOut, INPUT);
// Set Frequency scaling to 20%
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
// 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(S2,LOW);
digitalWrite(S3,LOW);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;

}
// Function to read Green Pulse Widths
int getGreenPW() {
// Set sensor to read Green only
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
}
// Function to read Blue Pulse Widths
int getBluePW() {
// Set sensor to read Blue only
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
Objective 4
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 6
#define redLED 7
#define greenLED 8
#define blueLED 9
//Output from the sensor:
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;
//Formatted color values:
int redColor = 0;
int greenColor = 0;
int blueColor = 0;
//Values used for calibration
int redMin;
int redMax;
int greenMin;
int greenMax;
int blueMin;
int blueMax;
int color = 0;
void setup() {
//Declarations:
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(13, OUTPUT);
pinMode(sensorOut, INPUT);
// Set frequency scaling to 20%:
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
Serial.begin(9600);//begin serial communication
calibrate();//calibrate sensor (look at serial monitor)
}
void loop() {
readColor();//read sensor
decideColor();//format color values
printColor();//print values
}
void decideColor() {//format color values
EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)
Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
//Limit possible values:
redColor = constrain(redColor, 0, 255);
greenColor = constrain(greenColor, 0, 255);
blueColor = constrain(blueColor, 0, 255);
//find brightest color:
int maxVal = max(redColor, blueColor);
maxVal = max(maxVal, greenColor);
//map new values
redColor = map(redColor, 0, maxVal, 0, 255);
greenColor = map(greenColor, 0, maxVal, 0, 255);
blueColor = map(blueColor, 0, maxVal, 0, 255);
redColor = constrain(redColor, 0, 255);
greenColor = constrain(greenColor, 0, 255);
blueColor = constrain(blueColor, 0, 255);
//light led
analogWrite(redLED, redColor);
analogWrite(greenLED, greenColor);
analogWrite(blueLED, blueColor);
//decide which color is present (you may need to change some values here):
if (redColor > 250 && greenColor > 250 && blueColor > 250) {
color = 1;//white
}
else if (redColor < 25 && greenColor < 25 && blueColor < 25) {
color = 2;//black
}
else if (redColor > 200 && greenColor > 200 && blueColor < 100) {
color = 4;//yellow
}
else if (redColor > 200 && greenColor > 25 /*&& blueColor < 100*/) {
color = 3;//orange
}
else if (redColor > 200 && greenColor < 100 && blueColor > 200) {
color = 5;//purple
}
else if (redColor > 250 && greenColor < 200 && blueColor < 200) {
color = 6;//red
}
else if (redColor < 200 && greenColor > 250 && blueColor < 200) {
color = 7;//green
}
else if (redColor < 200 /*&& greenColor < 200*/ && blueColor > 250) {
color = 8;//blue
}
else {
color = 0;//unknown
}
}

void calibrate() {
Serial.println("Calibrating...");
delay(3000);
Serial.println("White");//aim sensor at something white
//set calibration values:
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMin = pulseIn(sensorOut, LOW);
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
greenMin = pulseIn(sensorOut, LOW);
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMin = pulseIn(sensorOut, LOW);
delay(100);
Serial.println("next...");//aim sensor at something black
digitalWrite(13, LOW);
delay(2000);
Serial.println("Black");
//set calibration values:
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redMax = pulseIn(sensorOut, LOW);
delay(100);
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenMax = pulseIn(sensorOut, LOW);
delay(100);
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueMax = pulseIn(sensorOut, LOW);
delay(100);
Serial.println("Done calibrating.");
digitalWrite(13, LOW);
}
void printColor() {//print data
Serial.print("R = ");
Serial.print(redColor);
Serial.print(" G = ");
Serial.print(greenColor);
Serial.print(" B = ");
Serial.print(blueColor);
Serial.print(" Color: ");
switch (color) {
case 1: Serial.println("WHITE"); break;
case 2: Serial.println("BLACK"); break;
case 3: Serial.println("ORANGE"); break;
case 4: Serial.println("YELLOW"); break;
case 5: Serial.println("PURPLE"); break;
case 6: Serial.println("RED"); break;
case 7: Serial.println("GREEN"); break;
case 8: Serial.println("BLUE"); break;
default: Serial.println("unknown"); break;
}
}
void readColor() {//get data from sensor
//red:
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
redFrequency = pulseIn(sensorOut, LOW);
redColor = map(redFrequency, redMin, redMax, 255, 0);
delay(100);
//green:
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
greenFrequency = pulseIn(sensorOut, LOW);
greenColor = map(greenFrequency, greenMin, greenMax, 255, 0);
delay(100);

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9
//blue:
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
blueFrequency = pulseIn(sensorOut, LOW);
blueColor = map(blueFrequency, blueMin, blueMax, 255, 0);
delay(100);
}
Observation:
Objective 1

Objective 2

Objective 3

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9

Objective 4

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.
POST-LAB ESPUA (4109) ASSIGNMENT
Experiment – 9

EMBEDDED SYSTEM PROJECT USING ARDUINO (EET 4109)


Colour Recognition by using RGB LED and Colour Sensor with Arduino.

You might also like