Projects

Line-Following Robot

A two-wheeled robot with closed-loop PD control and live parameter tuning.

Boston, MA Principles of Integrated Engineering

The robot running the non-circular loop course.

A two-wheeled robot that follows a taped track on the floor. An Arduino runs a closed-loop PD controller on five IR reflectance sensors and drives the wheels through a motor shield, while a Python GUI tunes the gains over serial as the robot runs. The best lap was 30 s, with 15 of 15 successful runs.

30 s
best lap time
15/15
successful attempts (100%)
5
TCRT5000 IR reflectance sensors
10 ms
control loop time (~100 Hz)

Overview

The course was a non-circular loop with corners tighter than 90 degrees. One requirement was tuning the controller over serial while the robot ran, without recompiling, so we could push for the fastest lap.

The assembled line-following robot: an Arduino and motor shield on a breadboard chassis with two yellow wheels and a front caster, wired to five IR sensors
The assembled robot, with the Arduino, motor shield and solderless breadboard on the custom chassis.

We designed our own chassis so we could set the wheel-to-wheel distance, sensor placement and sensor height ourselves. The controller is proportional-derivative (PD) and reads five infrared sensors:

The controller keeps the robot centered using the three main sensors and only reads the outer pair when the main sensors lose the line. That split let the robot run fast on straights and still take sharp corners reliably.

System architecture diagram: laptop exchanges serial parameters and sensor data with the Arduino Uno and Motor Shield v2.3; a 12 V supply powers the Arduino and motors; five IR reflectance sensors feed analog voltages to the Arduino; PWM drives the DC motors, which move the robot over the tape line
System architecture, with data flow in blue and power flow in red between the laptop, Arduino, sensors, motors and the environment.

Design

Chassis

The motors and wheels sit at the back, which puts the sensors on a longer arm for turning and keeps the weight over the drive wheels for traction. The chassis is 5 inches wide by 6.5 inches long and rests on three points:

CAD render of the blue chassis with four numbered callouts: motor nut clearance hole, Arduino mounting hole, sloped caster wheel cut-out and reinforced motor mounts
The chassis CAD with its four design features numbered.

The numbered features in the CAD render:

  1. Motor nut clearance: a hole that leaves room for the nut on the screw holding the motor.
  2. Arduino mounting: a hole for screwing the Arduino and motor shield to the chassis.
  3. Caster wheel slope: a sloped cut-out so the caster clears the chassis. Only the front needs it since the robot drives forward.
  4. Reinforced motor mounts: thick mounts chamfered into the base.

Every dimension came from measuring the physical parts with a caliper, with tolerances added for assembly.

Sensor array

The first version had three sensors close together in the center. It failed on turns sharper than 90 degrees because none of the sensors could see the line in the corner. We added one sensor on each side, set slightly further back, to catch the line when the center sensors lost it.

The three center sensors are 8 mm apart, slightly wider than the tape. That made the code simpler: when the robot is centered, the middle sensor sees full tape and the two beside it see only the edges.

CAD render of the curved sensor mount with three center sensor slots circled in the middle and one outer sensor slot circled at each end
The sensor mount, with three center slots at 8 mm spacing and one outer slot on each side set further back.

Electronics and control

Components

Wiring schematic: 12 V power supply into the Adafruit Motor Shield driving two TT motors, Arduino Uno pins A0 to A4 reading five IR sensor circuits, each with a 200 Ω LED resistor and 10 kΩ phototransistor pull-up
Wiring diagram for the power, the motor shield and the five IR sensor circuits.

Sensor circuit

Each IR reflectance sensor includes:

PD control

The firmware is a finite state machine (FSM) with two states:

In the PD state the controller computes an error from the left and right readings and adds a proportional and a derivative correction to the base speed:

PD controller · pseudocode
ERROR = (LEFT_READING - RIGHT_READING)
CORRECTION = Kp * ERROR + Kd * (ERROR - LAST_ERROR)
LEFT_SPEED = BASE_SPEED + CORRECTION
RIGHT_SPEED = BASE_SPEED - CORRECTION

Tuned parameters

ParameterValueRole
Kp (proportional gain)10Provides immediate response to error
Kd (derivative gain)15Provides damping to reduce oscillation
Base speed50On the 0–255 PWM range

Software

Dark-themed Python Robot GUI window with a live plot of five normalized IR sensor readings and sliders for kP, kD, left and right base speed and left and right lost-line speed
The Python tuning GUI, with a live sensor plot and sliders for Kp, Kd and the motor speeds.

I wrote a Python GUI that talks to the Arduino over serial, so we could change parameters while the robot was running instead of recompiling. It has:

Tuning went from an upload per change to a slider drag per change, which is where most of the lap-time improvement came from.

Three stacked plots over a 10 second run: left sensor readings, right sensor readings and left and right motor PWM speeds, showing the speeds diverging at each corner
Logged sensor readings and motor speeds over a 10-second run.

Results

Sharp corners

Problem: Sharp corners (especially the "armpit" of the course) made the 3-sensor design lose the line.

Fix: Added 2 outer sensors and the FSM line-search state to detect and recover from line loss.

Prototype iteration

Problem: The first build used a solderable breadboard, so swapping components meant desoldering.

Fix: Switched to a solderless breadboard for prototyping and planned a PCB for the final version.

Tuning speed

Problem: Recompiling and re-uploading for every gain change was slow.

Fix: The Python serial GUI, described above.

Specifications

SpecificationValue
Dimensions5" W × 6.5" L chassis
Sensors5× TCRT5000 IR reflectance sensors
Sensor height5 mm above ground (set by testing)
Sensor spacing8 mm between center sensors
Motors2× DC gearmotors, 210 RPM, 48:1 ratio
Control frequency~100 Hz (10 ms loop time)
Power12 V DC supply

What I learned