### Install adafruit-circuitpython-tcs34725 in a virtual environment Source: https://github.com/adafruit/adafruit_circuitpython_tcs34725/blob/main/docs/index.md Install the driver within a virtual environment for a specific project. Activate the environment first. ```shell mkdir project-name && cd project-name python3 -m venv .venv source .venv/bin/activate pip3 install adafruit-circuitpython-tcs34725 ``` -------------------------------- ### Install adafruit-circuitpython-tcs34725 Source: https://github.com/adafruit/adafruit_circuitpython_tcs34725/blob/main/docs/index.md Install the driver locally for the current user. Ensure pip3 is available. ```shell pip3 install adafruit-circuitpython-tcs34725 ``` -------------------------------- ### Install adafruit-circuitpython-tcs34725 system-wide Source: https://github.com/adafruit/adafruit_circuitpython_tcs34725/blob/main/docs/index.md Install the driver system-wide. This may be required in some cases. Use with caution. ```shell sudo pip3 install adafruit-circuitpython-tcs34725 ``` -------------------------------- ### Install TCS34725 Driver via pip Source: https://github.com/adafruit/adafruit_circuitpython_tcs34725/blob/main/README.rst Commands to install the library on supported GNU/Linux systems. ```shell pip3 install adafruit-circuitpython-tcs34725 ``` ```shell sudo pip3 install adafruit-circuitpython-tcs34725 ``` ```shell mkdir project-name && cd project-name python3 -m venv .venv source .venv/bin/activate pip3 install adafruit-circuitpython-tcs34725 ``` -------------------------------- ### Continuous Color Reading Example Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Continuously monitors and displays color sensor readings including RGB hex, RGB tuple, raw RGBC values, color temperature, and lux. Adjust integration time and gain for optimal indoor readings. Handles sensor saturation by suggesting adjustments. ```python import board import time import adafruit_tcs34725 # Initialize I2C and sensor i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Configure sensor for optimal indoor readings sensor.integration_time = 150 # 150ms integration time sensor.gain = 4 # 4x gain for indoor lighting print("TCS34725 Color Sensor - Continuous Reading") print("=" * 45) while True: # Read all color formats color_int = sensor.color color_rgb = sensor.color_rgb_bytes r_raw, g_raw, b_raw, c_raw = sensor.color_raw # Read light measurements temp = sensor.color_temperature lux = sensor.lux # Display readings print(f"RGB Hex: #{color_int:06X}") print(f"RGB Tuple: {color_rgb}") print(f"Raw RGBC: R={r_raw}, G={g_raw}, B={b_raw}, C={c_raw}") if temp and lux: print(f"Color Temp: {temp:.0f}K | Lux: {lux:.1f}") else: print("Sensor saturated - try reducing integration time or gain") print("-" * 45) time.sleep(1.0) ``` -------------------------------- ### Integration Time Property Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Gets or sets the sensor's integration time in milliseconds. Valid range is 2.4ms to 614.4ms. ```APIDOC ## integration_time Property The `integration_time` property gets or sets the sensor's integration time in milliseconds. Valid range is 2.4ms to 614.4ms in 2.4ms increments. Longer integration times increase sensitivity but reduce the maximum light level that can be measured before saturation. ### Request Example ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Get current integration time print(f"Current integration time: {sensor.integration_time}ms") # Set integration time for different conditions # Short time (2.4ms) - bright light, fast readings sensor.integration_time = 2.4 # Medium time (150ms) - balanced sensitivity sensor.integration_time = 150 # Long time (614.4ms) - low light, maximum sensitivity sensor.integration_time = 614.4 print(f"New integration time: {sensor.integration_time}ms") ``` ### Response Example ``` Current integration time: 2.4ms New integration time: 614.4ms ``` ``` -------------------------------- ### Gain Property Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Gets or sets the sensor's analog gain. Valid values are 1, 4, 16, or 60. ```APIDOC ## gain Property The `gain` property gets or sets the sensor's analog gain. Valid values are 1, 4, 16, or 60. Higher gain increases sensitivity for detecting colors in low light but may cause saturation in bright light. ### Request Example ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Get current gain setting print(f"Current gain: {sensor.gain}x") # Set gain for different lighting conditions sensor.gain = 1 # Bright light - minimum amplification sensor.gain = 4 # Normal indoor lighting sensor.gain = 16 # Dim lighting sensor.gain = 60 # Very low light - maximum sensitivity print(f"New gain: {sensor.gain}x") # Optimal settings for low-light color detection sensor.integration_time = 150 sensor.gain = 16 ``` ### Response Example ``` Current gain: 1x New gain: 16x ``` ``` -------------------------------- ### Glass Attenuation Property Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Gets or sets the Glass Attenuation (GA) factor to compensate for light loss when the sensor is behind glass. ```APIDOC ## glass_attenuation Property The `glass_attenuation` property gets or sets the Glass Attenuation (GA) factor to compensate for light loss when the sensor is behind glass or a protective cover. The GA is the inverse of glass transmissivity (GA = 1/T). Default is 1.0 (no glass). ### Request Example ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Default: no glass attenuation print(f"Glass attenuation: {sensor.glass_attenuation}") # Compensate for 50% transmissivity glass (GA = 1/0.50 = 2.0) sensor.glass_attenuation = 2.0 # Compensate for 80% transmissivity glass (GA = 1/0.80 = 1.25) sensor.glass_attenuation = 1.25 # Read corrected values lux = sensor.lux temp = sensor.color_temperature print(f"Corrected Lux: {lux:.1f}, Temp: {temp:.0f}K") ``` ### Response Example ``` Glass attenuation: 1.0 Corrected Lux: 100.5, Temp: 5000K ``` ``` -------------------------------- ### DisplayIO Integration for Color Sensor Readings Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Displays real-time color sensor readings (RGB, color temperature, lux) on an attached display using DisplayIO. Requires labels for each reading and updates them in a loop. ```python import board import time from adafruit_display_text.bitmap_label import Label from displayio import Group from terminalio import FONT import adafruit_tcs34725 # Create display group main_group = Group() # Initialize sensor i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Create labels for display output color_label = Label(FONT, text="", scale=2) color_label.anchor_point = (0, 0) color_label.anchored_position = (4, 20) temp_label = Label(FONT, text="", scale=2) temp_label.anchor_point = (0, 0) temp_label.anchored_position = (4, 60) lux_label = Label(FONT, text="", scale=2) lux_label.anchor_point = (0, 0) lux_label.anchored_position = (4, 100) # Add labels to group main_group.append(color_label) main_group.append(temp_label) main_group.append(lux_label) # Set display root group board.DISPLAY.root_group = main_group # Update loop while True: color_label.text = f"RGB: {sensor.color_rgb_bytes}" temp_label.text = f"Temp: {sensor.color_temperature:.0f}K" lux_label.text = f"Lux: {sensor.lux:.1f}" time.sleep(0.5) ``` -------------------------------- ### Configure Glass Attenuation Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Sets the compensation factor for light loss when the sensor is placed behind protective glass. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Default: no glass attenuation print(f"Glass attenuation: {sensor.glass_attenuation}") # Output: Glass attenuation: 1.0 # Compensate for 50% transmissivity glass (GA = 1/0.50 = 2.0) sensor.glass_attenuation = 2.0 # Compensate for 80% transmissivity glass (GA = 1/0.80 = 1.25) sensor.glass_attenuation = 1.25 # Read corrected values lux = sensor.lux temp = sensor.color_temperature print(f"Corrected Lux: {lux:.1f}, Temp: {temp:.0f}K") ``` -------------------------------- ### Configure Integration Time Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Sets the sensor integration time between 2.4ms and 614.4ms. Longer times increase sensitivity but lower the saturation threshold. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Get current integration time print(f"Current integration time: {sensor.integration_time}ms") # Default output: Current integration time: 2.4ms # Set integration time for different conditions # Short time (2.4ms) - bright light, fast readings sensor.integration_time = 2.4 # Medium time (150ms) - balanced sensitivity sensor.integration_time = 150 # Long time (614.4ms) - low light, maximum sensitivity sensor.integration_time = 614.4 print(f"New integration time: {sensor.integration_time}ms") # Example output: New integration time: 614.4ms ``` -------------------------------- ### Configure Interrupt Thresholds Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Sets 16-bit thresholds and persistence filters to trigger hardware interrupts based on clear channel light levels. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Configure interrupt thresholds (16-bit values: 0-65535) sensor.min_value = 1000 # Trigger if clear channel < 1000 sensor.max_value = 50000 # Trigger if clear channel > 50000 # Set persistence filter - number of consecutive readings outside # threshold before triggering interrupt # Valid values: 0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 ``` -------------------------------- ### Read TCS34725 sensor data Source: https://github.com/adafruit/adafruit_circuitpython_tcs34725/blob/main/docs/examples.md Initializes the sensor via I2C and continuously prints color, temperature, and lux values. ```python # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Simple demo of the TCS34725 color sensor. # Will detect the color from the sensor and print it out every second. import time import board import adafruit_tcs34725 # Create sensor object, communicating over the board's default I2C bus i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = adafruit_tcs34725.TCS34725(i2c) # Change sensor integration time to values between 2.4 and 614.4 milliseconds # sensor.integration_time = 150 # Change sensor gain to 1, 4, 16, or 60 # sensor.gain = 4 # Main loop reading color and printing it every second. while True: # Raw data from the sensor in a 4-tuple of red, green, blue, clear light component values # print(sensor.color_raw) color = sensor.color color_rgb = sensor.color_rgb_bytes print(f"RGB color as 8 bits per channel int: #{color:02X} or as 3-tuple: {color_rgb}") # Read the color temperature and lux of the sensor too. temp = sensor.color_temperature lux = sensor.lux print(f"Temperature: {temp}K Lux: {lux}\n") # Delay for a second and repeat. time.sleep(1.0) ``` ```python # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT # Simple demo of the TCS34725 color sensor. # Will detect the color from the sensor and print it out every second. import time import board import adafruit_tcs34725 # Create sensor object, communicating over the board's default I2C bus i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = adafruit_tcs34725.TCS34725(i2c) # Change sensor integration time to values between 2.4 and 614.4 milliseconds # sensor.integration_time = 150 # Change sensor gain to 1, 4, 16, or 60 # sensor.gain = 4 # Main loop reading color and printing it every second. while True: # Raw data from the sensor in a 4-tuple of red, green, blue, clear light component values # print(sensor.color_raw) color = sensor.color color_rgb = sensor.color_rgb_bytes print(f"RGB color as 8 bits per channel int: #{color:02X} or as 3-tuple: {color_rgb}") # Read the color temperature and lux of the sensor too. temp = sensor.color_temperature lux = sensor.lux print(f"Temperature: {temp}K Lux: {lux}\n") # Delay for a second and repeat. time.sleep(1.0) ``` -------------------------------- ### Configure Sensor Gain Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Adjusts analog gain (1, 4, 16, or 60) to balance sensitivity and saturation. Higher gain is better for low-light conditions. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Get current gain setting print(f"Current gain: {sensor.gain}x") # Default output: Current gain: 1x # Set gain for different lighting conditions sensor.gain = 1 # Bright light - minimum amplification sensor.gain = 4 # Normal indoor lighting sensor.gain = 16 # Dim lighting sensor.gain = 60 # Very low light - maximum sensitivity print(f"New gain: {sensor.gain}x") # Example output: New gain: 16x # Optimal settings for low-light color detection sensor.integration_time = 150 sensor.gain = 16 ``` -------------------------------- ### Read Ambient Light with lux Property Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Retrieves the ambient light level in lux. Returns None if the sensor is saturated. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Read ambient light level in lux lux = sensor.lux if lux is not None: print(f"Ambient Light: {lux:.1f} lux") # Classify lighting conditions if lux < 50: print("Dim lighting") elif lux < 500: print("Indoor lighting") else: print("Bright/outdoor lighting") else: print("Sensor saturated - adjust settings") # Example output: Ambient Light: 325.7 lux # Indoor lighting ``` -------------------------------- ### Initialize TCS34725 Sensor Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Create an I2C bus and initialize the TCS34725 sensor. The default I2C address is 0x29. Ensure the sensor is connected and detected by checking the sensor ID register. ```python import board import adafruit_tcs34725 # Create I2C bus using default pins (board.SCL and board.SDA) i2c = board.I2C() # Initialize the TCS34725 sensor with default address (0x29) sensor = adafruit_tcs34725.TCS34725(i2c) # Alternative: Use STEMMA QT connector on supported boards # i2c = board.STEMMA_I2C() # sensor = adafruit_tcs34725.TCS34725(i2c) # Alternative: Use custom I2C address if hardware configured differently # sensor = adafruit_tcs34725.TCS34725(i2c, address=0x39) ``` -------------------------------- ### Interrupt Threshold Configuration Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Configures hardware interrupts for color change detection based on clear channel value thresholds. ```APIDOC ## Interrupt Threshold Configuration The sensor supports hardware interrupts when the clear channel value falls outside configured thresholds. Use `min_value`, `max_value`, `cycles`, and `interrupt` properties to configure and manage interrupt-based color change detection. ### Request Example ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Configure interrupt thresholds (16-bit values: 0-65535) sensor.min_value = 1000 # Trigger if clear channel < 1000 sensor.max_value = 50000 # Trigger if clear channel > 50000 # Set persistence filter - number of consecutive readings outside # threshold before triggering interrupt # Valid values: 0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 sensor.cycles = 10 # Enable interrupts sensor.interrupt = True ``` ### Response Example ``` # Interrupts configured successfully. ``` ``` -------------------------------- ### Lux Property Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Reads the ambient light level in lux. Returns None if the sensor is saturated. ```APIDOC ## lux Property The `lux` property returns the ambient light level in lux (lumens per square meter). This value is computed using the DN40 algorithm with IR rejection for improved accuracy. Returns `None` if the sensor is saturated. ### Request Example ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Read ambient light level in lux lux = sensor.lux if lux is not None: print(f"Ambient Light: {lux:.1f} lux") # Classify lighting conditions if lux < 50: print("Dim lighting") elif lux < 500: print("Indoor lighting") else: print("Bright/outdoor lighting") else: print("Sensor saturated - adjust settings") ``` ### Response Example ``` Ambient Light: 325.7 lux Indoor lighting ``` ``` -------------------------------- ### Sensor Properties Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Properties for accessing color data, raw sensor readings, and color temperature. ```APIDOC ## Properties ### color - **Type**: int (24-bit) - **Description**: Returns the RGB color as a single 24-bit integer (0xRRGGBB). ### color_rgb_bytes - **Type**: tuple (3 bytes) - **Description**: Returns the RGB color as a 3-tuple of bytes (red, green, blue), normalized and gamma-corrected. ### color_raw - **Type**: tuple (4 integers) - **Description**: Returns the raw 16-bit RGBC (Red, Green, Blue, Clear) values directly from the sensor. ### color_temperature - **Type**: float or None - **Description**: Returns the color temperature in degrees Kelvin. Returns None if the sensor is saturated. ``` -------------------------------- ### TCS34725 Class Initialization Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Initializes the TCS34725 sensor instance using an I2C bus object. ```APIDOC ## Class Initialization ### Description Creates a driver instance for the TCS34725 color sensor. Validates the sensor by checking the ID register. ### Parameters - **i2c** (I2C object) - Required - The I2C bus object. - **address** (int) - Optional - The I2C address of the sensor (default 0x29). ``` -------------------------------- ### Active Property Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Controls the power state of the sensor. Set to True to enable, False to put in low-power sleep mode. ```APIDOC ## active Property The `active` property controls the power state of the sensor. Set to `True` to enable the sensor for color measurements, or `False` to put it in low-power sleep mode. The sensor is automatically activated when reading color values. ### Request Example ```python import board import time import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Check if sensor is active print(f"Sensor active: {sensor.active}") # Manually enable the sensor sensor.active = True print(f"Sensor active: {sensor.active}") # Take a reading (sensor auto-activates if needed) color = sensor.color print(f"Color: #{color:06X}") # Disable sensor to save power sensor.active = False time.sleep(5) # Low power mode # Re-enable for more readings sensor.active = True ``` ### Response Example ``` Sensor active: False Sensor active: True Color: #FF0000 ``` ``` -------------------------------- ### Configure Interrupt Persistence Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Sets the number of consecutive out-of-range readings required to trigger an interrupt. Use -1 to disable interrupts. Displays current min, max, and persistence cycle settings. ```python sensor.cycles = 5 # Require 5 consecutive out-of-range readings print(f"Min threshold: {sensor.min_value}") print(f"Max threshold: {sensor.max_value}") print(f"Persistence cycles: {sensor.cycles}") # Check if interrupt is triggered if sensor.interrupt: print("Interrupt triggered - color changed!") # Clear the interrupt sensor.interrupt = False ``` -------------------------------- ### Read RGB Color as 24-bit Integer Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Read the detected RGB color as a single 24-bit integer (0xRRGGBB format). This format is useful for display color settings. Individual color channels can be extracted using bitwise operations. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Read color as a 24-bit integer color = sensor.color print(f"RGB color as hex: #{color:06X}") # Example output: RGB color as hex: #FF5733 # Extract individual channels from the integer red = (color >> 16) & 0xFF green = (color >> 8) & 0xFF blue = color & 0xFF print(f"Red: {red}, Green: {green}, Blue: {blue}") # Example output: Red: 255, Green: 87, Blue: 51 ``` -------------------------------- ### Manage Sensor Power State Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Controls the active state of the sensor to toggle between measurement and low-power sleep modes. ```python import board import time import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Check if sensor is active print(f"Sensor active: {sensor.active}") # Output: Sensor active: False # Manually enable the sensor sensor.active = True print(f"Sensor active: {sensor.active}") # Output: Sensor active: True # Take a reading (sensor auto-activates if needed) color = sensor.color print(f"Color: #{color:06X}") # Disable sensor to save power sensor.active = False time.sleep(5) # Low power mode # Re-enable for more readings sensor.active = True ``` -------------------------------- ### Read RGB Color as Bytes Tuple Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Read the detected RGB color as a 3-tuple of bytes (red, green, blue), with each value ranging from 0-255. These values are normalized and gamma-corrected for accurate representation. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Read color as RGB tuple (0-255 per channel) r, g, b = sensor.color_rgb_bytes print(f"RGB: ({r}, {g}, {b})") # Example output: RGB: (255, 87, 51) # Use with display libraries color_rgb = sensor.color_rgb_bytes print(f"Color tuple: {color_rgb}") # Example output: Color tuple: (255, 87, 51) ``` -------------------------------- ### Read Color Temperature Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Read the color temperature in degrees Kelvin. Returns `None` if the sensor is saturated and the calculation cannot be performed. Useful for classifying light sources. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Read color temperature in Kelvin temp = sensor.color_temperature if temp is not None: print(f"Color Temperature: {temp:.0f}K") # Classify the light source if temp < 3500: print("Warm light (incandescent/candle)") elif temp < 5500: print("Neutral light (daylight)") else: print("Cool light (overcast/blue sky)") else: print("Sensor saturated - reduce light or decrease integration time") # Example output: Color Temperature: 4250K # Neutral light (daylight) ``` -------------------------------- ### Read Raw RGBC Values Source: https://context7.com/adafruit/adafruit_circuitpython_tcs34725/llms.txt Read the raw Red, Green, Blue, and Clear (RGBC) values directly from the sensor. Returns a 4-tuple of 16-bit values (0-65535). The clear channel indicates overall light intensity. ```python import board import adafruit_tcs34725 i2c = board.I2C() sensor = adafruit_tcs34725.TCS34725(i2c) # Read raw 16-bit color values red, green, blue, clear = sensor.color_raw print(f"Raw RGBC: R={red}, G={green}, B={blue}, C={clear}") # Example output: Raw RGBC: R=4523, G=3892, B=2156, C=10234 # Calculate percentage of each color relative to clear channel if clear > 0: red_pct = (red / clear) * 100 green_pct = (green / clear) * 100 blue_pct = (blue / clear) * 100 print(f"Percentages: R={red_pct:.1f}%, G={green_pct:.1f}%, B={blue_pct:.1f}%") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.