Skip to content

Optocoupler-Based Tracking Sensor

Imagine you have a sensor that can detect whether a surface is bright or not or more precisely reflective or not. This sensor works using an optocoupler, which combines an infrared LED and a phototransistor.

Reflective vs. Non-Reflective Surfaces. What is the difference?

Why do some surfaces shine while others seem to absorb light? The key lies in how they interact with light—whether they reflect or scatter it.

What Makes a Surface Reflective?

A reflective surface bounces back most of the light that hits it. This happens when the surface is smooth and bright, allowing light rays to maintain their direction. Examples include: ✔️ Polished metal ✔️ Glass or mirrors ✔️ White or glossy paper

What Makes a Surface Non-Reflective?

A non-reflective surface absorbs or scatters light in multiple directions, making it appear dark. This happens when the surface is rough, matte, or dark-colored, preventing a clear reflection. Examples include: ❌ Black fabric ❌ Rubber or matte plastic ❌ Unpolished wood

Why Does It Matter?

Many sensors, such as optocouplers and infrared detectors, rely on reflection to detect objects. A bright, reflective surface bounces back infrared light, while a dark, non-reflective surface absorbs it, allowing for easy differentiation.

This principle is used in barcode scanners, optical sensors, and robotics, enabling machines to distinguish between materials and surfaces effectively.

The robot is equipped with four optocouplers, located on its underside, facing downward. These sensors are positioned to detect surface reflections beneath the robot.

Emitting Infrared Light

At the heart of this system is a small infrared LED inside the optocoupler. It constantly emits invisible infrared light, thanks to a 150 Ω resistor that limits the current to keep the LED operating safely.

Interesting Fact

The human eye cannot perceive infrared light, but a smartphone camera can! Try using your phone's camera to check if the optocouplers are emitting infrared radiation. You can even estimate the emission angle by observing the spread of light in the camera image.

Detecting Reflections with the Phototransistor

Right next to the LED, there's a phototransistor (see the picture below). It works like a small switch that responds to infrared light. Its behavior changes depending on whether the infrared light from the LED reflects off a surface and returns to it, or not.

TCRT5000 Optocoupler

To make it easier to see what's happening, a blue LED is connected to each comparator output on the Pico Robot:

  • LED ON → No reflection detected (sensor sees a dark surface).
  • LED OFF → Reflection detected (sensor sees a bright surface).

✅ Task 1 - Test the optocouplers

Evaluate the optocouplers under different conditions and observe how the blue LEDs behave. Pay attention to when they turn on or off in response to various surfaces and positions.

Test Scenarios:

  • Bright surface (e.g., white paper, reflective material) – Does the LED turn off as expected?
  • Dark surface (e.g., black fabric, matte object) – Does the LED turn on correctly?
  • Dark but shiny surface (e.g., glossy black plastic, polished metal) – Does the sensor react inconsistently or as expected?
  • Lifting the sensor away from any surface – Does it behave like a non-reflective surface (LED on)?
  • Other test cases – Try different materials, angles, or distances. Does anything cause unexpected behavior?

How the Optocoupler Detects Reflection

The sensor works by detecting whether infrared light is reflected back to the phototransistor or not.

  • If a reflective surface is present (e.g., white or shiny object):

    • The infrared light bounces back and reaches the phototransistor.
    • This activates the phototransistor, allowing current to flow.
    • As a result, the collector voltage drops to nearly 0V (ground).
    • The microcontroller reads this as a low logic level (0).
  • If no reflection occurs (e.g., dark or matte surface, or object too far away):

    • The infrared light does not return, leaving the phototransistor inactive.
    • Without incoming light, the transistor remains off, meaning no current flows.
    • A 10 kΩ pull-up resistor keeps the collector voltage high (same as the supply voltage).
    • The microcontroller reads this as a high logic level (1).

📄 Optocoupler Datasheet: TCRT5000 Datasheet


Microcontroller Integration

The outputs of the comparators are connected to the microcontroller's GPIO pins:

  • X1 → GP2
  • X2 → GP3
  • X3 → GP4
  • X4 → GP5

With this setup, the microcontroller can continuously monitor the sensor's signals and determine whether an object is present, what type of surface it has, or even track movement if the surface is changing.


✅ Task 2 - Test the optocouplers in Pico

Create a program that detects a dark line under the robot or the edge of a desk.

If a non-reflective surface is detected, turn on all the addressable LEDs in red. Otherwise, turn them off—or, if preferred, switch them to dimmed green instead of turning them off.

Below is an example program that turns on the green top LED on the Pico board when any sensor detects a non-reflective surface:

import machine

# Define onboard LED
LED = machine.Pin("LED", machine.Pin.OUT)

# Define individual tracking sensors
# Non-reflective surface = 0, Reflective surface = 1
tracking_ll = machine.Pin(2, machine.Pin.IN)  # Left sensor
tracking_lc = machine.Pin(3, machine.Pin.IN)  # Left-center sensor
tracking_rc = machine.Pin(4, machine.Pin.IN)  # Right-center sensor
tracking_rr = machine.Pin(5, machine.Pin.IN)  # Right sensor

while True:
    # Check if any sensor detects a non-reflective surface (0)
    if ( tracking_ll.value() == 0
        or tracking_lc.value() == 0
        or tracking_rc.value() == 0
        or tracking_rr.value() == 0 ):

        LED.on()  # Turn LED on if an object is detected
    else:
        LED.off()  # Turn LED off if no object is detected

How to control the addressable LEDs?

If you need a littel remionder how toe control the addressable LEDs just go back Lab1 - Addressable LEDs

Test the program by placing the sensors over a dark surface or near the edge of the desk to ensure proper detection!


➡ Next Steps