Detects colours in the visible spectrum (400-700nm)
Detects colours in the visible spectrum (400-700nm)
5% of measured value
Adjustable from 1-100%
I2C or UART
3.3V or 5V
<10mA
-20C to 70C
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
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.
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.