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.

Below is a list of available blocks:
set Optical light – Turns the Optical Sensor’s LED on or off.
set Optical light power – Adjusts the brightness of the Optical Sensor’s LED.
Optical found object? – Returns whether the sensor is detecting an object.
Optical detects color? – Returns whether the sensor is detecting a specified color.
Optical brightness – Returns the amount of light reflected from the object as a percent.
Optical hue – Returns the hue value of the detected color.
when Optical – Runs the attached stack when sensor detects or loses an object.
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
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:
|
Example
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:

([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:
|