Optical Sensor#

Introduction#

The V5 Optical Sensor detects the color, brightness, and presence of nearby objects using a combination of an RGB color sensor and proximity sensor. It can also use its built-in LED to illuminate objects for more accurate detection.

The VEX V5 Optical Sensor.

Below is a list of available blocks:

set Optical light#

The set Optical light stack block turns the Optical Sensor’s LED on or off. This can help increase the sensor’s accuracy when detecting colors.

  set [Optical1 v] light [on v]

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

state

The state of the Optical Sensor’s light: on or off.

  when started
  [Turn on the sensor's LED.]
  set [Optical1 v] light [on v]

set Optical light power#

The set Optical light power stack block sets the brightness of the Optical Sensor’s LED. The light can help the Optical Sensor detect objects and colors more clearly.

A higher percentage makes the light brighter. A lower percentage makes the light dimmer.

If the Optical Sensor’s light is off, setting the light power above 0% will turn the light on.

If the Optical Sensor’s light is on, setting the light power at 0% will turn the light off.

  set [Optical1 v] light power to [50] %

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

brightness

The brightness to set the Optical Sensor’s light to, from 0% to 100%. Use whole numbers.

  when started
  [Turn on the sensor's LED at half brightness.]
  set [Optical1 v] light power to [50] %

Optical found an object#

The Optical found an object Boolean block reports whether or not the Optical Sensor detects an object within range.

  • True — The Optical Sensor detects an object.

  • False — The Optical Sensor does not detect an object.

  <[Optical1 v] found an object?>

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

Example

Drive forward until an object is detected#
  when started
  [Drive forward until an object is found.]
  drive [forward v]
  wait until <[Optical1 v] found an object?>
  stop driving

Optical detects color#

The Optical detects color Boolean block reports whether the Optical Sensor detects a specified color, based on the detected hue value.

  • True – The sensor detects the specified color.

  • False – The sensor does not detect the specified color.

Note: The Optical Sensor is looking for hue ranges that match the specified color. For detecting specific hue ranges, see the Optical hue reporter block.

  <[Optical1 v] detects [red v]?>

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

color

Which color to look for:

  • red – A detected hue value between 340° - 20°.
  • green – A detected hue value between 80° - 140°.
  • blue – A detected hue value between 200° - 240°.
  • yellow – A detected hue value between 40° - 60°.
  • orange – A detected hue value between 20° - 40°.
  • purple – A detected hue value between 240° - 280°.
  • cyan – A detected hue value between 140° - 200°.

Example

Drive forward until red is detected#
  when started
  [Drive forward until red is found.]
  drive [forward v]
  wait until <[Optical1 v] detects [red v]?>
  stop driving

Optical brightness#

The Optical brightness reporter block reports how much light is reflected back to the Optical Sensor, as a percentage from 0% to 100%.

A higher percentage means more light is reflected back to the Optical Sensor. A lower percentage means less light is reflected back.

  ([Optical1 v] brightness in %)

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

Optical hue#

The Optical hue reporter block reports the hue detected by the Optical Sensor as a number from 0 to 359 degrees.

Hue is a way to describe color using numbers around a color wheel.

The VEX Color Wheel showing the degrees of colors around a circle, with red at 0 degrees and transitioning through the rainbow as the value increases.

  ([Optical1 v] hue in degrees)

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

Example

  when started
  [Look for the color pink using hue.]
  forever
  clear screen
  set cursor to row [1] column [1] on screen
  if <[300] [math_less_than v] ([Optical1 v] hue in degrees) [math_less_than v] [359]> then
    print [This is pink!] on screen ▶
  else
  print [No pink detected!] on screen ▶

when Optical#

The when Optical hat block runs the attached stack of blocks whenever the Optical Sensor detects or loses an object.

  when [Optical1 v] [detects v] an object :: hat events

Parameters

Description

device

Which Optical Sensor to use, configured in the Devices window.

state

When the attached stack of blocks will run: detects runs when an undetected object is detected, while loses runs when a detected object is now no longer detected.