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 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 to set the LED to:

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

set Optical light power#

The set Optical light power block sets the brightness of the Optical Sensor’s LED. If the LED’s brightness is set above 0 while it is off, it will automatically turn the LED on.

  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 LED to as a percent.

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

Optical Sensor found object?#

The Optical Sensor found object? block returns a Boolean indicating whether the sensor is detecting an object.

  • True – The Optical Sensor is detecting an object.

  • False – The Optical Sensor is not detecting 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 Sensor detects color?#

The Optical Sensor detects color? block returns a Boolean indicating whether the sensor is detecting a specified color.

  • True – The Optical Sensor is detecting the specified color.

  • False – The Optical Sensor is not detecting 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 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 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 brightness#

The Optical brightness block returns the amount of light reflected from the object as a percent.

  ([Optical1 v] brightness in %)

Parameters

Description

device

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

Optical hue#

The Optical hue block returns the hue value of the detected color.

This value ranges from 0 to 359 degrees, which correlates to the 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 block runs the attached stack 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 – An undetected object is now detected
  • loses – A detected object is now no longer detected