Skip to content

[14] Line follower module


White and black line detector for following some path on the floor. It reports line offset underneath the sensor which is used to steer a robot in the correct direction.


  • White/Black line position detection
  • Junction detection
  • Automatic line color detection
  • Automatic contrast calibration

Totem Module 14

Code examples

Arduino projects: 14_lineFollower

TotemModule14 module;
Function usage (click to expand)
/* Reading sensor data */
// Get line position "3"
int pos = module.getPos();
// Get line color 'W'
char color = module.getColor();
// Check if color is white "true"
bool isWhite = module.isWhite();
// Check if color is black "false"
bool isBlack = module.isBlack();
// Check if color (line) is detected "true"
bool isColor = module.isDetected();
// Check if junction is detected "false"
bool isJunction = module.isJunction();
// Get current detection accuracy "50"
int accuracy = module.getAccuracy();
// Set new detection threshold
// Get raw sensor reading
uint8_t *contrast = NULL;
// contrast contains array: {21,30,65,70,70,68,36,20}
int sensorA = contrast[0]; // 21
int sensorD = contrast[3]; // 70
int sensorH = contrast[7]; // 20
/* All LED control */
// Turn all LED on
// Turn all LED off;
// Set all LED on
// Toggle all LED on / off
// Is any of LED on "true"
bool isAllOn = module.led.isOn();
// Set LEDs : on|off|off|on|on|off|off|on
// Reset LEDs to default behavior
/* Single LED control */
// Turn LED A on
// Turn LED D off
// Toggle LED B on / off
// Set LED A off
// Check if LED A is on "false"
bool isAOn = module.led[0].isOn();


Reading sensor data

(position) module.getPos()

Line position below sensor. (negative, 0, positive) = (left, center, right).
position - line position [-35:35]. 0 - center

(color) module.getColor()

Get currently detected line color. This can be White or Black. Color is returned in ASCII character. Use 'W', 'B' expression.
color - line color (char) ['W' - white, 'B' - black]. 0 - not detected.


Returns 8 byte array of contrast reading from each sensor.
0% - light does not reflect (black), 100% - light does reflect (white).
Array index 0 corresponds to sensor A, 1 - B, ..., 7 - H.
Data is returned trough data array parameter.
data[8] - 8 byte array of each sensor contrast [0:100]%

(state) module.isWhite()

Check if white line is detected.
state - white line detected [true:false]

(state) module.isBlack()

Check if black line is detected.
state - black line detected [true:false]

(state) module.isDetected()

Check if line color is detected.
state - line detected [true:false]

(state) module.isJunction()

Check if sensor is currently detecting line junction (cross). A horizontal lines in its follow path. This is used for invoking some kind of action or to change direction (turn robot left or right)
NOTE: This will also return true if line is not detected at all.
state - junction detected [true:false]

(contrast) module.getAccuracy()

Percentage of current contrast difference between line and ground (maximum values).
Higher value means better detection accuracy. If value is lower than setThreshold(), sensor won't be able to work properly. In this case lower setThreshold() value or improve surface contrast.
contrast - contrast difference [0:100]%


Minimum contrast difference between line and ground required to start detection.
Lower value means more sensitivity when detecting line, but more susceptible to noise and misreadings. Should be changed only when there is an issue with detection on particular surface.
contrast - detection start contrast [10:80]%. Default: 20


Set line color to improve accuracy. This can be White or Black.
color - line color (char) ['W' - white, 'B' - black]. 0 - auto.

All LED control


Turn all LED on.

Turn all LED off.


Set all LED to specific state (on / off).
state - state on / off [HIGH:LOW] or [true:false]


Toggle all LED between on / off states.

(state) module.led.isOn()

Check if any of LED is on.
state - is any LED on [true:false] or [HIGH:LOW].


Set all LED state with single value mask.
Each bit in 8-bit value represents individual LED state. 1 - on, 0 - off.
state - binary mask [B00000000:B11111111].


Re-enable default behavior of LED. It is switched off when any of LED function is used.

Single LED control

Control specific LED by providing its number. 0 - A, 1 - B, ..., 7 - H.

Individual LED can be accessed by specifying index: module.rgb[0].on().
LED from A to H is identified as indexes 0-7.
Accessing "without index" will affect all LED.

Low level commands

These are low level TotemBUS commands accepted by module. Is not required when using objective API described above.

Command Parameters Description
sensor/threshold (int) Sensor threshold
sensor/range Returns:(int) Accuracy
sensor/raw Returns:(byte[8]) Raw each sensor contrast
line/junction Returns:(bool) Is junction detected
line/color Returns:(char) Detected line color
line/pos Returns:(int) Detected line position
led (byte) Write 8-bit mask of each LED on/off