RoboticsA Line-Following Car
Two infrared sensors see a black line on the floor and the car follows it on its own, round the bends too. On a wide black strip it stops, as if at a finish line.
RoboticsA two-motor robot car drives forward, spots a wall from afar with its ultrasonic “eye”, stops, looks left and right and turns where the way is clear.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Fix the two motors and wheels to the chassis plate, and the swivel wheel at the front or back. If the motors have no wires, solder them on.
Connect the left motor to the L298N's OUT1 and OUT2 terminals and the right one to OUT3 and OUT4. Take the jumpers off ENA and ENB and wire those pins to the board's pins 5 and 6. Connect IN1, IN2, IN3 and IN4 to pins 7, 8, 4 and 2.
Charge the cells fully and switch the holder off. Connect the holder's “+” to the L298N's 12V terminal and to the board's VIN, and “−” to the L298N's GND and the board's GND. Everything must share the same ground, or the motors won't work. Check “+” and “−” twice before switching on: reversed polarity on VIN destroys the board.
Fix the HC-SR04 to the servo arm with glue or a bracket, and the servo to the front of the car. Connect the servo to pin 3, the sensor's TRIG to pin 12 and ECHO to pin 11. Both take power from 5V (the servo's red wire and the sensor's VCC) and ground from GND (the servo's brown wire and the sensor's GND).
Keep the car wheels-up while uploading. If a wheel spins backwards, swap that motor's two wires. If the car is too fast, lower SPEED.
#include <Servo.h>
// L298N: take the jumpers off ENA and ENB and wire them to PWM pins
const int ENA = 5, IN1 = 7, IN2 = 8; // left motor
const int ENB = 6, IN3 = 4, IN4 = 2; // right motor
const int TRIG = 12, ECHO = 11, SERVO = 3;
const int SPEED = 170; // 0-255: lower it if the car is too fast
const int STOP_CM = 25; // stop when something is this close
Servo neck;
void drive(int left, int right) { // -255..255 for each side
digitalWrite(IN1, left > 0 ? HIGH : LOW);
digitalWrite(IN2, left < 0 ? HIGH : LOW);
digitalWrite(IN3, right > 0 ? HIGH : LOW);
digitalWrite(IN4, right < 0 ? HIGH : LOW);
analogWrite(ENA, abs(left));
analogWrite(ENB, abs(right));
}
long distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000);
return us == 0 ? 400 : us / 58; // nothing in range counts as far away
}
long lookAt(int angle) {
neck.write(angle);
delay(400); // let the servo get there
return distanceCm();
}
void setup() {
int pins[] = {ENA, IN1, IN2, ENB, IN3, IN4, TRIG};
for (int pin : pins) pinMode(pin, OUTPUT);
pinMode(ECHO, INPUT);
neck.attach(SERVO);
neck.write(90); // look straight ahead
delay(2000); // time to put the car on the floor
}
void loop() {
if (distanceCm() > STOP_CM) {
drive(SPEED, SPEED);
delay(50);
return;
}
drive(0, 0); // something ahead: stop
delay(100);
drive(-SPEED, -SPEED); // back off a little
delay(300);
drive(0, 0);
long left = lookAt(160);
long right = lookAt(20);
neck.write(90);
if (left > right) drive(-SPEED, SPEED); // turn on the spot to the left
else drive(SPEED, -SPEED); // or to the right
delay(350);
drive(0, 0);
delay(200);
}If the car turns towards the wall, the servo is mounted the other way round: swap 160 and 20 in the code.

Illustration, made with AI
Illustration: on a living-room floor a robot car with an ultrasonic sensor on a servo approaches wooden blocks.
Step 6: If it looks the wrong way
RoboticsTwo infrared sensors see a black line on the floor and the car follows it on its own, round the bends too. On a wide black strip it stops, as if at a finish line.
RoboticsAn HC-05 Bluetooth module links the car to your phone. Drive it forward and back and turn it left and right with the buttons of an Android app, and change its speed too.
NewRoboticsFive infrared sensors measure how far the robot has drifted off the line, and a PID controller corrects the motors smoothly and fast. Built for competitions: the track, the sensor bar, the code and how to tune it.