Skip to content

Color Sensor

The most important sensor in FLL, period. Line following, line detection and mat-mark recognition all depend on it.

What it measures

ModeReadingNotes
Colorblack, white, red, yellow, green, blue, azure, magenta/violet, no colorDiscrete color recognition
Reflected Light0-100%Sensor shines light and measures the reflection. Line following uses this
Ambient Light0-100%Passive brightness, rarely used in FLL

Mounting

  • Facing down, about 16 mm above the mat (the official optimal reading distance, roughly 5 LEGO plates). Too high = noisy readings, too low = scraping.
  • Mount at the front of the chassis, ahead of the wheel axle - the further forward, the earlier the correction.
  • For two-sensor line following, mount them side by side about one line-width apart.
  • Shield it: surround the sensor with LEGO pieces to block ambient light. Venue spotlights are brutal.

Word Blocks

<color sensor [C] color = [black]>          ← Sensors category condition
(color sensor [C] reflected light)           ← round reporter
wait until <color sensor [C] color = [black]>

Example 1: drive to the black line

when program starts
set movement motors [A+E]
start moving [forward]
wait until <color sensor [C] reflected light < [30]>
stop moving

Example 2: count lines

set [lines] to [0]
start moving [forward]
repeat until <(lines) = [3]>
  wait until <color sensor [C] reflected light < [30]>
  change [lines] by [1]
  wait until <color sensor [C] reflected light > [60]>    ← key: wait to EXIT the line, or one line counts many times
stop moving

Python

python
import runloop, color_sensor, color
from hub import port

async def main():
    # read color (returns constants like color.BLACK)
    if color_sensor.color(port.C) == color.BLACK:
        pass
    # read reflected light 0-100
    value = color_sensor.reflection(port.C)

runloop.run(main())

Threshold calibration: not optional

The 30 in "reflected light < 30 means black" cannot be copied - measure it on your mat under your lighting:

  1. Write a loop that shows reflected light on the matrix: forever → write (reflected light).
  2. Read a value on the black line (say 12) and one on white mat (say 85).
  3. Threshold = middle: (12 + 85) / 2 ≈ 48. Black is < 48, white is > 48.

Recalibrate at every new venue

Lighting, mat wear and sensor height all shift readings. First thing on competition day: re-measure black and white. Store thresholds in variables so there's one place to edit.

Common pitfalls

  • Color mode is unreliable at speed: readings flicker when moving fast. Line following always uses reflected light.
  • Colored artwork on the mat fools "find the black line": dark blue and dark green reflect little light too. Combine with position (drive a known distance before arming line detection).
  • Two sensors reading differently is normal: calibrate each separately.

Applications continue in Line Following. Next sensor: Distance Sensor.