> For the complete documentation index, see [llms.txt](https://docs.andymark.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.andymark.com/frc-electronics/can-sensors/can-color-sensor-am-5683/examples.md).

# Examples

## Use case

This example reads raw red, green, blue, clear, and proximity values from the CAN Color Sensor and publishes them to SmartDashboard. It also shows how to enable the onboard LED and safely normalize the color channels.

{% hint style="info" %}
The snippets use CAN bus 2 and device ID 0. Change both values to match the controller and sensor configuration.
{% endhint %}

## Mounting example

![CAN Color Sensor mounted for target detection](https://content.gitbook.com/content/cQgnvayQud3Xf5sZi0wz/blobs/OG1ogAquE0pJX5AcnUSq/image)

## Setup

Install the [AndyMark WPILib vendor library](/frc-electronics/wpilib-am-vendor-library-setup.md) before using these examples. LED control requires device firmware 1.1.1 or newer and AmLib 2026.0.2 or newer. See [Device API](/frc-electronics/can-sensors/can-color-sensor-am-5683/device-api.md) for all available methods.

{% tabs %}
{% tab title="Java" %}

```java
import com.andymark.jni.AM_CAN_Color_Sensor;
import com.andymark.jni.AM_CAN_Color_Sensor.AM_ColorSensorData;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

// CAN bus 2, device ID 0
AM_CAN_Color_Sensor colorSensor =
    new AM_CAN_Color_Sensor(2, 0);

// Restore the default 100 ms report period.
colorSensor.resetReportPeriod();

// Requires device firmware 1.1.1+ and AmLib 2026.0.2+.
colorSensor.turnLedOn();

AM_ColorSensorData data = colorSensor.getData();

SmartDashboard.putNumber("Color/RedRaw", data.red);
SmartDashboard.putNumber("Color/GreenRaw", data.green);
SmartDashboard.putNumber("Color/BlueRaw", data.blue);
SmartDashboard.putNumber("Color/Clear", data.clearC);
SmartDashboard.putNumber("Color/Proximity", data.proximity);
SmartDashboard.putNumber("Color/TimestampMs", data.millisStamp);

// Avoid division by zero when the clear channel is zero.
if (data.clearC > 0) {
    SmartDashboard.putNumber(
        "Color/RedNormalized",
        (double) data.red / data.clearC
    );
    SmartDashboard.putNumber(
        "Color/GreenNormalized",
        (double) data.green / data.clearC
    );
    SmartDashboard.putNumber(
        "Color/BlueNormalized",
        (double) data.blue / data.clearC
    );
}
```

{% endtab %}

{% tab title="C++" %}

```cpp
#include "AM_CAN_Color_Sensor.h"
#include <frc/smartdashboard/SmartDashboard.h>

// CAN bus 2, device ID 0
AM_CANColorSensor colorSensor{2, 0};

// Restore the default 100 ms report period.
colorSensor.ResetReportPeriod();

// Requires device firmware 1.1.1+ and AmLib 2026.0.2+.
colorSensor.TurnLedOn();

AM_ColorSensorData data = colorSensor.GetData();

frc::SmartDashboard::PutNumber("Color/RedRaw", data.red);
frc::SmartDashboard::PutNumber("Color/GreenRaw", data.green);
frc::SmartDashboard::PutNumber("Color/BlueRaw", data.blue);
frc::SmartDashboard::PutNumber("Color/Clear", data.clearC);
frc::SmartDashboard::PutNumber(
    "Color/Proximity",
    data.proximity
);
frc::SmartDashboard::PutNumber(
    "Color/TimestampMs",
    data.millisStamp
);

// Avoid division by zero when the clear channel is zero.
if (data.clearC > 0) {
    frc::SmartDashboard::PutNumber(
        "Color/RedNormalized",
        static_cast<double>(data.red) / data.clearC
    );
    frc::SmartDashboard::PutNumber(
        "Color/GreenNormalized",
        static_cast<double>(data.green) / data.clearC
    );
    frc::SmartDashboard::PutNumber(
        "Color/BlueNormalized",
        static_cast<double>(data.blue) / data.clearC
    );
}
```

{% endtab %}
{% endtabs %}

## Validation steps

1. Record raw values with no target present.
2. Record each target at the actual operating distance and angle.
3. Repeat with the robot’s normal illumination and surrounding structure installed.
4. Test under practice and event lighting.
5. Choose thresholds from measured separation between targets and include margin for noise.
