Chassis with line tracking for Arduino

FunduinoLast updated on

Ein Roboterfahrzeug einer schwarzen Linie folgen lassen

Ein Roboter, der einer aufgeklebten Linie folgt, gehoert zu den dankbarsten Arduino-Projekten überhaupt: Das Ergebnis ist sofort sichtbar, und die Logik dahinter passt in wenige Zeilen.

Erstaunlicherweise genügt dafür ein einziger Sensor, der nur hell und dunkel unterscheiden kann. Das Fahrzeug fährt dann nicht mittig auf der Linie, sondern pendelt im Zickzack an ihrer Kante entlang – und kommt dabei zuverlässig voran.

Wiring diagram of the chassis with L298N motor driver and line sensor
Wiring diagram: H-bridge, both motors, and the infrared sensor connected to the microcontroller

You need this

Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino 13,90 € *Item no. F23107480About the product — Funduino UNO R3 Mikrocontroller - ATmega328P, 16 MHz, für Arduino
Chassis Komplettset - 2 Getriebemotoren, Räder, Batteriefach Chassis Komplettset - 2 Getriebemotoren, Räder, Batteriefach 34,90 € *Item no. F23105900About the product — Chassis Komplettset - 2 Getriebemotoren, Räder, Batteriefach
L298N Motortreiber - H-Brücke für 2 DC-Motoren, 5-35 V L298N Motortreiber - H-Brücke für 2 DC-Motoren, 5-35 V 3,22 € *Item no. F23107161About the product — L298N Motortreiber - H-Brücke für 2 DC-Motoren, 5-35 V
TCRT5000 Reflexsensor 90° - Infrarot, 1-50 mm, 3,3-5 V TCRT5000 Reflexsensor 90° - Infrarot, 1-50 mm, 3,3-5 V 1,69 € *Item no. F23107303About the product — TCRT5000 Reflexsensor 90° - Infrarot, 1-50 mm, 3,3-5 V
Batteriefach 6x AA - 9 V Ausgang, Clip-Anschluss Batteriefach 6x AA - 9 V Ausgang, Clip-Anschluss 1,59 € *Item no. F23108808About the product — Batteriefach 6x AA - 9 V Ausgang, Clip-Anschluss
Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei 2,90 € *Item no. FUN-1010553About the product — Breadboard 830 Kontakte - Steckbrett für Arduino, lötfrei
Breadboardkabel männlich-männlich - 40 Stück, 20 cm Breadboardkabel männlich-männlich - 40 Stück, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboardkabel männlich-männlich - 40 Stück, 20 cm

All 7 parts together 59,94 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Material

For this project, you need:

  • 1× microcontroller with a suitable USB cable
  • 1× robot chassis with two DC motors
  • 1× L298N H-bridge
  • 1× infrared obstacle detection sensor with digital output (e.g., TCRT5000)
  • 6× AA batteries (1.5 V) with battery holder
  • Male-to-male jumper wires

You’ll also need a test track: a wide black line on a light-colored surface, for example, made by sticking electrical tape onto paper.

Prerequisite: the chassis is assembled.

This guide assumes that the chassis is already assembled - the motors, wheels, and H-bridge are in place. If not, assemble it first using the chassis assembly instructions.

This is the next step: the vehicle should follow a black line autonomously without straying from it.

Structure

The sensor is mounted at the front of the chassis, with its eye facing down toward the floor. Wire it according to this diagram:

  • Sensor OUT → Pin 2
  • H-bridge ENA → Pin 10, IN1 → Pin 9, IN2 → Pin 8 (left motor)
  • H-bridge ENB → Pin 5, IN3 → Pin 7, IN4 → Pin 6 (right motor)

A suitable mount for the sensor can be 3D-printed.

Sensor mount on Thingiverse

The microcontroller and H-bridge are powered together by six AA batteries at 1.5 volts each - that is, 9 volts. The motors don't get enough power from the USB port alone.

Wiring diagram of the chassis with L298N motor driver and line sensor
Wiring diagram: H-bridge, both motors, and the infrared sensor connected to the microcontroller

How line following works

