Professional Documents
Culture Documents
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:
Circuit/Schematic Diagram:
Objective 1
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);
{
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 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();
}
// 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;
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);
Objective 2
Objective 3
Objective 4