How to Build a Line-Following Robot

An IR line sensor works by pairing an infrared LED with a phototransistor mounted right next to it: the LED shines infrared light down at the floor, and the phototransistor measures how much of that light bounces back. A white or light-colored surface reflects most of the infrared back up, so the phototransistor sees a strong signal; a black line absorbs most of it, so the reflected signal drops. That single difference in reflected light — strong over white, weak over black — is the entire physical mechanism a line-following robot uses to tell where the track is.

What parts does a line-following robot need?

We asked Compoden's storefront assistant to spec this exact build with the prompt "I want to build a basic line-following robot that follows a black line on the floor, using an Arduino Uno", and it resolved a complete seven-part tray. In its own words:

"This build creates a basic robot that follows a black line on the floor. It uses light sensors to detect the line, and a small computer to tell the motors how to move the robot to stay on the line."

The tray it built: an Arduino Uno R3 (ATmega328P) as the controller, a 4 Channel IR Sensor Tracking Module (TCRT5000) to read the line, an L298N Motor Driver Module because "DC motors, like the ones in this kit, cannot be connected directly to the Arduino because they draw too much current," two 95RPM BO Motor Single Side L Shape drive motors, a 2WD Robot Chassis Kit to hold everything, a 9V Battery Snap Connector for power, and 23 AWG hookup wire to connect it all. Every part above came directly from that live tray — nothing here is a generic recommendation.

How does the Arduino know which way to steer?

A line-following robot steers by comparing readings from more than one point across the track. With two sensors mounted side by side, the logic is direct: if the left sensor reports "line" and the right sensor reports "no line," the robot has drifted right of center and needs to correct left; if the right sensor sees the line and the left doesn't, it corrects right; and when both agree, it drives straight. When we asked Compoden's firmware generator (Soldr) to write this exact logic — "follow a black line by comparing the two sensors and steering to stay centered" — it described the result plainly: "The sketch will read two TCRT5000 IR line sensors on digital pins 2 and 7, and control two DC motors via an L298N driver on digital pins 3, 4, 5, and 6, following a black line on a white surface." Each loop, the Arduino reads both sensor pins, compares the two digital states, and drives the L298N's control pins to turn the motors accordingly.

One sensor vs a sensor array: how many do you actually need?

Be honest about what a given sensor count can actually tell you. A single IR sensor can only report one bit of information: line, or no line. It cannot tell you which direction to correct, only that you've drifted off the track entirely — by the time it fires, the robot is already lost. Two sensors, one on each side of the line, add a direction: left-drift versus right-drift, which is the minimum needed for real steering, and it's exactly what our firmware capture above wired up. The part the storefront actually selected for this build, though, is a step beyond that: the 4 Channel IR Sensor Tracking Module (TCRT5000) puts four emitter/receiver pairs on a single PCB, so instead of one bit of "left" or "right," you get four points of contact across the track width. That means the robot can distinguish a small drift from a sharp one, and has a better chance of holding a line through a shallower curve, without wiring up four separate discrete modules by hand. It's still a digital line/no-line reading at each of the four points, not a continuous position value — going further than that would mean the analog-output version of these sensors or a dedicated array board with onboard interpolation, which is a fair next upgrade once the basic four-point version is working.

How do you calibrate sensor sensitivity for different track colors/lighting?

Most IR line sensor modules, including the TCRT5000-based ones used here, carry a small onboard potentiometer next to the sensor. Turning it adjusts the threshold the module's onboard comparator uses to decide "this counts as black" versus "this counts as white." That threshold needs to move when your track or lighting changes: a duller black tape reflects a bit more light than a glossy one, and brighter ambient light raises the baseline reflectance the sensor reads even over the white parts of the track. Before a run, place the sensor over the line and over the surrounding surface in turn, and adjust the potentiometer until the module's onboard indicator LED (most boards have one) flips cleanly between the two states. Skipping this step is the most common reason a line follower works on one desk and loses the line on another.

Watch it built live

We typed the prompt "I want to build a basic line-following robot that follows a black line on the floor, using an Arduino Uno" into Compoden's storefront assistant and captured the tray it produced end to end.

Compoden storefront assistant building a line-following robot parts tray for an Arduino Uno

Beyond the parts list, the assistant also flagged a real coherence check on this exact tray: "the active parts in this build draw roughly 5090mA in total" and "the 9V battery will power the Arduino, but you will also need to provide a separate motor supply to the L298N Motor Driver Module because the motors require 6V and draw a lot of current, which the Arduino cannot supply on its own." That's a genuine wiring consideration for this build, not filler text.

Watch the firmware get generated

Separately, we asked Compoden's firmware tool (Soldr) to write the actual control code: "I'm building a line-following robot with an Arduino Uno, two IR line sensors, an L298N motor driver, and two DC motors. Write the full firmware to follow a black line by comparing the two sensors and steering to stay centered." It matched all seven parts against its catalog and generated working firmware in the FIRMWARE tab.

Compoden PWA generating Arduino firmware for a two-sensor line-following robot

The generated sketch defines the L298N control pins for both motors (enable and direction pins for Motor A and Motor B) and reads the two TCRT5000 sensors on their assigned digital pins, then drives the motors based on which sensor sees the line, exactly as described in the build log: "following a black line on a white surface."

Get everything in this build

These are the exact parts, prices, and quantities from the live tray captured above. Each row links straight to that item in your Compoden cart.

Part Role Qty Price Add to cart
Arduino Uno R3 CH340G ATmega328P Board Controller 1 Rs.230 Add to cart
4 Channel IR Sensor Tracking Module (TCRT5000) Line sensor 1 Rs.110 Add to cart
L298N Motor Driver Module Motor driver 1 Rs.180 Add to cart
95RPM BO Motor Single Side L Shape Drive motor 2 Rs.91 each Add to cart
2WD Robot Chassis Kit Chassis 1 Rs.280 Add to cart
9V Battery Snap Connector Power 1 Rs.35 Add to cart
23 AWG Solid Core Hookup Wire Wiring 1 Rs.10 Add to cart
Total Rs.1,027 Add all to cart

Built and Backed by Compoden. Every part in this tray was checked against live Compoden stock at the time this build was captured, and the same AI assistant that specced it also generates the wiring diagram and starter firmware for it, so you're not left matching a parts list to a separate tutorial's pinout. If a part goes out of stock or a better-fit alternative appears, the assistant will surface it the next time you run this build.

FAQ

Can a line follower work with just one IR sensor? Only in a limited way. A single sensor can tell you "on the line" or "off the line," but not which direction to correct, so the robot can only stop or search once it loses the line rather than steer smoothly back onto it. Two or more sensors are what actually let it correct direction.

Why does the robot need a separate motor driver like the L298N? An Arduino Uno's pins can only supply a small amount of current, far less than a DC motor draws under load. The L298N sits between the Arduino and the motors, taking a low-current signal from the Arduino and switching the higher current the motors need, in both directions.

Why do I need to recalibrate the sensor for a new track? The onboard potentiometer on IR line sensor modules sets a fixed brightness threshold. Different tape colors, floor materials, and ambient lighting all shift how much infrared reflects back, so a threshold tuned for one track can misread another until it's adjusted again.

Can I power the motors from the same 9V battery as the Arduino? You can wire them to the same battery, but it isn't ideal: the motors draw a large, variable current that can cause voltage dips affecting the Arduino. Many builders route a separate, adequately rated supply to the L298N's motor input while keeping the Arduino on its own regulated supply.

Back to blog