Spring 2021 · ECE 3724, Mississippi State University

Line Follower Robot, from 3D Model to PCB

A two-sensor line follower built around a Microchip dsPIC33EP128GP502 on a board I designed for ECE 3724. I modelled the chassis in SolidWorks, 3D printed it, and generated the motor PWM from timer interrupts.

  • dsPIC33 / PIC24
  • PCB design (KiCad)
  • Timer interrupts
  • L293D
  • SolidWorks
  • MPLAB X
Assembled line follower: black custom PCB with the dsPIC33 and L293D, two yellow gear motors, and two TCRT5000 sensor boards on a grey 3D-printed nose
The finished robot: custom PCB, 3D-printed chassis, two gear motors and two TCRT5000 sensor modules.
$44.86Total parts cost
1 kHzSoftware PWM
30 %Motor duty cycle
2IR line sensors

At a glance

MCU
Microchip dsPIC33EP128GP502 (28-pin DIP), from the course kit
Firmware
C in MPLAB X, using the pic24_all.h support library
Motor driver
L293D dual H-bridge, two DC gear motors on a 9 V supply
Sensors
Two TCRT5000 IR reflective modules, read as digital inputs
Board
Custom PCB designed in KiCad, with an LM2937 regulator, FTDI serial header, reset button and status LED
Chassis
Modelled in SolidWorks, 3D printed

How it steers

The two sensors sit side by side at the front, one on each side of the line. Each module outputs 3.3 V over white and 0 V over black, so every pass of the main loop is a two-bit lookup:

Sensor ASensor BAction
WhiteWhiteLine is between the sensors: drive forward
WhiteBlackTurn left
BlackWhiteTurn right
BlackBlackReverse (placeholder that still drives forward)

A turn stops one wheel and keeps the other one driving.

Motor PWM from two timers

The GP variant of the dsPIC33 has no motor-control PWM module, so I generated PWM in software. Each motor's L293D enable pin is toggled by its own timer interrupt: Timer2 for the left motor and Timer3 for the right. The ISR alternates the timer period between 300 µs (enable high) and 700 µs (enable low), which gives a 1 kHz PWM at 30 % duty and runs the motors from the 9 V supply at reduced speed.

line_follower.c (excerpt)
//create PWM for left motor
void createPWM_Left(void){
    //700+300 = 1000us = 1ms results in 1KHz PWM with 30% duty cycle.
    if (MOTOR_1_EN == 1) {
        PR2 = usToU16Ticks(700, getTimerPrescale(T2CONbits)) - 1;
        MOTOR_1_EN = 0;
    } else {
        PR2 = usToU16Ticks(300, getTimerPrescale(T2CONbits)) - 1;
        MOTOR_1_EN = 1;
    }
}

// Interrupt Service Routine for Timer2 which is for LEFT motor
void _ISR _T2Interrupt(void) {
   createPWM_Left();
  _T2IF = 0;
}

The full source, including the sensor and timer setup, is in the original code download.

Board and chassis

The board carries the regulator, the dsPIC33, the L293D, an FTDI header for serial, a reset button and screw terminals for the motors. Select an image to enlarge it.

Schematic: power management, MCU and LED, FTDI-to-TTL connector, sensor headers and L293D motor driver
Schematic: power, MCU, FTDI-to-TTL connector, sensors and motor driver.
3D render of the PCB from above
3D render, top
3D render of the PCB at an angle
3D render, angled
PCB copper layout
Copper layout
Bare black PCB with the Designed by Ajaya Dahal for ECE 3724 silkscreen
Bare PCB
Assembled PCB with the dsPIC33, L293D, regulator, capacitors and screw terminals
Assembled PCB
SolidWorks model of the robot with the board, gear motors and wheels
SolidWorks model of the chassis, board and wheels.

Bill of materials

PartNotesCost
Custom PCBDesigned for this project$15.90
Gear motorsDC gear motors$11.99
L293D motor driverDual H-bridge$7.99
TCRT5000 IR sensorsReflective sensor modules$8.98
dsPIC33EP128GP502 and support partsFrom the course kit$0.00
Chassis3D printed$0.00
Total$44.86

Demo

Final project video. The YouTube player loads only when you press play.

What I would change

  • Read the sensors as analog values instead of thresholded bits.
  • Replace the if/else steering with a proportional (then PI) controller that sets each motor's duty cycle from the left/right sensor difference.
  • Move from two sensors to a five-sensor IR array.

Files and links