Optical Sensors

V5 Optical Sensor

Some tasks in robotics require the robot to actually “see” what it is picking up. For example: sorting colored discs, stopping a conveyor belt when an object arrives, or detecting the color of a game piece before deciding what to do with it. For these tasks, robots use Optical Sensors.

An optical sensor is a small device that combines three abilities into one: it can detect how close something is, what color it is, and how bright the light around it is. It does this by shining its own light and measuring how that light bounces back.

Why does this matter?

In competition games that involve sorting colored pieces, an optical sensor can automate the entire process. The robot can detect the color, make a decision, and react — all without any human input!

Key Terms

  • Light Intensity: How much total light the sensor picks up, measured in Lux. (Like how bright a room feels.)
  • Reflectance: How much of the sensor’s light bounces back from a surface. Shiny, bright objects reflect a lot. Dark, matte objects reflect very little.
  • Ambient Light: Room lighting or sunlight that was not produced by the sensor. This “background” light can confuse the sensor if not managed.
  • Threshold: A value you set in code as a boundary. For example: “if hue is between 0 and 22, it is red.”

How it Works

The optical sensor has three systems working together through a single lens:

1. Proximity Detection

The sensor shoots out invisible infrared (IR) light — similar to a TV remote. When an object is close, the light bounces back to the sensor. The sensor measures the strength of the returning light and converts it to a number between 0.0 (nothing nearby) and 1.0 (right up against the lens). This tells you how close an object is.

2. Color Detection (RGB + White LED)

To read color accurately, the sensor has a built-in white LED. It turns on this light to illuminate the object, then measures how much Red, Green, and Blue light bounces back. These three values are combined and converted into a single Hue Angle — a number between 0 and 360 degrees representing the color on a color wheel.

VEX Hue Color Chart

For example:

  • Red is around 0–30 degrees
  • Green is around 100–150 degrees
  • Blue is around 210–250 degrees

3. Update Speed

The sensor reads new data many times per second. The relationship between update speed and update time is:

$$f = \frac{1}{T}$$

Where:

  • f is how many times per second the sensor updates (Hertz, Hz)
  • T is the time between each update (in seconds)

Faster updates make the robot react more quickly, but noisy sensor readings can cause problems if updates are too fast for the sensor to stabilize. A 20ms loop cycle is a good default.

Hardware Specifics

  • VEX V5: Plugs into any of the 21 Smart Ports. Live sensor data appears on the V5 Brain screen.
  • VEX IQ Gen 2: Uses the same smart port system with 12 universal ports.
  • Best Range: The sensor reads color and proximity most accurately when the object is within 100mm (about 4 inches) of the lens.

Pro Tip: Build a small hood (a cover made of dark plastic or metal) around the sensor to block out room lighting. If only the sensor’s own LED illuminates the object, the readings will be much more consistent regardless of the room’s ambient lighting.

Troubleshooting

  • Check the dashboard first: Before writing code, open the Devices menu on the V5 Brain. Confirm the sensor is connected and watch the live values. If the hue number bounces around wildly while the robot is still, the sensor may be picking up glare or vibrations.
  • Do the cable test: Move your robot’s mechanisms through their full range of motion while watching the sensor’s wire. Make sure the cable does not get snagged or yanked loose.
  • Calibrate with real game pieces: Hold actual game pieces in front of the sensor and write down the exact hue values you read. Use those real numbers to set your thresholds in code — do not guess!

Connection to Code

Below is sample code for using optical sensors.

void autonomousColorSorter() {
    // 1. Turn on the built-in LED to full power to standardize the lighting environment
    OpticalSensor.setLightPower(100, percent);

    // 2. Continuous control update loop
    while (true) {
        // Only run sorting checks if an object is highly close to the lens
        if (OpticalSensor.proximity() > 0.85) {

            // Read the exact hue value (0.0 to 360.0 degrees)
            double currentHue = OpticalSensor.hue();

            // Define a strict mathematical threshold window for a RED piece
            if (currentHue >= 0.0 && currentHue <= 22.0) {
                // Friendly color detected: Index into the robot storage
                ConveyorMotor.spin(forward, 100, percent);
            }
            // Define a threshold window for an ENEMY BLUE piece
            else if (currentHue >= 210.0 && currentHue <= 245.0) {
                // Enemy color detected: Fire pneumatic piston to eject the piece
                EjectPiston.set(true);
                wait(80, msec);
                EjectPiston.set(false);
            }
        } else {
            // No object present: Keep intake running smoothly
            ConveyorMotor.spin(forward, 50, percent);
        }

        // Fixed loop cycle time (T) to stabilize CPU processing load
        wait(20, msec);
    }
}
What is the first thing you should measure or inspect when the robot has a problem with optical sensors?Start with the part of the robot most directly connected to light intensity. Then check one measurable behavior, such as distance, time, angle, sensor value, current draw, or number of successful cycles.
How does an optical sensor measure hue?By emitting light onto a target and measuring the intensity of the wavelengths reflected back, then using those values to calculate the color's position on a 360 degree color wheel.