Skip to content

darkgrecher/line-follower

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

ESP32 Line Follower Robot

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.

Project Structure

200bothworked/
    200bothworked.ino    # Working version with motor speed 200
    check/
new/
    new.ino             # Latest version with motor speed 180

Hardware Requirements

Main Components

  • 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)

Pin Configuration

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)

Features

Advanced PID Control

  • 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

Adaptive Line Detection

  • 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

Bluetooth Control

  • 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

Smart Recovery

  • 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

Getting Started

1. Hardware Assembly

  1. Mount the 5 IR sensors in a straight line at the front of the robot
  2. Connect the TB6612FNG motor driver to the ESP32 according to the pin configuration
  3. Wire the two DC motors to the motor driver outputs
  4. Connect power supply to both ESP32 and motor driver
  5. Ensure proper grounding between all components

2. Software Setup

  1. Install the Arduino IDE with ESP32 board support
  2. Install required libraries:
    • ESP32 BluetoothSerial (included with ESP32 package)
  3. Open either new/new.ino (recommended) or 200bothworked/200bothworked.ino
  4. Select "ESP32 Dev Module" as your board
  5. Upload the code to your ESP32

3. Calibration

  1. Place the robot on your track
  2. Connect to "ESP32_BT" via Bluetooth
  3. Monitor the Serial output to observe sensor readings
  4. Adjust PID parameters via Bluetooth for optimal performance
  5. Test on different parts of your track

Usage

Basic Operation

  1. Power on the robot
  2. Place it on the line
  3. The robot will automatically start following the line
  4. Monitor performance via Serial Monitor or Bluetooth connection

PID Tuning

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

Troubleshooting

  • 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

Technical Details

Line Detection Algorithm

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

Motor Control

  • PWM frequency optimized for smooth motor operation
  • Speed range: 0-255
  • Bidirectional control with dynamic speed adjustment
  • Constrained output to prevent motor damage

Future Enhancements

  • OLED display integration for real-time status
  • Speed optimization algorithms
  • Advanced path prediction
  • Wireless telemetry dashboard
  • Multiple line detection modes

License

This project is open source. Feel free to use and modify it for your own line follower robot projects.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About

line following robot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages