Using the Optical Sensor with VEX V5

The Optical Sensor is one of the powerful V5 sensors which are designed for complete integration with the V5 robotics platform.

Angled view of the V5 Optical Sensor.


Description of the Sensor

The Optical Sensor is a combination of the following sensors:

  • Ambient light sensor
  • Color sensor
  • Proximity sensor

Color information is available as RGB (Red, Green, Blue), hue and saturation, or grayscale. Color detection works best when the object is closer than 100 millimeters (mm).

The proximity sensor measures reflected IR (infrared) energy sourced from an integrated IR LED. As such, the values will change with ambient light and object reflectivity.

The Optical Sensor has a white LED to assist color detection in low light conditions.

V5 Optical Sensor is shown with the tabs on its sides and the slotted holes in the Tabs highlighted and labeled.

The housing of this sensor has two mounting tabs with slotted holes to provide flexibility when mounting the sensor to a robot.

V5 Optical Sensor is shown with the window for its optical sensors highlighted and labeled.

On the face of the sensor there is a small window where the optical sensors are located.

V5 Optical Sensor is nested and mounted into a V5 C Channel piece using two screws and two 1/4 inch Standoff pieces.

The width of the Optical Sensor allows it to be nested into a piece of C-Channel.

Note: a 1/4 inch Standoff (275 - 1013) or an 8 mm Plastic Spacer (276-2019) needs to be used for this attachment in order to provide clearance for the V5 Smart Port.

V5 Optical Sensor is shown connected to a V5 Brain using a Smart Cable.

In order for the Optical Sensor to be functional with the V5 Robot Brain, the sensor’s V5 Smart Port and a V5 Robot Brain’s Smart Port need to be connected with a V5 Smart Cable. The Optical Sensor will work with any of the 21 Smart Ports on the V5 Robot Brain. When connecting a V5 Smart Cable to the ports, be sure the cable’s connector is fully inserted into the port and the connector’s locking tab is fully engaged.


How the Optical Sensor Works

The Optical Sensor receives light energy and converts the energy into electrical signals. The sensor's internal electronics (a hardware state machine) converts these signals into output signals to be received as an input by the V5 Brain.

As previously mentioned:

  • The sensor’s color detection works best when the object is closer than 100 millimeters (mm) or approximately 3.9 inches.
  • The proximity sensor measures reflected lR light intensity. This will cause the values to change with ambient light and object reflectivity.

The Optical Sensor needs to be paired with a programming language such as the VEXcode V5 or VEXcode Pro V5 to create a user program for the V5 Robot Brain to utilize the sensor’s readings to control the robot’s behavior.

The V5 Brain in concert with a user program can be used with the Optical Sensor to:

  • Turn the sensor’s white LED light on or off.
  • Set the white LED light’s percentage of power.
  • Detect an object.
  • Detect a color.
  • Measure the percent brightness of ambient light.
  • Measure the hue of a color in degrees.

Setup of the Optical Sensor

Placement.

V5 Optical Sensor is shown placed on the front of a claw arm on a V5 robot build. A red ball is in front of the sensor with a dotted line pointing to it, indicating that the sensor can be used to find the color of objects.

The placement of the Optical Sensor is very important to obtaining accurate readings. Be sure no structure on the robot is in front of the small optical sensor window on the face of the sensor. There needs to be a clear path in front of the sensor between any object being measured and the sensor.

Reading Optical Sensor Values.

It is helpful to use the Device Info screen on the V5 Robot Brain to see the information the Optical Sensor is returning. This can be done with the sensor connected to the Brain with the following steps:

Brain screen is shown in the Home menu with the Devices option highlighted.

Remove the V5 Brain Magnetic Screen Protector, turn on the Brain, and touch the Devices icon.

Brain screen is shown in the Device Info menu which has a list of all of the Brain's Smart Ports and connected devices. The Optical Sensor icon in the Device Info menu is highlighted to indicate that items can be selected to open their information menus.

Touch the Optical Sensor icon on the Device Info screen.

Brain screen is shown in the Optical Sensor menu that lists the sensor's data. The menu is in Color mode, and it reports data of an object's brightness, distance, and hue. The menu also has a diagram that visualizes the hue as a position on a color wheel. In this screenshot, the object's brightness is 0.01, the distance is near, the hue is 97, and the Led Brightness is set to 50%.

Color mode. Place an object in front of the Optical Sensor. The display will show the brightness the sensor is detecting, the proximity of the object, and the number for the hue of the color of the object. Touching the LED brightness portion of the screen will change the brightness of the LED.

Note: Be sure the V5 Robot Brain has the latest version of firmware.


