DIY High Performance Line Follower Robot — ESP32 + PID.

by Abhishek Rayareddi in Circuits > Robots

22 Views, 0 Favorites, 0 Comments

DIY High Performance Line Follower Robot — ESP32 + PID.

Screenshot 2026-06-10 145909.png
botpic.jpeg
maze and bot.jpeg

"This bot is a competition-grade line follower robot built from scratch using an ESP32 microcontroller, TB6612FNG dual motor driver, N20 600RPM gear motors, TCRT 5000L 5 Channel Infrared Sensor, LM2596 DC-DC Buck Converter and Wheels for the n20 motors. It uses a PID (Proportional-Integral-Derivative) control algorithm to follow a black line on a white surface with smooth, high-speed precision. Unlike basic line followers that use simple if-else logic and produce jerky movement, this robot continuously calculates a precise correction value every millisecond — enabling fast, fluid performance on curves and straight sections alike. The robot features real-time wireless PID tuning via Bluetooth from a smartphone app, allowing Kp, Kd, and base speed to be adjusted without stopping the bot or reflashing the firmware. It also includes automatic lost-line recovery, junction detection, and manual RC mode — all controlled from a single Bluetooth terminal app on your phone. Powered by an 11.1V 3S LiPo battery with dual buck converters for clean regulated power, this bot is designed for reliability and performance in robotics competitions."

Supplies

Hardware Components:

- ESP32 DevKit V1 x1

- TB6612FNG Motor Driver x1

- N20 600RPM Gear Motor x2

- TCRT 5000L 5 Channel Infrared Sensor x1

- 11.1V 3S LiPo Battery x1

- Buck Converter (set to 5V) x1

- Buck Converter (set to 6V) x1

- Toggle Switch x1

- N20 Wheels x2

- Ball Caster x1

- Robot Chassis (acrylic) x1

- Jumper Wires x40

- Breadboard x1


Software Required:

- Arduino IDE (free - arduino.cc)

- Serial Bluetooth Terminal app (free - Play Store)


Tools Required:

- Screwdriver

- Wire stripper

- Multimeter (for setting buck converters)

- Soldering iron (optional)


*Chassi design is given below can download and get it printed.

Circuit Connections and Wiring

WhatsApp Image 2026-06-22 at 4.18.58 PM.jpeg
WhatsApp Image 2026-06-22 at 4.18.58 PM (1).jpeg

In this step we will connect all the

components together.


POWER SYSTEM:

- Battery (+) → Toggle Switch →

Both Buck Converter inputs

- Battery (−) → Common GND

- Buck #1 output (5V) → ESP32 VIN

- Buck #2 output (6V) → TB6612 VM pin


ESP32 → TB6612FNG:

- GPIO25 → PWMA (left motor speed)

- GPIO26 → AIN1 (left motor direction 1)

- GPIO27 → AIN2 (left motor direction 2)

- GPIO32 → PWMB (right motor speed)

- GPIO33 → BIN1 (right motor direction 1)

- GPIO14 → BIN2 (right motor direction 2)

- GPIO13 → STBY (must be HIGH)

- 3.3V → TB6612 VCC

- GND → TB6612 GND (all 3 pins)


TB6612 → Motors:

- AO1, AO2 → Left N20 motor

- BO1, BO2 → Right N20 motor


ESP32 → TCRT5000L IR Array:

- 3.3V → IR Array VCC

- GND → IR Array GND

- GPIO34 → S1 (far left sensor)

- GPIO35 → S2 (left sensor)

- GPIO32 → S3 (center sensor)

- GPIO33 → S4 (right sensor)

- GPIO25 → S5 (far right sensor)


IMPORTANT RULES:

1. All GND connections must join at one

common GND rail — battery GND, both

buck GNDs, TB6612 GND, ESP32 GND,

IR array GND all connected together

2. Never connect 5V to ESP32 GPIO pins —

maximum is 3.3V or it will damage ESP32

3. TB6612 has 3 GND pins — connect all 3

4. STBY pin must always be HIGH otherwise

motors will not run

Setting Up the IR Sensor Array

The TCRT5000L 5-channel IR sensor array

is the most important part of the robot.

Proper setup ensures clean and reliable

line detection.


SENSOR HEIGHT:

Mount the sensor array at exactly 4mm

above the ground surface.

- Too close (less than 2mm) → IR light

over-saturates, cannot distinguish

black from white

- Perfect (3mm to 6mm) → clean reliable

detection

- Too far (more than 12mm) → IR light

too weak, unreliable readings


TIP: Stack 4-5 playing cards under the

sensor while mounting. Each card is

approximately 0.8mm thick. This gives

you the perfect 4mm gap. Tighten the

screws then remove the cards.


SENSOR POLARITY:

- Over WHITE surface → sensor reads HIGH

→ analog value around 4095

- Over BLACK line → sensor reads LOW →

analog value around 500-700


TESTING THE SENSORS:

Upload this test code to verify all 5

sensors are working correctly:


void setup() {

Serial.begin(115200);

pinMode(34, INPUT);

pinMode(35, INPUT);

pinMode(32, INPUT);

pinMode(33, INPUT);

pinMode(25, INPUT);

}


