Sensors in FTC

beginner15 min

Overview

Sensors let the robot perceive its environment. Configure each sensor in the robot’s hardware configuration, then map it in code. Common types: distance (obstacle detection, alignment), color (line following, object detection), and IMU (orientation, turning).

Distance Sensors

Distance sensors measure how far objects are. Use for obstacle avoidance, alignment, and detecting game elements.

Distance Sensor

Read distance in centimeters. Handle NaN for out-of-range readings.

private DistanceSensor distanceSensor;

distanceSensor = hardwareMap.get(DistanceSensor.class, "distance_sensor");
double distance = distanceSensor.getDistance(DistanceUnit.CM);

if (!Double.isNaN(distance) && distance < 10) {
    leftDrive.setPower(0);
    rightDrive.setPower(0);
}

Color Sensors

Color sensors return red, green, blue, and alpha values. Use for line following, game element detection, and object differentiation. Ambient light affects readings.

Color Sensor

Read RGB values and detect conditions.

private ColorSensor colorSensor;

colorSensor = hardwareMap.get(ColorSensor.class, "color_sensor");
colorSensor.enableLed(true);

int red = colorSensor.red();
int green = colorSensor.green();
int blue = colorSensor.blue();

if (red > 100 && red > blue) {
    telemetry.addData("Object", "Red detected");
}

IMU (Inertial Measurement Unit)

The IMU measures orientation (heading, pitch, roll). REV hubs include a built-in IMU; external BNO055 modules connect via I2C. Use for turning to angles, field-centric drive, and navigation.

IMU Initialization and Heading

Both built-in and external BNO055 use the same initialization. Use a different I2C port than 0 for external IMUs.

private BNO055IMU imu;

imu = hardwareMap.get(BNO055IMU.class, "imu");
BNO055IMU.Parameters params = new BNO055IMU.Parameters();
params.angleUnit = BNO055IMU.AngleUnit.DEGREES;
imu.initialize(params);

float heading = imu.getAngularOrientation().firstAngle;

Troubleshooting

Distance: NaN usually means out of range. Color: test in your environment; use thresholds. IMU: calibrate before matches; handle drift.

Further Reading