Reading sensors
Check what a sensor actually measures before using its reading to control a robot.
Shared configuration. The optical example adds a sensor on Smart Port 9. Shared configuration ↗
On this pageChoose the measurement
Choose the measurement
| Sensor | Useful reading | Important limitation |
|---|---|---|
| Inertial | Heading or continuous rotation | Keep still during calibration; heading wraps. |
| Rotation | Continuous shaft position | Wheel slip and lost ground contact still matter. |
| Distance | Range to the nearest detected object | The sensor does not identify a field wall. |
| Optical | Object proximity and hue | Lighting, surface, and mounting affect the reading. |
References: VEX Robotics: Inertial Sensor — C++ API (opens in a new tab) · VEX Robotics: Rotation Sensor — C++ API (opens in a new tab) · VEX Robotics: Distance Sensor — C++ API (opens in a new tab) · VEX Robotics: Optical Sensor — C++ API (opens in a new tab)
Read an optical sensor
The Optical Sensor can report proximity and a hue angle around the color wheel. Check isNearObject() before classifying color. A hue value without an object in the intended sensing area is not a reliable detection.
The example checks a red interval that crosses 0° using an OR condition. Set the light power and test your own thresholds with the real object, mounting distance, and expected ambient lighting.
optical colorSensor(PORT9);
bool isRedObject() {
if (!colorSensor.isNearObject()) return false;
double hue = colorSensor.hue();
return hue >= 340.0 || hue <= 20.0;
}
void autonomous() {
stopDrive();
colorSensor.setLightPower(50, percent);
timer elapsed;
elapsed.reset();
while (elapsed.time(msec) < 10000) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.print("near %d hue %.1f red %d ",
colorSensor.isNearObject(), colorSensor.hue(), isRedObject());
wait(50, msec);
}
colorSensor.setLight(ledState::off);
}References: VEX Robotics: Optical Sensor — C++ API (opens in a new tab) · VEX Robotics: Brain Screen — C++ API (opens in a new tab)
Reject invalid readings
Check that the expected device is connected, that an object is detected when required, and that the value is finite and inside a plausible range. A missing reading should trigger a defined fallback instead of being treated as a real distance.
Keep the original units until a clear conversion point. For example, convert millimeters to inches with rangeMm / 25.4 before combining a distance reading with an inch-based pose.
References: VEX Robotics: Distance Sensor — C++ API (opens in a new tab)
Filter noise without hiding motion
A median over a few recent valid readings can reject an isolated spike. An average can smooth noise but also delays the signal. That delay matters when a moving object passes a color sensor or a robot approaches a wall.
For a detection, require several consecutive matching samples if brief flicker is a problem. Reset the counter when the condition fails. Confirm that the added delay still leaves enough time to respond. A stable incorrect object can pass a filter, so filtering does not establish object identity.
Test before controlling a motor
- Print the raw reading while the robot is stationary.
- Move one object or shaft at a time. Confirm the sign, units, wraparound, and useful range.
- Try no object, an unexpected surface, and normal room lighting changes.
- Record observed ranges before selecting a threshold.
- Only connect the reading to motor control once you can explain both the normal and invalid states.
Sources for this guide
Examples are written for VEXcode C++. PROS and LemLib references explain related concepts; their APIs are different.
- VEX RoboticsOptical Sensor — C++ API (opens in a new tab)
Optical proximity, hue, and illumination controls used in the sensor guide.
- VEX RoboticsDistance Sensor — C++ API (opens in a new tab)
Object detection, distance units, installation checks, and the stated 20–2,000 mm operating range.
- VEX RoboticsRotation Sensor — C++ API (opens in a new tab)
Continuous shaft position and sensor reversal for tracking wheels.
- VEX RoboticsInertial Sensor — C++ API (opens in a new tab)
Calibration, continuous rotation, heading wraparound, and the default clockwise-positive convention.
- VEX RoboticsBrain Screen — C++ API (opens in a new tab)
Screen cursor positioning and formatted output for diagnostics.