Interactive simulatorBeginner

Line Follower Simulator: Tune a Robot in Your Browser

Build a virtual line-following robot, visualize its sensors, and tune a PID controller with an interactive browser-based robotics simulator.

Category
Arduino
Time
45–90 min
Platform
Browser · Arduino
Line Follower Simulator: Tune a Robot in Your Browser technical schematicSENSOR ERROR

01 / Start here

Introduction

A line-following robot turns sensor readings into steering corrections. This project lets you draw a track, change the robot and sensor geometry, then watch a deterministic PID controller respond in real time. Use it to connect control theory with the behavior you would see from an Arduino robot on a physical course.

Live lab / Deterministic 60 Hz model

Line follower simulator

Run the starter track, tune the controller, or pause and draw a course of your own.

Browser native
A top-down line-follower robot simulator. Use the controls below to run the simulation or read the complete algorithm and theory after this lab.

Bright nodes are sensors currently detecting the track. The lavender heading line shows steering output. Drawing pauses the robot automatically.

Simulation

Paused. Press Start or Step to begin.

Robot geometry
PID calibration
Elapsed
0.0 s
Distance
0 units
Line error
0.000
Steering
0.000
Track contact
100%

Keyboard: focus the course, then use Space to run/pause, N to step, R to reset, D to draw, and F for full screen.

Controls

Use the track presets to establish a baseline, or pause the run and draw directly on the course. Robot speed controls how far the chassis advances per update. Sensor count and spacing change the field of view. The P, I, and D controls change steering response; Step advances one deterministic update while paused.

Start with a moderate speed, five sensors, and the default gains. Change one variable at a time and compare the error, lap time, and time-on-track metrics.

Theory

The sensor bar samples reflectance across the front of the robot. Each active sensor contributes its signed position to a weighted average. The center of the bar is error zero; readings left or right of center produce negative or positive error.

Bar chart of five reflectance sensors across the robot bar. The line sits right of center, so the right-hand sensors read strongest. A dashed line marks the bar center (error zero) and a solid line marks the reflectance-weighted position at plus 0.69, the computed steering error.
The weighted average of the sensor readings gives a smooth line position: here the line sits right of center, so the error is positive and the controller steers back toward zero. Download SVG

The controller combines three terms:

$$u(t) = K_p e(t) + K_i \int e(t)dt + K_d \frac{de(t)}{dt}$$

The correction $u(t)$ speeds one wheel and slows the other. Proportional control reacts to the current offset, integral control corrects persistent bias, and derivative control damps rapid changes.

Algorithm

  1. Sample every sensor at a fixed interval.
  2. Compute a weighted line position and normalize it around the center.
  3. Reuse the last observed line direction when every sensor loses the track.
  4. Update integral and derivative terms using the same fixed time step.
  5. Clamp the correction, calculate left and right motor commands, and integrate the robot pose.

Tune proportional gain until the robot follows gentle turns, add derivative gain until oscillation settles, and introduce only enough integral gain to remove a repeatable offset.

Source code

The browser simulator and a microcontroller use the same control shape. A compact Arduino-style loop looks like this:

float integral = 0.0f;
float previousError = 0.0f;

void controlStep(float dt) {
  float error = readWeightedLinePosition();
  integral = constrain(integral + error * dt, -2.0f, 2.0f);
  float derivative = (error - previousError) / dt;
  float correction = kp * error + ki * integral + kd * derivative;

  setMotorSpeeds(baseSpeed - correction, baseSpeed + correction);
  previousError = error;
}

Keep the real control interval stable. A variable delay changes the derivative and integral terms even when the gains stay the same.

Circuit diagram

Connect the sensor outputs to analog-capable inputs, share ground between every module, and drive motors through an H-bridge rather than from controller pins.

Block diagram of the line-follower wiring. An IR sensor array feeds analog inputs on an Arduino, the Arduino sends PWM and direction signals to a motor driver, and the motor driver powers the motors. A separate battery and regulator supplies the Arduino.
Signal flows left to right; motor current comes from a separate regulated supply, never through the controller's logic regulator. Download SVG

Verify the motor supply rating and add local decoupling near the controller and driver. Never route motor current through a development board’s logic regulator.

Hardware checklist

Components

  • Arduino Uno or compatible controller
  • Three to nine IR reflectance sensors
  • Two geared DC motors and wheels
  • Dual H-bridge motor driver
  • Battery pack, chassis, and caster

Continue building

Download resources

Use these on-page references while working through the project. Downloadable project bundles will be added only after their source and version are published.

Common questions

Frequently asked questions

Why does the robot oscillate around the line?

Oscillation usually means proportional gain is too high, derivative damping is too low, or the robot is moving faster than its sensor update rate can support. Reduce speed first, then tune P and D. On a physical robot, a long delay() in the loop is a frequent hidden cause: the dead time it adds makes the derivative term ineffective no matter how high you set it.

How many sensors should a line follower use?

Five sensors are a useful starting point—they reveal the direction and magnitude of the error while keeping the weighted-position calculation easy to inspect. Three sensors work at slow speeds, while competition robots often use eight for finer position resolution at high speed.

Why does my line follower lose the line on sharp turns?

The chassis is moving faster than the sensor bar can resolve the curve, so the line leaves the array before the controller reacts. Reduce speed, widen the sensor bar, or increase the sensor update rate. A good controller also reuses the last known line direction when every sensor loses the track, so it turns back toward the line instead of driving straight off.

Should I use analog or digital IR sensors for line following?

Analog reflectance sensors report a continuous value, which lets you compute a smooth weighted line position and steer proportionally. Digital sensors only report on or off after an internal threshold, which is simpler but gives coarser position information. This simulator models the analog, weighted-position approach.

How do I calibrate the IR sensor array?

Before a run, sweep the sensors across both the line and the background and record each sensor's minimum and maximum reading, then normalize live readings against that range. Calibration matters because sensor height, surface reflectivity, and ambient light change the raw values, and an uncalibrated array biases the weighted position.

What are good starting PID values for an Arduino line follower?

Treat published gains as a starting point, not a copy-paste answer: motor response, sensor height, battery voltage, surface reflectivity, and loop timing all change the gains a physical robot needs. Begin with proportional only, raise it until the robot tracks gentle curves with slight wobble, then add derivative to remove the wobble and a very small integral only if a repeatable offset remains.

Further reading

References

Authoritative sources for going deeper than this simulator's bounded educational model.