Skip to content
Constell Shop
Line-following robot 3D

Project guides

Line-following robot

Two KY-033 sensors watch the floor: whichever way the black line drifts, the robot turns that way. Stick black electrical tape on white paper — that's your track.

Difficulty
Easy
Time
~120 min
Parts
12 parts
Step by step
4 steps
  • Using an infrared reflectance sensor
  • A simple control algorithm
  • Tuning a sensor (the potentiometer)

Parts

You'll also need

  • — A computer with the Arduino IDE (free from arduino.cc)
  • — A USB cable for the UNO (the UNO SMD “+ USB cable” option)
  • — A small Phillips screwdriver
  • — Black electrical tape and white paper or card

Step by step

  1. 01

    Build the car

    Chassis, motors, L298N and power — as in the Robot car guide.

  2. 02

    Mount the sensors

    Fix both KY-033s at the front facing down, 5–10 mm above the floor and a little wider apart than the line (2–3 cm).

  3. 03

    Wire and tune

    Left sensor OUT → D12, right OUT → D11, VCC → 5V, GND → GND. Power up and turn each potentiometer until its LED is on over white and off over the black line.

  4. 04

    Upload and run the track

    Place the robot with the line between the sensors. If it overshoots the bends, lower SPEED.

Wiring

  • Power (+)
  • Ground (−)
  • Signal pin
  • I²C / SPI bus
L298N ⟷ UNO8 wires
  1. L298NENA→UNOD5 remove the jumper on ENA
  2. L298NIN1→UNOD7
  3. L298NIN2→UNOD8
  4. L298NIN3→UNOD2
  5. L298NIN4→UNOD4
  6. L298NENB→UNOD6 remove the jumper on ENB
  7. L298NGND→UNOGND common ground — required
  8. L298N5V→UNO5V powers the UNO from the driver
L298N ⟷ Left motor1 wire
  1. L298NOUT1OUT2→Left motor
L298N ⟷ Right motor1 wire
  1. L298NOUT3OUT4→Right motor
Battery ⟷ L298N2 wires
  1. Battery+(2×18650)→L298N12V
  2. Battery−→L298NGND
KY-033 ⟷ UNO4 wires
  1. KY-033leftOUT→UNOD12
  2. KY-033rightOUT→UNOD11
  3. KY-033VCC→UNO5V
  4. KY-033GND→UNOGND

The code

sketch.ino
// motors (L298N)
const int ENA = 5, IN1 = 7, IN2 = 8;   // left
const int ENB = 6, IN3 = 2, IN4 = 4;   // right

void motors(int left, int right) {     // -255 … 255
  digitalWrite(IN1, left >= 0);  digitalWrite(IN2, left < 0);
  digitalWrite(IN3, right >= 0); digitalWrite(IN4, right < 0);
  analogWrite(ENA, abs(left));   analogWrite(ENB, abs(right));
}

void setupMotors() {
  int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4};
  for (int p : pins) pinMode(p, OUTPUT);
  motors(0, 0);
}

const int LEFT = 12, RIGHT = 11;   // HIGH = over the black line
const int SPEED = 150, TURN = 170;

void setup() {
  setupMotors();
  pinMode(LEFT, INPUT); pinMode(RIGHT, INPUT);
  delay(1500);
}

void loop() {
  bool l = digitalRead(LEFT), r = digitalRead(RIGHT);
  if (!l && !r)      motors(SPEED, SPEED);    // line between the sensors
  else if (l && !r)  motors(-TURN / 2, TURN); // line under the left one: turn left
  else if (!l && r)  motors(TURN, -TURN / 2); // under the right one: turn right
  else               motors(0, 0);            // both black: a crossing or the end
}

If something doesn't work

  • It doesn't see the line: sensors too high — lower them to 5–10 mm and retune.
  • It turns the wrong way: swap the sensor wires (D12/D11).
  • It fails in sunlight: IR sensors dislike bright light — test indoors.

Where to go from here

  • Add a third sensor for smoother turns
  • Learn PID control — faster and more precise