Skip to content

Example 42: IR Tracking Sensor ​

Experiment Overview ​

An IR tracking sensor identifies black lines (low reflection) and white backgrounds (high reflection) by detecting ground reflectance differences. Suitable for line-following robots, path tracking, and similar applications.

Hardware Wiring ​

Board LabelGPIO PinConnected Device
OUT_LGPIO4Left tracking module DO
OUT_RGPIO15Right tracking module DO

Outputs HIGH over black lines (no reflection); outputs LOW over white ground (reflection detected).

JSON Configuration Example ​

json
{
  "peripherals": [
    {
      "id": "track_left",
      "name": "Tracking - Left",
      "type": 11,
      "enabled": false,
      "pins": [4],
      "params": {}
    },
    {
      "id": "track_right",
      "name": "Tracking - Right",
      "type": 11,
      "enabled": false,
      "pins": [15],
      "params": {}
    }
  ]
}

Peripheral Execution Linkage ​

Scenario: Line-Following Control (Timer Trigger + Script) ​

Feature: Control motor direction based on left/right sensor states to follow a black line

Web UI Configuration Steps ​

Step 1: Ensure Motor Peripherals are Configured

  • Motor IN1: motor_in1 (GPIO Output)
  • Motor IN2: motor_in2 (GPIO Output)

Step 2: Create Rule

  1. Click Peripheral Execution in the left menu
  2. Click New Rule button
  3. Fill in basic configuration:
    • Rule Name: Line-Following Control
    • Report Data: ✅ Enabled
    • Enable: ✅ Enabled

Step 3: Configure Trigger (Timer Trigger)

  1. Click Add Trigger button

  2. Fill in trigger configuration:

    FieldValueDescription
    Trigger TypeSelect Timer TriggerPeriodic detection
    Timer ModeSelect Fixed IntervalMillisecond interval
    Interval5050ms (fast response)

Step 4: Configure Action (Script Control)

  1. Click Add Action button

  2. Fill in:

    • Action Type: Select Command Script
    • Action Parameter:
    var l=gpioRead('track_left');
    var r=gpioRead('track_right');
    if(l==1&&r==0){
      gpioWrite('motor_in1',0);
      gpioWrite('motor_in2',1);
    } else if(l==0&&r==1){
      gpioWrite('motor_in1',1);
      gpioWrite('motor_in2',0);
    } else {
      gpioWrite('motor_in1',1);
      gpioWrite('motor_in2',1);
    }
  3. Click Save button

Line-Following Control Logic ​

Left SensorRight SensorStatusAction
1 (Black Line)0 (White Ground)Drift LeftTurn Right
0 (White Ground)1 (Black Line)Drift RightTurn Left
1 (Black Line)1 (Black Line)CenterGo Straight
0 (White Ground)0 (White Ground)Off LineStraight / Stop

Notes ​

  1. Mounting Height: 1-2cm from ground for best results
  2. Black Line Width: Recommended 2-3cm width, matching sensor spacing
  3. Ambient Light: Strong light may affect detection; add a light shield
  4. Multi-Sensor Expansion: Use 3-5 sensors for improved tracking accuracy

FastBee | fastbee.cn