Adding the Optical Sensor as a Device in VEXcode V5

Whenever a sensor is used with a programming language, it needs to be configured within that language. With VEXcode V5 and VEXcode Pro V5, this is accomplished by using the ‘Add a device’ feature. To provide an example of this, the following steps will add the Optical Sensor as a device within VEXcode V5.

VEXcode V5 Toolbar with the Devices icon highlighted in between the Code Viewer and Print Console icons.

Select the Devices icon.

VEXcode V5 Devices menu with the Add a device button highlighted.

Select ‘Add a device.’

VEXcode V5 Devices menu after the Add a device button has been selected. The Optical option is highlighted.

Select ‘OPTICAL.’

VEXcode V5 Devices menu with the Smart Port menu shown. The first Smart Port is highlighted.

Select the Smart Port that the Optical Sensor is plugged into on the V5 Robot Brain.

VEXcode V5 Devices menu after setting the new Optical Sensor's Smart Port option to 1. Above, the device's name is highlighted to indicate that it can be changed. Below, the Done button is highlighted.

Once the port is chosen, select ‘DONE.’

Note: There is an option to change the name of the Optical Sensor in the text window from Optical1 to a different name.

VEXcode V5 with the blocks Toolbox shown. The Optical Sensing category of blocks is now available.

Once the Optical sensor has been added to your user program, a new set of sensor commands will become available.

For more information on the commands from the ‘Sensing’ category pertaining to the Optical Sensor, view the Help information (Blocks project or Python project).


Common Uses of the Optical Sensor

The Optical Sensor can produce several measurements which can be used to change the behavior of the robot.

These features can be used with either:

  • The ‘Device info’ option on the V5 Robot Brain.
  • Using the example project ‘Detecting Objects (Optical)’ found in the ‘Open Examples’ option in the File menu of VEXcode V5.
  • Writing a custom user program with one of the VEXcode programs.

VEXcode V5 Toolbar with the File menu open and the Open Examples option highlighted. Open Examples is the fifth option in the menu, below New Blocks Project, New Text Project, Open, and Open Recent. Beside the screenshot, the thumbnail for the Detecting Objects (Optical) example project is shown.

These features include:

  • Detect an object. This feature allows your robot to detect an object when it becomes within the range of the Optical Sensor. A fun classroom challenge would be to place objects with different reflectivity in front of the sensor, such as a block of black foam rubber or a shiny ball of aluminum foil, to see if the object’s surface has any effect on its detection.

V5 Optical Sensor is shown mounted to a claw arm on a V5 robot build. A series of different paint swatches are placed one by one in front of the sensor.

Detect a color. This feature allows your robot to identify the color of an object. A fun classroom activity would be to get some paint swatches from the hardware store in different colors (red, green, blue, yellow, orange, purple, cyan) and see if the shade of the color affects its detection.

  • Measure the percent brightness of ambient light. This feature allows your robot to measure the brightness of the light around it. A fun classroom activity is to use this feature to measure the amount of light in a room with the classroom lights on and the classroom lights off. Then, write a custom user program so when the lights in the room are turned on your robot drives in a circle and when the lights are turned off your robot stops moving.

Color wheel demonstrating how the 360 degree circle correlates to a hue value. The color red is at a degree of 0, the color green is at a degree of 120, and the color blue is at a degree of 240.

Measure the hue of a color in degrees. This feature allows your robot to provide a number for the hue of color of an object. The Optical Sensor reports the corresponding hue value in degrees from 0 to 359 based on the color wheel below. This provides a more precise measurement of color as compared to a generalized red, green, or blue designation. A fun classroom activity would be to measure the hue of different objects in the room and see which student can find the object with the highest hue number.


Uses of the Optical Sensor on a Competition Robot

The Optical Sensor will provide an excellent competitive advantage for competition robots. The ability to detect objects and their color will provide a great deal of information when designing autonomous routines.

An object’s presence and hue of color will provide helpful information with the detection of game pieces and or goals.

V5 Optical Sensor is shown mounted to a claw arm on a V5 robot build. A red ball is shown in front of the sensor, to indicate that the Optical Sensor could be used to detect a colored game object in a competition.

For example, in the 2020 - 2021 VEX Robotics Competition Game, Change Up, an Optical sensor could be angled to scan the floor in front of your robot's intake. As soon as one of your alliance's colored balls is detected, this could automatically turn on the robot's intake and pick up the ball.

No matter what the application the VEX Optical Sensor is used for, there is no doubt it will be a welcomed addition for teams. The function of the sensor’s values are open for the imagination of the user.

The Optical Sensor is available on VEX’s website.

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: