Stufin
Home Quick Cart Profile

Colour Sensor

Buy Now

Colour Range

Detects colours in the visible spectrum (400-700nm)

Accuracy

5% of measured value

Sensitivity

Adjustable from 1-100%

Digital Output

I2C or UART

Power Supply

3.3V or 5V

Current Consumption

<10mA

Operating Temperature

-20C to 70C

Dimensions

15mm x 10mm x 5mm

Interfaces and Connectivity

The Colour Sensor provides a digital output that can be easily interfaced with microcontrollers and other devices using standard communication protocols such as I2C or UART.

Applications

  • Robotics: Colour sensors can be used in robotics to enable colour-based object recognition and sorting.
  • Smart Home: Colour sensors can be used in smart home devices to detect the colour of objects and adjust lighting and temperature accordingly.
  • Quality Control: Colour sensors can be used in quality control applications to detect defects and irregularities in production lines.
  • Automation: Colour sensors can be used in automation applications to detect and track objects based on their colour.

Conclusion

The Colour Sensor is a versatile and compact IoT component that provides accurate colour measurement and detection capabilities. Its compact design, low power consumption, and high accuracy make it an ideal component for a wide range of applications, from robotics and automation to smart home devices and quality control.

Pin Configuration

  • Colour Sensor Documentation
  • Pinout Explanation
  • The Colour Sensor module typically consists of a compact package with 6-8 pins, depending on the specific model. Here's a breakdown of each pin and its function:
  • Pin 1: VCC
  • Description: Power supply pin
  • Function: Provides voltage to the sensor module
  • Recommended Voltage: 3.3V or 5V (dependent on the specific module)
  • Connection: Connect to a power source (e.g., an Arduino board or a breadboard power rail)
  • Pin 2: GND
  • Description: Ground pin
  • Function: Provides a ground reference point for the sensor module
  • Connection: Connect to a ground pin on the microcontroller or a breadboard ground rail
  • Pin 3: SCL (Serial Clock)
  • Description: Serial clock pin for I2C communication
  • Function: Used for clocking data transmission between the sensor and the microcontroller
  • Connection: Connect to the SCL pin on the microcontroller (e.g., Arduino)
  • Pin 4: SDA (Serial Data)
  • Description: Serial data pin for I2C communication
  • Function: Used for transmitting data between the sensor and the microcontroller
  • Connection: Connect to the SDA pin on the microcontroller (e.g., Arduino)
  • Pin 5: OUT (Output)
  • Description: Output pin for colour sensor data
  • Function: Provides the colour data sensed by the module to the microcontroller
  • Connection: Connect to an analog input pin on the microcontroller (e.g., Arduino)
  • Pin 6: INT (Interrupt)
  • Description: Interrupt pin (optional)
  • Function: Generates an interrupt signal when the colour sensor detects a specific condition (e.g., a certain colour threshold)
  • Connection: Connect to an interrupt pin on the microcontroller (e.g., Arduino) if using interrupt-based programming
  • Pin 7: ADDR (Address)
  • Description: Address pin (optional)
  • Function: Used to set the I2C address of the sensor module (dependent on the specific module)
  • Connection: Connect to a digital output pin on the microcontroller (e.g., Arduino) to set the address
  • Pin 8: NC (Not Connected)
  • Description: Not connected pin (dependent on the specific module)
  • Function: No function assigned to this pin
  • Connection: Leave unconnected
  • Connection Structure:
  • 1. Connect VCC to the power source (e.g., 3.3V or 5V from an Arduino board).
  • 2. Connect GND to the ground rail (e.g., GND on an Arduino board).
  • 3. Connect SCL to the SCL pin on the microcontroller (e.g., Arduino).
  • 4. Connect SDA to the SDA pin on the microcontroller (e.g., Arduino).
  • 5. Connect OUT to an analog input pin on the microcontroller (e.g., A0 on an Arduino Uno).
  • 6. If using interrupt-based programming, connect INT to an interrupt pin on the microcontroller (e.g., D2 on an Arduino Uno).
  • 7. If setting the I2C address, connect ADDR to a digital output pin on the microcontroller (e.g., D5 on an Arduino Uno).
  • 8. Leave Pin 8 (NC) unconnected.
  • Please note that the specific pin connections may vary depending on the microcontroller and the Colour Sensor module being used. Always refer to the datasheets and documentation provided with the components for accurate connection information.

Code Examples

Colour Sensor Documentation
The Colour Sensor is a versatile IoT component designed to measure and detect colours in various environments. This documentation provides an overview of the Colour Sensor's features, technical specifications, and code examples to help developers and makers integrate it into their projects.
Technical Specifications:
Communication Interface: I2C, SPI
 Colour Sensing Technology: RGB (Red, Green, Blue) photodiodes
 Colour Range: 360 ( full-color spectrum)
 Sensitivity: 10,000:1 dynamic range
 Power Supply: 3.3V to 5V
 Operating Temperature: -20C to 85C
