Color Detector

The light sensor works by shining a white light at an object and then recording the reflected colour. It can also record the intensity of the reflection (brightness). Through red, green and blue colour filters the photodiode converts the amount of light to current.

MetaEnvironment boards come equipped with a TCS34725 color detector. Enums and functions for interacting with this sensor are defined in the colordetector_tcs34725.h header file.

Configuration

The TCS34725 sensor has 2 configurable parameters that affect the data range and resultion:

Parameter

Description

Gain

Analog signal scale

Integration Time

Amount of time spent to aggregate adc values

Afer you configure the API with your desired settings, call mbl_mw_cd_tcs34725_write_config to write the changes to the detector.

#include "metawear/sensor/colordetector_tcs34725.h"

void colordetector_config(MblMwMetaWearBoard* board) {
    // set analog gain to 16x
    mbl_mw_cd_tcs34725_set_gain(board, MBL_MW_CD_TCS34725_GAIN_16X);

    // set integration time to 4.8ms
    mbl_mw_cd_tcs34725_set_integration_time(board, 4.8f);

    // write the configuration to the sensor
    mbl_mw_cd_tcs34725_write_config(board);
}

The MetaWear board also comes with an illuminator LED which can be used to provide additional light when reading the color. To enable the LED, call mbl_mw_cd_tcs34725_enable_illuminator_led before writing the configuration changes.

Color ADC

To retrieve color adc, call mbl_mw_datasignal_read with a color adc data signal.

Users can use the timer functions (see Timer section)

to schedule periodic reads.

#include "metawear/core/datasignal.h"
#include "metawear/core/types.h"

void colordetector_adc_stream(MblMwMetaWearBoard* board) {
    auto adc_signal= mbl_mw_cd_tcs34725_get_adc_data_signal(board);

    mbl_mw_datasignal_subscribe(adc_signal, [](const MblMwData* data) -> void {
        // Cast value to MblMwTcs34725ColorAdc*
        auto color_adc = (MblMwTcs34725ColorAdc*) data->value;
        printf("{clear: %d, red: %d, green: %d, blue: %d}", color_adc->clear,
                color_adc->red, color_adc->green, color_adc->blue);
    });
    mbl_mw_datasignal_read(adc_signal);
}