This repository contains Arduino code for an advanced line-following robot built with ESP32. The project includes two main sketches with PID control, Bluetooth communication, and adaptive line polarity detection.
200bothworked/
200bothworked.ino # Working version with motor speed 200
check/
new/
new.ino # Latest version with motor speed 180
- ESP32 Development Board - Main microcontroller
- 5 IR Sensor Array - For line detection (connected to pins 25, 26, 27, 14, 12)
- Dual TB6612FNG Motor Driver - For controlling two DC motors
- LED OLED Display - For status display (not implemented in current code)
- Two DC Motors - For robot movement
- Chassis and wheels
- Power supply (Li-Po battery recommended)
Motor A (Left):
- PWM: Pin 2
- Direction 1: Pin 4
- Direction 2: Pin 16
Motor B (Right):
- PWM: Pin 19
- Direction 1: Pin 18
- Direction 2: Pin 5
Motor Driver:
- Standby: Pin 17
IR Sensors:
- Sensor 0: Pin 25 (leftmost)
- Sensor 1: Pin 26
- Sensor 2: Pin 27 (center)
- Sensor 3: Pin 14
- Sensor 4: Pin 12 (rightmost)
- Proportional (Kp): 50.0 - Quick response to line position errors
- Integral (Ki): 0.0 - Disabled to prevent windup
- Derivative (Kd): 20.0 - Smooth steering and stability
- Real-time PID tuning via Bluetooth
- Automatic polarity detection - Works with both black lines on white surface and white lines on black surface
- Dynamic threshold adjustment - Adapts to different lighting conditions
- Robust error calculation - Weighted position calculation for precise steering
- Device name: ESP32_BT
- Real-time parameter tuning: Send PID values and motor speed via Bluetooth
- Format:
Kp Ki Kd MotorSpeed(e.g., "50.0 0.0 20.0 180") - Serial debugging: Real-time monitoring of sensor values and PID outputs
- Line loss detection - Robot rotates to find the line when no sensors detect it
- Intelligent search pattern - Rotates based on last known line position
- Smooth transitions - Between line following and search modes
- Mount the 5 IR sensors in a straight line at the front of the robot
- Connect the TB6612FNG motor driver to the ESP32 according to the pin configuration
- Wire the two DC motors to the motor driver outputs
- Connect power supply to both ESP32 and motor driver
- Ensure proper grounding between all components
- Install the Arduino IDE with ESP32 board support
- Install required libraries:
- ESP32 BluetoothSerial (included with ESP32 package)
- Open either
new/new.ino(recommended) or200bothworked/200bothworked.ino - Select "ESP32 Dev Module" as your board
- Upload the code to your ESP32
- Place the robot on your track
- Connect to "ESP32_BT" via Bluetooth
- Monitor the Serial output to observe sensor readings
- Adjust PID parameters via Bluetooth for optimal performance
- Test on different parts of your track
- Power on the robot
- Place it on the line
- The robot will automatically start following the line
- Monitor performance via Serial Monitor or Bluetooth connection
Send Bluetooth commands in the format: Kp Ki Kd MotorSpeed
Example tuning sequence:
- Start conservative:
30.0 0.0 10.0 150 - Increase response:
50.0 0.0 20.0 180 - Fine-tune for your track:
45.0 0.0 15.0 200
- Robot oscillates: Reduce Kp value
- Robot is too slow to respond: Increase Kp value
- Robot overshoots corners: Increase Kd value
- Robot can't find line: Check sensor connections and line contrast
The robot uses a weighted average calculation to determine line position:
- Position range: -2 to +2 (left to right)
- Error calculation accounts for line polarity
- Automatic switching between black and white line detection
- PWM frequency optimized for smooth motor operation
- Speed range: 0-255
- Bidirectional control with dynamic speed adjustment
- Constrained output to prevent motor damage
- OLED display integration for real-time status
- Speed optimization algorithms
- Advanced path prediction
- Wireless telemetry dashboard
- Multiple line detection modes
This project is open source. Feel free to use and modify it for your own line follower robot projects.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.