Code Examples:
### Example 1: Colour Detection using Arduino
This example demonstrates how to use the Colour Sensor with an Arduino Board to detect and display the detected colour on the serial monitor.
```cpp
#include <Wire.h>
#define COLOUR_SENSOR_ADDRESS 0x39 // I2C address of the Colour Sensor
void setup() {
  Wire.begin();
  Serial.begin(9600);
}
void loop() {
  uint16_t r, g, b;
  
  // Read colour data from the Colour Sensor
  Wire.beginTransmission(COLOUR_SENSOR_ADDRESS);
  Wire.write(0x00); // Register for reading colour data
  Wire.endTransmission();
  
  Wire.requestFrom(COLOUR_SENSOR_ADDRESS, 6);
  r = Wire.read() << 8 | Wire.read();
  g = Wire.read() << 8 | Wire.read();
  b = Wire.read() << 8 | Wire.read();
  
  // Calculate the detected colour
  float h, s, v;
  RGBtoHSV(r, g, b, &h, &s, &v);
  
  // Print the detected colour to the serial monitor
  Serial.print("Detected Colour: ");
  if (h < 30 || h > 330) {
    Serial.println("Red");
  } else if (h >= 30 && h < 90) {
    Serial.println("Yellow");
  } else if (h >= 90 && h < 150) {
    Serial.println("Green");
  } else if (h >= 150 && h < 210) {
    Serial.println("Cyan");
  } else if (h >= 210 && h < 270) {
    Serial.println("Blue");
  } else if (h >= 270 && h < 330) {
    Serial.println("Magenta");
  }
  delay(500);
}
void RGBtoHSV(uint16_t r, uint16_t g, uint16_t b, float h, float s, float v) {
  float r_f = r / 255.0;
  float g_f = g / 255.0;
  float b_f = b / 255.0;
  
  float max = max(r_f, g_f, b_f);
  float min = min(r_f, g_f, b_f);
  float delta = max - min;
  
  v = max;
  
  if (max == 0) {
    s = 0;
    h = 0;
  } else {
    s = delta / max;
    
    if (r_f == max) {
      h = (g_f - b_f) / delta;
    } else if (g_f == max) {
      h = 2 + (b_f - r_f) / delta;
    } else {
      h = 4 + (r_f - g_f) / delta;
    }
    
    h = 60;
    if (h < 0) {
      h += 360;
    }
  }
}
```
### Example 2: Colour Matching using Raspberry Pi (Python)
This example demonstrates how to use the Colour Sensor with a Raspberry Pi to match detected colours with predefined colour values.
```python
import smbus
import time
# I2C bus and Colour Sensor address
I2C_BUS = 1
COLOUR_SENSOR_ADDRESS = 0x39
# Predefined colour values
COLOURS = {
    "Red": (255, 0, 0),
    "Green": (0, 255, 0),
    "Blue": (0, 0, 255),
    "Yellow": (255, 255, 0),
    "Cyan": (0, 255, 255),
    "Magenta": (255, 0, 255)
}
# Initialize the I2C bus
bus = smbus.SMBus(I2C_BUS)
while True:
    # Read colour data from the Colour Sensor
    bus.write_byte(COLOUR_SENSOR_ADDRESS, 0x00)  # Register for reading colour data
    r = bus.read_word_data(COLOUR_SENSOR_ADDRESS, 0x01)
    g = bus.read_word_data(COLOUR_SENSOR_ADDRESS, 0x02)
    b = bus.read_word_data(COLOUR_SENSOR_ADDRESS, 0x03)
    
    # Calculate the detected colour
    h, s, v = rgb_to_hsv(r, g, b)
    
    # Match the detected colour with the predefined colour values
    min_diff = float('inf')
    matched_colour = None
    for colour, (r_ref, g_ref, b_ref) in COLOURS.items():
        r_diff = abs(r - r_ref)
        g_diff = abs(g - g_ref)
        b_diff = abs(b - b_ref)
        diff = r_diff + g_diff + b_diff
        if diff < min_diff:
            min_diff = diff
            matched_colour = colour
    
    # Print the matched colour
    print("Matched Colour:", matched_colour)
    time.sleep(1)
def rgb_to_hsv(r, g, b):
    r_f = r / 255.0
    g_f = g / 255.0
    b_f = b / 255.0
    
    max_val = max(r_f, g_f, b_f)
    min_val = min(r_f, g_f, b_f)
    delta = max_val - min_val
    
    v = max_val
    if max_val == 0:
        s = 0
        h = 0
    else:
        s = delta / max_val
        if r_f == max_val:
            h = (g_f - b_f) / delta
        elif g_f == max_val:
            h = 2 + (b_f - r_f) / delta
        else:
            h = 4 + (r_f - g_f) / delta
        
        h = 60
        if h < 0:
            h += 360
    return h, s, v
```
Note: The code examples provided are for illustration purposes only and may require modifications to suit your specific project requirements. Ensure to calibrate the Colour Sensor and adjust the code according to the sensor's specifications and your project's needs.