Color Sensor Module
Supplies Needed for this activity
What is RGB?
People's eyes have two main types of cells, rods and cones. Most people have 3 different types of cone cells. Below is an electron scanning microscope with "false color" differentiating the rod and cone cells. Can you tell which is which? Can you tell why their names are rods and cones?
The RGB Sensor
An example application of this sensor is a fruit sorting machine. Unripened fruit is a different color.
You can use this sensor's color inputs to trigger a motor to kick unripened fruit into another conveyor.
You can use this sensor's color inputs to trigger a motor to kick unripened fruit into another conveyor.
Wiring the Sensor
You will need to connect the sensor in the following way
1) SDA to an analogue pin (this picture shows A4)
2) The SCL to an analogue pin (this picture shows A5)
3) The Ground (GND) pin to a board's GND
4) The VDD or VIN pin to the board's 5V
1) SDA to an analogue pin (this picture shows A4)
2) The SCL to an analogue pin (this picture shows A5)
3) The Ground (GND) pin to a board's GND
4) The VDD or VIN pin to the board's 5V
Add the Library
You will need to add a library so that code you make can use it to reference what you want done. Here are those steps.
Starting code
Here is some sample code you can try that just outputs to the serial monitor values the red, green, and blue sensors are reading for what is in front of it. Try it out, run it, and see if it works after you wire it up.
Try putting different color things in front and see if values change in a way that makes sense. |
Multicolor LED responds to RGB sensor.
Below is code for getting a multicolor led to work in response to colors.
include <Wire.h>
#include "Adafruit_TCS34725.h"
#define redpin 3 // RGB LED pins
#define greenpin 5
#define bluepin 6
#define commonAnode false // Set to false if using a common cathode LED
byte gammatable[256]; // Gamma correction table
Adafruit_TCS34725 tcs = Adafruit_TCS34725( // Initialize the sensor with reasonable integration time and gain
TCS34725_INTEGRATIONTIME_50MS,
TCS34725_GAIN_4X
);
void setup() {
Serial.begin(9600);
if (!tcs.begin()) {
Serial.println("No sensor found ... check connections");
while (1); // halt!
}
pinMode(redpin, OUTPUT); // Set LED pins as outputs
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
for (int i = 0; i < 256; i++) { // Create gamma correction table
float x = i / 255.0;
x = pow(x, 2.5);
x *= 255.0;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
}
}
void loop() {
unsigned int red, green, blue; //Read RGB values
tcs.setInterrupt(false); // Enable sensor LED for accurate reading
delay(60); // Wait for integration time (50ms)
tcs.getRGB(&red, &green, &blue);
tcs.setInterrupt(true); // Turn off sensor LED
float maxVal = max(red, max(green, blue)); // Normalize RGB so max value is 255
if (maxVal == 0) maxVal = 1; // avoid divide-by-zero
int r = (red / maxVal) * 255;
int g = (green / maxVal) * 255;
int b = (blue / maxVal) * 255;
// Constrain values to 0-255
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
Serial.print("Sensor RGB: "); // Debug output
Serial.print((int)red); Serial.print(", ");
Serial.print((int)green); Serial.print(", ");
Serial.print((int)blue); Serial.print(" -> ");
Serial.print("Scaled RGB: ");
Serial.print(r); Serial.print(", ");
Serial.print(g); Serial.print(", ");
Serial.println(b);
analogWrite(redpin, gammatable[r]); // Output to RGB LED using gamma-corrected values
analogWrite(greenpin, gammatable[g]);
analogWrite(bluepin, gammatable[b]);
delay(1000);
}
#include "Adafruit_TCS34725.h"
#define redpin 3 // RGB LED pins
#define greenpin 5
#define bluepin 6
#define commonAnode false // Set to false if using a common cathode LED
byte gammatable[256]; // Gamma correction table
Adafruit_TCS34725 tcs = Adafruit_TCS34725( // Initialize the sensor with reasonable integration time and gain
TCS34725_INTEGRATIONTIME_50MS,
TCS34725_GAIN_4X
);
void setup() {
Serial.begin(9600);
if (!tcs.begin()) {
Serial.println("No sensor found ... check connections");
while (1); // halt!
}
pinMode(redpin, OUTPUT); // Set LED pins as outputs
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
for (int i = 0; i < 256; i++) { // Create gamma correction table
float x = i / 255.0;
x = pow(x, 2.5);
x *= 255.0;
if (commonAnode) {
gammatable[i] = 255 - x;
} else {
gammatable[i] = x;
}
}
}
void loop() {
unsigned int red, green, blue; //Read RGB values
tcs.setInterrupt(false); // Enable sensor LED for accurate reading
delay(60); // Wait for integration time (50ms)
tcs.getRGB(&red, &green, &blue);
tcs.setInterrupt(true); // Turn off sensor LED
float maxVal = max(red, max(green, blue)); // Normalize RGB so max value is 255
if (maxVal == 0) maxVal = 1; // avoid divide-by-zero
int r = (red / maxVal) * 255;
int g = (green / maxVal) * 255;
int b = (blue / maxVal) * 255;
// Constrain values to 0-255
r = constrain(r, 0, 255);
g = constrain(g, 0, 255);
b = constrain(b, 0, 255);
Serial.print("Sensor RGB: "); // Debug output
Serial.print((int)red); Serial.print(", ");
Serial.print((int)green); Serial.print(", ");
Serial.print((int)blue); Serial.print(" -> ");
Serial.print("Scaled RGB: ");
Serial.print(r); Serial.print(", ");
Serial.print(g); Serial.print(", ");
Serial.println(b);
analogWrite(redpin, gammatable[r]); // Output to RGB LED using gamma-corrected values
analogWrite(greenpin, gammatable[g]);
analogWrite(bluepin, gammatable[b]);
delay(1000);
}
Extension Activities
You can do a lot of things as extensions to this. You can have a multicolor LED try and match the colors in front of the sensor. You can have a motor or buzzer turn on if one of the colors is above a certain value. And a whole bunch else!
Here is a website that has some more things you can do with it. You've already done the first half of it :)
https://www.makerguides.com/tcs34725-rgb-color-sensor-with-arduino/
Here is a website that has some more things you can do with it. You've already done the first half of it :)
https://www.makerguides.com/tcs34725-rgb-color-sensor-with-arduino/