0% found this document useful (0 votes)
15 views

RGBsensor

This document describes wiring an RGB LED and photodiode to an Arduino board to sense color. It connects the RGB LED cathodes to digital pins and takes readings from the photodiode on an analog pin while pulsing each LED color to determine the highest value.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

RGBsensor

This document describes wiring an RGB LED and photodiode to an Arduino board to sense color. It connects the RGB LED cathodes to digital pins and takes readings from the photodiode on an analog pin while pulsing each LED color to determine the highest value.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

Arduino RGB color sensor.


Wire a common anode RGB LED so that the anode connects to +5V, the red cathode
connects to digital pin 9,
the blue cathode connects to digital pin 10, and the green cathode connects to
digital pin 11. Use a 1 KO resister between
all of the cathode connections, one for each cathode. Wire a photodiode so that
the collector connects to +5V, and the emitter
connects to analog pin 2 and a 5 KO resistor which connects to ground. Wire a b
utton so that one side of the button connects to
+5V, and the other side connects to digital pin 7 and a 1 KO resistor which con
nects to ground. Place the photodiode and the RGB led
so that they are both facing the object that you are sensing the color of.
*/
int r = 9;
int b = 10;
int g = 11;
int in = A2;
int button = 7;
int rval;
int gval;
int bval;
void setup()
{
pinMode(r, OUTPUT);
pinMode(b, OUTPUT);
pinMode(g, OUTPUT);
pinMode(in, INPUT);
pinMode(button, INPUT);
digitalWrite(r, HIGH);
digitalWrite(g, HIGH);
digitalWrite(b, HIGH);
}
void loop()
{
while(digitalRead(button) == LOW);
rval = 0;
bval = 0;
gval = 0;
for(int var; var < 10; var++)
{
digitalWrite(r, LOW);
delay(5);
rval = rval + analogRead(in);
delay(5);
digitalWrite(r, HIGH);
delay(5);
digitalWrite(g, LOW);
delay(5);
gval = gval + analogRead(in);
delay(5);
digitalWrite(g, HIGH);
delay(5);
digitalWrite(b, LOW);
delay(5);
bval = bval + analogRead(in);

delay(5);
digitalWrite(b, HIGH);
}
if(rval > gval && rval > bval)
{
for(int val = 0; val < 10; val++)
{
Serial.println("Red");
digitalWrite(r, LOW);
delay(100);
digitalWrite(r, HIGH);
delay(100);
}
}
if(gval > rval && gval > bval)
{
for(int val = 0; val < 10; val++)
{
Serial.println("Green");
digitalWrite(g, LOW);
delay(100);
digitalWrite(g, HIGH);
delay(100);
}
}
if(bval > rval && bval > gval)
{
Serial.println("Blue");
for(int val = 0; val < 10; val++)
{
digitalWrite(b, LOW);
delay(100);
digitalWrite(b, HIGH);
delay(100);
}
}
}

You might also like