The infrared sensor provides only two states: 1 for a light surface, 0 for a dark one. This single piece of information is already enough to follow a line - not by driving down the middle of it, but by zigzagging along its edge.

  • When the sensor detects white (1), only the left motor runs. The vehicle turns right - back toward the line.
  • When the sensor detects black (0), only the right motor runs. The vehicle turns left - back out toward the edge.

As a result, the robot follows a continuous zigzag path along the edge of the line. It looks unsteady, but works reliably - and needs only a single sensor.

There is a potentiometer on the sensor that lets you adjust its sensitivity. Turn it until the LED on the module switches reliably over the black line - there’s no point running the sketch before that.

The sketch

Upload the sketch to the microcontroller and place the vehicle on the edge of the line. With the USB cable connected, you can use the Serial Monitor to see what the sensor is detecting.

If the robot moves too erratically or loses the line on curves, reduce its speed: You can choose any value between 0 and 255 for GS at the very top of the sketch.

Sketch: Follow the line
int GS = 200;    // Geschwindigkeit der Motoren. Moeglich sind Werte von 0 bis 255.
int DWert;       // Variable fuer den ausgelesenen digitalen Wert

// Gleichstrommotor 1 = LINKS
int GSM1 = 10;   // Enable-Pin: steuert die Drehgeschwindigkeit des linken Motors
int in1 = 9;     // Ueber in1 und in2 wird die Drehrichtung des linken Motors gesteuert
int in2 = 8;

// Gleichstrommotor 2 = RECHTS
int GSM2 = 5;    // Enable-Pin: steuert die Drehgeschwindigkeit des rechten Motors
int in3 = 7;     // Ueber in3 und in4 wird die Drehrichtung des rechten Motors gesteuert
int in4 = 6;

int Sensor = 2;  // Pin, an dem der Liniensensor ausgelesen wird

void setup()
{
  Serial.begin(9600);
  pinMode(GSM1, OUTPUT);
  pinMode(GSM2, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(Sensor, INPUT);
}

void loop()
{
  DWert = digitalRead(Sensor);   // Digitalen Wert am Sensor auslesen

  // Zur Kontrolle: der Wert wird an den Seriellen Monitor geschickt
  Serial.print("Wert: ");
  Serial.println(DWert);

  if (DWert == 1)   // Weisser Untergrund erkannt ...
  {
    // ... dann laeuft nur der linke Motor und das Fahrzeug dreht nach rechts,
    // also zurueck in Richtung Linie.
    analogWrite(GSM1, GS);
    digitalWrite(in1, HIGH);   // Vorwaerts. Rueckwaerts waere in1 LOW und in2 HIGH.
    digitalWrite(in2, LOW);

    // Der rechte Motor steht still: beide Richtungspins auf LOW.
    digitalWrite(in3, LOW);
    digitalWrite(in4, LOW);
  }

  if (DWert == 0)   // Schwarzer Untergrund erkannt ...
  {
    // ... dann laeuft nur der rechte Motor und das Fahrzeug dreht nach links,
    // also wieder an die Kante der Linie heran.
    analogWrite(GSM2, GS);
    digitalWrite(in3, HIGH);
    digitalWrite(in4, LOW);

    // Der linke Motor steht still.
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
  }
}
Rate the instructions

Comments

2 posts
Funduino TeamTeam

Hallo Bernd,
danke für den Tipp aus der Praxis! Du hast recht: Mit einem Sensor pendelt das Fahrzeug im Zickzack an der Linienkante entlang. Das ist gewollt, weil es den Einstieg so einfach wie möglich macht. Wer mehr will, ist mit dem IR Liniensensor 3-fach gut bedient. Deine Gewichtung mit Zweierpotenzen ist dafür ein schöner Kniff: Jedes Sensormuster ergibt genau eine Zahl, und das Programm kann für jede Zahl eine passende Lenkbewegung wählen.
Funduino Team

Bernd Albrecht

Lieber gleich mehrere Sensoren einsetzen. Das Dreiermodul im Sortiment ist klasse. Fünf ist noch besser. Wenn man die Werte der Sensoren von links nach rechts mit den Zweierpotenzen gewichtet, kann man viel bessere Fahrbefehle vorgeben.

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.