void loop() {

Serial.print(analogRead(34));

Serial.print(" | ");

Serial.print(analogRead(35));

Serial.print(" | ");

Serial.print(analogRead(32));

Serial.print(" | ");

Serial.print(analogRead(33));

Serial.print(" | ");

Serial.println(analogRead(25));

delay(200);

}


Open Serial Monitor at 115200 baud.

Expected readings:

- White surface → all values 2000-4095

- Black line → sensor over line shows

500-700, others remain high


THRESHOLD VALUE:

Calculate your threshold like this:

White reading = 4095

Black reading = 600

Threshold = (4095 + 600) / 2 = 2347

Round to 2400


Any reading BELOW 2400 = black line

Any reading ABOVE 2400 = white surface


HOW THE INDICATOR LEDS WORK:

Each sensor has a small indicator LED:

- LED ON → sensor sees WHITE surface

- LED OFF → sensor sees BLACK line

Use these LEDs to visually verify each

sensor is detecting correctly before

running the full PID code.


Link of the ONshape BOT design for understanding the design and mounting of the components.

Understanding PID Control

PID stands for Proportional Integral

Derivative. It is the algorithm that

makes the robot follow the line smoothly.


Think of it like driving a car:

- P (Proportional) → how far you are

from the line. Far away = steer hard.

Close = steer gently.

- D (Derivative) → how fast you are

moving back to the line. Moving fast =

ease off the steering to avoid

overshooting.

- I (Integral) → if robot keeps drifting

to one side, this slowly corrects it.


For beginners start with:

Kp = 25

Ki = 0

Kd = 15


The final correction steers the motors:

Left motor = Base Speed + Correction

Right motor = Base Speed - Correction

Uploading the Code

1. Download and install Arduino IDE from

arduino.cc


2. Install ESP32 board package:

- Open Arduino IDE

- Go to File → Preferences

- Add this URL in Additional Board

Manager URLs:

https://raw.githubusercontent.com/

espressif/arduino-esp32/gh-pages/

package_esp32_index.json

- Go to Tools → Board → Board Manager

- Search ESP32 and install


3. Select your board:

- Tools → Board → ESP32 Dev Module

- Tools → Port → select your COM port


4. Copy the full code provided in the

next step and paste into Arduino IDE


5. Click Upload button (→ arrow)


6. Wait for "Done uploading" message


7. Press EN button on ESP32 to restart

The Full Code

Copy and paste this complete code into

Arduino IDE and upload to your ESP32.


Key settings to note at the top:

- Kp = 35.0 (proportional gain)

- Kd = 20.0 (derivative gain)

- baseSpeed = 150 (motor speed 0-255)

- THRESHOLD = 2400 (black/white cutoff)


*The Full Code Is Given Below:


After uploading:

- Open Serial Monitor at 115200 baud

- You should see "Line Follower Started"

- Place bot on line and power on

PID Tuning Guide

Follow these steps in order:


STEP 1 - Set starting values:

Send via Bluetooth:

P20 → D0 → S120


STEP 2 - Tune Kp:

Increase Kp by 5 each run until robot

follows line but starts zigzagging.

Then reduce by 20%.

Example: P25 → P30 → P35 → back to P28


STEP 3 - Tune Kd:

Add Kd starting at half of Kp value.

This removes the zigzag.

Example: D15 → D20


STEP 4 - Increase speed:

Once stable increase speed gradually.

Example: S130 → S150 → S180


STEP 5 - Fine tune:

If robot drifts on straight sections

add tiny Ki: I0.001


TROUBLESHOOTING:

- Zigzagging → reduce Kp or increase Kd

- Loses line on curves → reduce speed

- One motor faster → send L0.9 or R0.9

- Motors wrong direction → send G1 or H1

Final Assembly and Testing

ASSEMBLY CHECKLIST:

1. Mount motors in brackets on chassis

2. Attach wheels to motor shafts

3. Mount ball caster at front center

4. Mount IR array at front, 4mm from ground

5. Mount ESP32 and TB6612 on chassis

6. Mount buck converters and battery

7. Connect all wires as per Step 2

8. Secure all wires away from wheels


BEFORE FIRST RUN:

1. Check all GND connections

2. Verify 5V and 6V from buck converters

3. Test IR sensors with test code

4. Test motors spin correct direction

5. Connect Bluetooth from phone


FIRST RUN:

1. Place robot on black line

2. Power on - robot waits 3 seconds

3. Robot starts following the line

4. Open Bluetooth app and tune PID


COMPETITION TIPS:

- Always recalibrate sensors under

competition lighting

- Start with lower speed, increase

after stable

- Keep spare jumper wires handy

- Charge battery fully before each run

Results and Conclusion

After tuning your robot should smoothly

follow the black line at high speed

through curves and straight sections.


WHAT WE BUILT:

A competition grade line follower robot

using PID control with wireless Bluetooth

tuning — built completely from scratch.


KEY LEARNINGS:

- PID gives much smoother movement than

basic if-else logic

- Sensor height of 4mm is critical for

reliable detection

- Bluetooth tuning saves huge amount of

time during PID tuning

- Common GND connection is the most

important wiring rule


POSSIBLE IMPROVEMENTS:

- Add encoder feedback for precise speed

control

- Add WiFi web interface for graphing

PID values

- Upgrade to custom PCB instead of

breadboard

- Add speed reduction on sharp curves


Thank you for reading! If this helped

you please vote for this Instructable

and leave a comment below.

Good luck in your competition!

Downloads