Note: this page refers to a product that is retired.

RobotLineFollowing

Draw a racing track and get your robot to run on it.

Line Following

Take a large piece of paper (or tape smaller pieces together to make a big one) and draw a shape on it using a thick black marker. This shape will be your racing track.

Put the robot on top of the line and turn it on. It will scan the floor to detect the line. When it finds the line, it will start following it the path.

Compete with other robots to see which one makes it faster!

Hardware Required

  • Arduino Robot

  • Large piece of white paper

  • Thick black marker

  • tape

Instruction

  1. Prepare the track. Draw a thick line on some white paper, making sure the ends of the line goes together. Use multiple pieces of paper if one is not big enough. The line should be around 3cm (1 inch) wide.

  2. Tape the paper to the ground so the robot will not cause the paper to slip. The surface should have light color.

  3. Upload the example, unplug USB and turn on power.

  4. Place the robot on the track, so it is facing down the line.

  5. After the starting screen, the robot will calibrate itself. It'll wag around a few times, detecting the line.

  6. The robot will start following the line. If everything goes OK, it'll be continuously going around the track. If the robot goes off the line, see lineFollowConfig() for details about how to calibrate it.

Try it out

Draw your own racing track
Draw your own racing track

Let your robot run the race!
Let your robot run the race!

Code

1/* Robot Line Follow
2
3 This sketch demonstrates the line following capabilities
4
5 of the Arduino Robot. On the floor, place some black
6
7 electrical tape along the path you wish the robot to follow.
8
9 To indicate a stopping point, place another piece of tape
10
11 perpendicular to the path.
12
13 Circuit:
14
15 * Arduino Robot
16
17 created 1 May 2013
18
19 by X. Yang
20
21 modified 12 May 2013
22
23 by D. Cuartielles
24
25 This example is in the public domain
26
27 */
28
29#include <ArduinoRobot.h> // include the robot library
30#include <Wire.h>
31
32long timerOrigin; // used for counting elapsed time
33
34void setup() {
35
36 // initialize the Robot, SD card, display, and speaker
37
38 Robot.begin();
39
40 Robot.beginTFT();
41
42 Robot.beginSD();
43
44 Robot.beginSpeaker();
45
46 // show the logots on the TFT screen
47
48 Robot.displayLogos();
49
50 Robot.drawBMP("lf.bmp", 0, 0); // display background image
51
52 Robot.playFile("chase.sqm"); // play a song from the SD card
53
54 // add the instructions
55
56 Robot.text("Line Following\n\n place the robot on\n the track and \n see it run", 5, 5);
57
58 Robot.text("Press the middle\n button to start...", 5, 61);
59
60 Robot.waitContinue();
61
62 // These are some general values that work for line following
63
64 // uncomment one or the other to see the different behaviors of the robot
65
66 //Robot.lineFollowConfig(14, 9, 50, 10);
67
68 Robot.lineFollowConfig(11, 7, 60, 5);
69
70 //set the motor board into line-follow mode
71
72 Robot.setMode(MODE_LINE_FOLLOW);
73
74 // start
75
76 Robot.fill(255, 255, 255);
77
78 Robot.stroke(255, 255, 255);
79
80 Robot.rect(0, 0, 128, 80); // erase the previous text
81
82 Robot.stroke(0, 0, 0);
83
84 Robot.text("Start", 5, 5);
85
86 Robot.stroke(0, 0, 0); // choose color for the text
87
88 Robot.text("Time passed:", 5, 21); // write some text to the screen
89
90 timerOrigin = millis(); // keep track of the elapsed time
91
92 while (!Robot.isActionDone()) { //wait for the finish signal
93
94 Robot.debugPrint(millis() - timerOrigin, 5, 29); // show how much time has passed
95
96 }
97
98 Robot.stroke(0, 0, 0);
99
100 Robot.text("Done!", 5, 45);
101}
102void loop() {
103
104 //nothing here, the program only runs once. Reset the robot
105
106 //to do it again!
107}

How it works

The Robot has five infrared sensors on the bottom of the motor board that detect where light reflects back. That information is be used to decide whether to continue straight or to change the wheel speeds to turn.

Stopping

When the robot detects a black shape covering all the five sensors at once, it will stop. Use this feature to create tracks for the robot to stop and perform actions.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.