SUMMER STEM-HEALTH CAMP
  • Home
  • 2025 RCC Camp
    • Day 1: Basic Coding >
      • Button Control
      • Multicolor LED >
        • Fading multicolor led
      • Melody (sound)
      • Day 1 Supplement: Measure Temperature
    • Day 2: Servos and Potentiometers >
      • DC Motor
      • Ultrasound
      • Potentiometer and Motors
      • 2 servo control >
        • Analog Stick Control
      • PIR Motion Sensor
      • IR Light Proximity Sensor
    • Day 3: Measuring Pulse >
      • How the body absorbs light
      • OLED Screen Basics >
        • Screen Pictures
        • Scrolling Screen Graph
        • BPM on OLED
      • IR Temp Sensor >
        • IR Temp Sensor And Screen
    • Day 4 Making an ECG/EKG! >
      • ECG/EKG health lesson
      • Light Color Module
    • Day 5: Finishing up/Show >
      • CO2 Sensor
  • The Teachers
  • Our Partners
  • Archive
    • Home (2024) >
      • 2024 Health-STEM Coding Camp
      • 2024 Coding/Robotics Camp >
        • Day 1: Basic Coding >
          • Saving your codes
          • Engineering design
        • Day 2 Servos
    • Pictures from prior years
    • 2023 3D Design Camp >
      • Day 1: 2D design
      • Day 2: 3D design basics
      • Day 3: Constraints
    • 2023 Health-STEM Coding Camp >
      • PreAcademy prep
      • Day 1: Basics and Lights >
        • Multicolor LED
        • Button Control
      • Day 2: Ultrasound >
        • Ultrasound Health Lesson
        • Supplement Melody
        • Supplement: Servo >
          • Potentiometer and Motors
          • 2 servo control
      • Day 3: Measuring Pulse >
        • How the body absorbs light
        • OLED Screen Basics >
          • Scrolling Screen Graph
      • Day 4 Making an ECG/EKG! >
        • ECG Health Lesson
        • Pictures to OLED
        • BPM on OLED
    • 2021 STEM Camp (HS) >
      • The Teachers (2021)
      • PreAcademy prep
      • Day 1: Basics, Lights, and Temperature Sensor >
        • Day 1 Supplement: Measure Temperature
        • Day 1 Supplement: IR Temp Sensor
        • Day 1 Supplement: Identifying Resistors
        • Supplement: Controlling A Servo
      • Day 2: Measuring Pulse >
        • IR Light Proximity Sensor
        • PIR Motion Sensor
      • Day 3 Supplement: OLED Screen Basics >
        • Display Screen Temperature
        • Scrolling Screen Graph
      • Day 3: Measuring Pulse
      • Day 4: Finishing up/Show
    • 2021 STEM Camp (MS) >
      • PreAcademy prep
      • Day 1: Basics, Lights, and Temperature Sensor
    • 2020 STEM Camp
    • 2018 Lessons
    • 2017 Camp
  • Contact Us!

Color Sensor Module

Supplies Needed for this activity

Arduino, wires, and breadboard
Picture
RGB color sensor
Picture
Multicolor LED and resistors.
Picture

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? 
Picture
Picture
Most people have one type of eye cone cell most sensitive in red light, one in green light, and one in red light. Hence, we say RGB are the primary colors for computer screens. Here is a graph of which wavelengths of light a typical person's cone cells are sensitive to. 
Picture

The RGB Sensor

The RGB Arduino Sensor works in a similar way, but it uses photosensors and filters to make a signal when the right type of light is going into the sensor. 
Picture
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. 

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 

Picture
Picture

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. 
Go to "Tools" and then "Manage Libraries"
Picture
Search for Adafruit TCS34725
Picture

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. 

Picture

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);
}

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/

Proudly powered by Weebly
  • Home
  • 2025 RCC Camp
    • Day 1: Basic Coding >
      • Button Control
      • Multicolor LED >
        • Fading multicolor led
      • Melody (sound)
      • Day 1 Supplement: Measure Temperature
    • Day 2: Servos and Potentiometers >
      • DC Motor
      • Ultrasound
      • Potentiometer and Motors
      • 2 servo control >
        • Analog Stick Control
      • PIR Motion Sensor
      • IR Light Proximity Sensor
    • Day 3: Measuring Pulse >
      • How the body absorbs light
      • OLED Screen Basics >
        • Screen Pictures
        • Scrolling Screen Graph
        • BPM on OLED
      • IR Temp Sensor >
        • IR Temp Sensor And Screen
    • Day 4 Making an ECG/EKG! >
      • ECG/EKG health lesson
      • Light Color Module
    • Day 5: Finishing up/Show >
      • CO2 Sensor
  • The Teachers
  • Our Partners
  • Archive
    • Home (2024) >
      • 2024 Health-STEM Coding Camp
      • 2024 Coding/Robotics Camp >
        • Day 1: Basic Coding >
          • Saving your codes
          • Engineering design
        • Day 2 Servos
    • Pictures from prior years
    • 2023 3D Design Camp >
      • Day 1: 2D design
      • Day 2: 3D design basics
      • Day 3: Constraints
    • 2023 Health-STEM Coding Camp >
      • PreAcademy prep
      • Day 1: Basics and Lights >
        • Multicolor LED
        • Button Control
      • Day 2: Ultrasound >
        • Ultrasound Health Lesson
        • Supplement Melody
        • Supplement: Servo >
          • Potentiometer and Motors
          • 2 servo control
      • Day 3: Measuring Pulse >
        • How the body absorbs light
        • OLED Screen Basics >
          • Scrolling Screen Graph
      • Day 4 Making an ECG/EKG! >
        • ECG Health Lesson
        • Pictures to OLED
        • BPM on OLED
    • 2021 STEM Camp (HS) >
      • The Teachers (2021)
      • PreAcademy prep
      • Day 1: Basics, Lights, and Temperature Sensor >
        • Day 1 Supplement: Measure Temperature
        • Day 1 Supplement: IR Temp Sensor
        • Day 1 Supplement: Identifying Resistors
        • Supplement: Controlling A Servo
      • Day 2: Measuring Pulse >
        • IR Light Proximity Sensor
        • PIR Motion Sensor
      • Day 3 Supplement: OLED Screen Basics >
        • Display Screen Temperature
        • Scrolling Screen Graph
      • Day 3: Measuring Pulse
      • Day 4: Finishing up/Show
    • 2021 STEM Camp (MS) >
      • PreAcademy prep
      • Day 1: Basics, Lights, and Temperature Sensor
    • 2020 STEM Camp
    • 2018 Lessons
    • 2017 Camp
  • Contact Us!