Sparkfun RGB LED and RGB Light Sensor
I can't quite figure this one out: it's VERY delicate and I can't get consistant color results with the built-on LED on or off. I know if it gets a UV light, it shows a lot of purple and if it gets hit with a red laser it maxes out the red LED. Anyhow... Video and code!
/*Expanded to use the Sparkfun RGB LED on pins 3, 5, and 6, sendingPWM signals as 1/10 of the result. YouTube video of this in action atUse of the RGB LED changes added by thoughtfix 2/2012Inherited OSHW license.Original comments below.*//*An Arduino code example for interfacing with theHDJD-S822-QR999 Color Sensor. Put an object in front of thesensor and look at the serial monitor to see the values the sensoris reading. Scaling factors and gains may have to be adjustedfor your application.by: Jordan McConnellSparkFun Electronicscreated on: 1/24/12license: OSHW 1.0, http://freedomdefined.org/OSHWConnect the gain pins of the sensor to digital pins 7 - 12 (or ground).Connect the led pin to digital 13.Connect Vr to analog 0, Vg to analog 1, and Vb to analog 2.*/// Define pinsconst int ledpin = 13;const int GSR1 = 12;const int GSR0 = 11;const int GSG1 = 10;const int GSG0 = 9;const int GSB1 = 8;const int GSB0 = 7;const int redLed = 3;const int greenLed = 5;const int blueLed = 6;int redpin = A0;int greenpin = A1;int bluepin = A2;// Sensor read valuesint red = 0;int green = 0;int blue = 0;int redBright = 0;int greenBright = 0;int blueBright = 0;void setup(){Serial.begin(9600);pinMode(ledpin, OUTPUT);pinMode(GSR1, OUTPUT);pinMode(GSR0, OUTPUT);pinMode(GSG1, OUTPUT);pinMode(GSG0, OUTPUT);pinMode(GSB1, OUTPUT);pinMode(GSB0, OUTPUT);pinMode(redLed, OUTPUT);pinMode(greenLed, OUTPUT);pinMode(blueLed, OUTPUT);// Turn on the LEDdigitalWrite(ledpin, LOW);// Set the gain of each sensordigitalWrite(GSR1, LOW);digitalWrite(GSR0, LOW);digitalWrite(GSG1, LOW);digitalWrite(GSG0, LOW);digitalWrite(GSB1, LOW);digitalWrite(GSB0, LOW);}void loop(){// Read sensors// On page 7 of the datasheet, there is a graph of the// spectral responsivity of the chip. Scaling factors were// selected based on this graph so that the gain of each// color is closer to being equalred = analogRead(redpin) * 10;green = analogRead(greenpin) * 14;blue = analogRead(bluepin) * 17;redBright = red/10;greenBright = green/10;blueBright = blue/10;// Print values to the serial monitorSerial.print("Red: ");Serial.print(red, DEC);analogWrite(redLed, redBright);Serial.print("\t\tGreen: ");Serial.print(green, DEC);analogWrite(greenLed, greenBright);Serial.print("\tBlue: ");Serial.println(blue, DEC);analogWrite(blueLed, blueBright);delay(20);}/*Expanded to use the Sparkfun RGB LED on pins 3, 5, and 6, sendingPWM signals as 1/10 of the result. YouTube video of this in action at
Use of the RGB LED changes added by thoughtfix 2/2012Inherited OSHW license.Original comments below.*/
/*An Arduino code example for interfacing with the HDJD-S822-QR999 Color Sensor. Put an object in front of thesensor and look at the serial monitor to see the values the sensoris reading. Scaling factors and gains may have to be adjustedfor your application.
by: Jordan McConnell SparkFun Electronics created on: 1/24/12 license: OSHW 1.0, http://freedomdefined.org/OSHW Connect the gain pins of the sensor to digital pins 7 - 12 (or ground).Connect the led pin to digital 13.Connect Vr to analog 0, Vg to analog 1, and Vb to analog 2.*/
// Define pinsconst int ledpin = 13;const int GSR1 = 12;const int GSR0 = 11;const int GSG1 = 10;const int GSG0 = 9;const int GSB1 = 8;const int GSB0 = 7;const int redLed = 3;const int greenLed = 5;const int blueLed = 6;
int redpin = A0;int greenpin = A1;int bluepin = A2;
// Sensor read valuesint red = 0;int green = 0;int blue = 0;int redBright = 0;int greenBright = 0;int blueBright = 0;
void setup() { Serial.begin(9600);
pinMode(ledpin, OUTPUT); pinMode(GSR1, OUTPUT); pinMode(GSR0, OUTPUT); pinMode(GSG1, OUTPUT); pinMode(GSG0, OUTPUT); pinMode(GSB1, OUTPUT); pinMode(GSB0, OUTPUT); pinMode(redLed, OUTPUT); pinMode(greenLed, OUTPUT); pinMode(blueLed, OUTPUT);
// Turn on the LED digitalWrite(ledpin, LOW); // Set the gain of each sensor digitalWrite(GSR1, LOW); digitalWrite(GSR0, LOW); digitalWrite(GSG1, LOW); digitalWrite(GSG0, LOW); digitalWrite(GSB1, LOW); digitalWrite(GSB0, LOW);}
void loop() { // Read sensors // On page 7 of the datasheet, there is a graph of the // spectral responsivity of the chip. Scaling factors were // selected based on this graph so that the gain of each // color is closer to being equal red = analogRead(redpin) * 10; green = analogRead(greenpin) * 14; blue = analogRead(bluepin) * 17; redBright = red/10; greenBright = green/10; blueBright = blue/10;
// Print values to the serial monitor Serial.print("Red: "); Serial.print(red, DEC); analogWrite(redLed, redBright); Serial.print("\t\tGreen: "); Serial.print(green, DEC); analogWrite(greenLed, greenBright); Serial.print("\tBlue: "); Serial.println(blue, DEC); analogWrite(blueLed, blueBright); delay(20);}
LEDs,
arduino,
sensors,
source code 
