Building a 3-Sensor Line Follower Robot | Arduino + L298N | DIY

by Amit_innovates in Circuits > Arduino

46 Views, 0 Favorites, 0 Comments

Building a 3-Sensor Line Follower Robot | Arduino + L298N | DIY

cropped-White, Black, and Neon Green Edgy Fashion Social Feed Static Ad.png
IMG_20250525_203944.jpg
IMG_20250525_203937.jpg

πŸ“Œ Introduction

In this project, we’ll build a simple and efficient 3-sensor line follower robot using an Arduino, L298N motor driver, and IR sensors. Perfect for beginners and ideal for robotics competitions, this bot can follow a white path on a black surface and navigate turns using only three IR sensors.

🎯 Features

  1. Follows a white line on a black surface
  2. Takes sharp and soft turns
  3. Stops at junctions or line loss
  4. Simple logic using digital IR sensors

πŸ“‘ Check For Update: https://github.com/Amitm0v

Supplies

IMG_20250525_175830.jpg
IMG_20250525_180029.jpg
IMG_20250525_181256.jpg
IMG_20250525_180522.jpg
IMG_20250525_181527.jpg
IMG_20250525_180653.jpg

️ Step 1: Components Required

  1. Arduino UNO Amazon
  2. L298N Motor Driver Amazon
  3. 3 IR Line Sensors (Digital Output) Amazon
  4. 2 BO Motors + Wheels Amazon
  5. Chassis (custom or kit) Amazon
  6. Jumper wires Amazon
  7. Power supply (6V–12V battery pack)
  8. 18650 Battery (12v-18v) Amazon
  9. 3-4 Cell Holder Amazon
  10. Switch (optional)

Note:

  1. i m using Buck converter because my L298N doesn't output 5volts

πŸ”§ Wiring and Assembly

Amit_3ir_sensor_Line Follower.png
IMG_20250525_184240.jpg
IMG_20250525_190122.jpg

Attach the motors to the chassis.

IR Sensors:

  1. Left Sensor (LS): Digital output to pin 13 (D13)
  2. Middle Sensor (MS): Digital output to pin 12 (D12)
  3. Right Sensor (RS): Digital output to pin 11 (D11)

L298N Motor Driver:

  1. Left Motor: OUT1 & OUT2
  2. Right Motor: OUT3 & OUT4
  3. IN1 (LM1): Arduino D2
  4. IN2 (LM2): Arduino D3
  5. IN3 (RM1): Arduino D4
  6. IN4 (RM2): Arduino D5
  7. Enable Pins: Jumpered or connected to 5V

Power:

  1. L298N VCC: 12V battery pack
  2. GND: Common GND with Arduino
  3. Arduino: Powered by USB or Vin pin (from L298N 5V)


🧠 Code

Flashing .png
Code on Arduino IDE Cloud.png

Im using Arduino IDE cloud.

Upload the following code to your Arduino:

( Code for black line on a white surface you will find on my github)

Working Principle

Each IR sensor detects either black or white:

  1. White (no line): HIGH output
  2. Black (line): LOW output

The bot uses these sensor readings to decide the direction

|LS|MS|RS| Action |

|----|----|----|----------------------|

| 0 | 1 | 0 | Move Forward |

| 1 | 1 | 0 | Sharp Left |

| 0 | 1 | 1 | Sharp Right |

| 1 | 0 | 0 | Turn Left |

| 0 | 0 | 1 | Turn Right |

| 1 | 1 | 1 | Stop (junction) |

| 0 | 0 | 0 | Stop (lost) |

βš™οΈ Testing and Calibration

IMG_20250525_203944.jpg
  1. Place the robot on a white line track with a black background.
  2. Power it on and observe its motion.
  3. Adjust sensor height or placement if it misreads.
  4. Check serial output for debugging info.

πŸ’‘ Tips and Troubleshooting

  1. If the bot moves randomly, check IR sensor logic (HIGH/LOW on line).
  2. If motors don’t rotate, verify L298N enable pins.
  3. You can swap wires to fix wrong motor directions.
  4. Add an LED to indicate line detection for debugging.

Adding Speed Control

For speed control, connect ENA and ENB pins of L298N to Digital I/O pins of Arduino UNO marked with PWM (~). The only addition in software would be to declare those pins, set them up in OUTPUT pinMode in void set up, and add the following lines of code at the start of void loop:

analogWrite(ENA, <speed>);

analogWrite(ENB, <speed>);

Where the value speed can take is between 0-255, 0 being stop, and 255 being highest achievable speed.

πŸ“š Conclusion


This project introduces basic autonomous robot logic using just three sensors. It's ideal for beginners learning about embedded systems, sensor integration, and motor control.

Ready to take it to the next level? Try:

  1. Analog sensors for smoother line tracking
  2. PID control
  3. Maze-solving algorithms
  4. Use N20 Motors or stepper