Example 42: IR Tracking Sensor
About 1 min
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 Label | GPIO Pin | Connected Device |
|---|---|---|
| OUT_L | GPIO4 | Left tracking module DO |
| OUT_R | GPIO15 | Right tracking module DO |
Outputs HIGH over black lines (no reflection); outputs LOW over white ground (reflection detected).
JSON Configuration Example
{
"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
- Click Peripheral Config in the left menu → Switch to Peripheral Execution tab
- Click New Rule button
- Fill in basic configuration:
- Rule Name:
Line-Following Control - Report Data: ✅ Enabled
- Enable: ✅ Enabled
- Rule Name:
Step 3: Configure Trigger (Timer Trigger)
Click Add Trigger button
Fill in trigger configuration:
Field Value Description Trigger Type Select Timer Trigger Periodic detection Timer Mode Select Fixed Interval Millisecond interval Interval 5050ms (fast response)
Step 4: Configure Action (Script Control)
Click Add Action button
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); }Click Save button
Line-Following Control Logic
| Left Sensor | Right Sensor | Status | Action |
|---|---|---|---|
| 1 (Black Line) | 0 (White Ground) | Drift Left | Turn Right |
| 0 (White Ground) | 1 (Black Line) | Drift Right | Turn Left |
| 1 (Black Line) | 1 (Black Line) | Center | Go Straight |
| 0 (White Ground) | 0 (White Ground) | Off Line | Straight / Stop |
Notes
- Mounting Height: 1-2cm from ground for best results
- Black Line Width: Recommended 2-3cm width, matching sensor spacing
- Ambient Light: Strong light may affect detection; add a light shield
- Multi-Sensor Expansion: Use 3-5 sensors for improved tracking accuracy
