Build a Low‑Cost Arduino‑Powered Line‑Following Robot in One Weekend
Read this article in clean Markdown format for LLMs and AI context.Ever dreamed of a tiny robot that can zip along a black line on a white floor, all for the price of a lunch? I built one in a Saturday afternoon, and you can too. In this post I’ll walk you through the parts, the wiring, and the code—nothing fancy, just the basics you need to get a line‑follower humming. Welcome to Robot Making, where we turn ideas into real, moving machines.
What You’ll Need (and Where to Save)
| Component | Why it matters | Approx. cost |
|---|---|---|
| Arduino Uno (or clone) | The brain of the robot | $8‑$12 |
| IR sensor array (2‑module) | Detects the line | $3‑$5 |
| Small DC gear motors (2) | Drive the wheels | $4‑$6 |
| Motor driver board (L298N) | Controls motor speed/direction | $2‑$4 |
| Chassis (plastic or 3D printed) | Holds everything together | $2‑$5 |
| Wheels (2) + caster wheel (1) | Moves the robot | $3‑$4 |
| Battery pack (4×AA or 9V) | Powers the robot | $2‑$3 |
| Misc: jumper wires, nuts, screws | Connects the dots | $2‑$3 |
Total: under $40. If you already have an Arduino or some spare motors, the price drops even more. All these parts are available on popular hobby sites or local electronics stores.
Step 1: Assemble the Chassis
- Lay out the base – If you bought a ready‑made plastic chassis, snap the motor mounts into place. If you printed your own, glue the motor brackets onto the bottom.
- Mount the motors – Secure the two DC gear motors on opposite sides of the chassis. Make sure the shafts face forward.
- Add the wheels – Push the wheels onto the motor shafts. Then attach the caster wheel at the front or back to keep the robot stable.
- Place the Arduino – Find a spot near the center so the wiring stays tidy. Use double‑sided tape or a small mounting hole.
That’s it—no tools beyond a small screwdriver. The whole frame should take about 10‑15 minutes.
Step 2: Wire the Motor Driver
The L298N board has two channels (A and B). Each channel controls one motor. Follow these simple connections:
- IN1, IN2 → Arduino pins 8 and 9 (Motor A)
- IN3, IN4 → Arduino pins 10 and 11 (Motor B)
- ENA, ENB → Arduino pins 5 and 6 (PWM speed control)
- 12V & GND → Battery pack (+) and (–)
- 5V → Arduino 5V (optional, but you can power the Arduino from the same pack if the voltage is safe)
Tie the ground of the Arduino to the ground of the motor driver—this common ground keeps the signals clean.
Step 3: Hook Up the IR Sensors
A basic line‑follower uses two infrared reflectance sensors placed side by side, about 2‑3 cm apart, near the front of the robot.
- VCC → Arduino 5V
- GND → Arduino GND
- OUT left → Arduino A0
- OUT right → Arduino A1
Mount the sensor board so the emitters face the floor. You’ll want the black line to be dark enough for the sensors to detect a difference in reflected light.
Step 4: Write the Sketch
Open the Arduino IDE and paste this minimal code. It’s deliberately simple—perfect for a weekend project.
// Robot Making line‑follower
const int leftSensor = A0;
const int rightSensor = A1;
const int in1 = 8, in2 = 9, enA = 5;
const int in3 = 10, in4 = 11, enB = 6;
const int baseSpeed = 150; // 0‑255 PWM
void setup() {
pinMode(leftSensor, INPUT);
pinMode(rightSensor, INPUT);
pinMode(in1, OUTPUT); pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT); pinMode(in4, OUTPUT);
pinMode(enA, OUTPUT); pinMode(enB, OUTPUT);
}
void loop() {
int leftVal = analogRead(leftSensor);
int rightVal = analogRead(rightSensor);
bool leftOn = leftVal < 500; // dark = line
bool rightOn = rightVal < 500;
if (leftOn && rightOn) { // centered
forward(baseSpeed);
} else if (leftOn && !rightOn) { // drift right
turnRight();
} else if (!leftOn && rightOn) { // drift left
turnLeft();
} else { // lost line
stopMoving();
}
}
void forward(int speed) {
digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
analogWrite(enA, speed);
analogWrite(enB, speed);
}
void turnLeft() {
digitalWrite(in1, LOW); digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH); digitalWrite(in4, LOW);
analogWrite(enA, baseSpeed);
analogWrite(enB, baseSpeed);
}
void turnRight() {
digitalWrite(in1, HIGH); digitalWrite(in2, LOW);
digitalWrite(in3, LOW); digitalWrite(in4, HIGH);
analogWrite(enA, baseSpeed);
analogWrite(enB, baseSpeed);
}
void stopMoving() {
digitalWrite(in1, LOW); digitalWrite(in2, LOW);
digitalWrite(in3, LOW); digitalWrite(in4, LOW);
}
Upload the sketch, place your robot on a sheet of white paper with a thick black line (tape works fine), and watch it go! If the robot wavers, adjust the threshold (the “500” value) or tweak the baseSpeed for smoother motion.
Step 5: Fine‑Tune and Test
- Sensor height – Raise or lower the IR board a millimeter at a time. Too close and the sensor saturates; too far and it can’t see the line.
- Wheel alignment – Make sure both wheels spin freely and are parallel. Misaligned wheels cause drift.
- Battery voltage – Fresh AA cells give about 6 V. If the robot feels sluggish, replace them or add a small boost regulator.
Spend 15‑20 minutes playing with these variables, and you’ll get a robot that follows curves reliably.
What’s Next? (Optional Upgrades)
Now that you have a working line‑follower, Robot Making suggests a few easy upgrades you can try over the next weekend:
- Add a third sensor in the middle for better edge detection.
- Use a Bluetooth module to start and stop the robot from your phone.
- Swap the Arduino for a Nano to make the robot lighter and more compact.
All of these upgrades keep the cost low while teaching you new skills. The key is to experiment, fail a little, and then succeed—exactly how we build robots at Robot Making.
Wrap‑Up
Building a line‑following robot doesn’t require a lab full of equipment or a massive budget. With a handful of inexpensive parts, a few minutes of wiring, and a short sketch, you can have a robot that tracks a line all weekend long. The joy of seeing your code translate into motion is what keeps me coming back to Robot Making—and I hope it sparks the same excitement for you.
Give it a try, share your results on the forum, and let me know what tweaks you made. Happy building!
- →
- →
- →
- →
- →