RobotControl Run along your track

Run along your track

This example shows you how to make run your Robot along a track realized by you. 
Draw with a thick black marker or with a black electrical tape a track on the floor. 
Put the robot on the track and turn it on. It will scan the floor to detect the line. 
When it finds it, the robot will start following the path. 
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. 
To indicate a stopping point, place another piece of tape perpendicular to the path.

Hardware Required

  • Arduino Robot
  • SD-card
  • Thick black marker or a black electrical tape
  • Large piece of white paper
  • Tape


  • Prepare the track: draw a thick line on some white paper, making sure the ends of the line goes together. The line should be around 3 cm (1 inch) wide.
  • Paste with tape the paper to the ground.
  • Upload the sketch.
  • Unplug USB.
  • Turn on power.
  • Place the robot on the track.

draw the track for Arduino Robot

  • After the starting screen, the robot will calibrate itself. It'll turn around a few times, detecting the line.
  • The robot will start following the line and it'll be continuously going around the track untill it detects a black shape covering all the five sensors at once and it will stop.

Run the Arduino Robot on the track


Change the parameters of this function "Robot.lineFollowConfig(KP,KD,robotSpeed,intergrationTime) " if the robot is not working as expected, or you want to change the robot's speed while line-following. 
The function parameters are:

  • KP : int, proportional gain
  • KD : int, derivative gain
  • robotSpeed: int, between 0 and 100, it indicates the percentage speed of motors.
  • integrationTime: int, delay between each time it runs the algorithm in ms

KP helps the robot to follow the line, and KD dampens the oscillation. 
It have to do some experimentation to find a good pair of values.

The complete code and its detailed description are shown down.

/* Robot Line Follow
* Arduino Robot
created 1 May 2013
by X. Yang
modified 12 May 2013
by D. Cuartielles
This example is in the public domain
#include <ArduinoRobot.h> // include the robot library
#include <Wire.h>
#include <SPI.h>
long timerOrigin; // used for counting elapsed time
void setup() {
// initialize the Robot, SD card, display, and speaker
// show the logots on the TFT screen
Robot.drawBMP("lf.bmp", 0, 0); // display background image
Robot.playFile("chase.sqm"); // play a song from the SD card
// add the instructions
Robot.text("Line Following\n\n place the robot on\n the track and \n see it run", 5, 5);
Robot.text("Press the middle\n button to start...", 5, 61);
// These are some general values that work for line following
// uncomment one or the other to see the different behaviors of the robot
//Robot.lineFollowConfig(14, 9, 50, 10);
Robot.lineFollowConfig(11, 7, 60, 5);
//set the motor board into line-follow mode
// start
Robot.fill(255, 255, 255);
Robot.stroke(255, 255, 255);
Robot.rect(0, 0, 128, 80); // erase the previous text
Robot.stroke(0, 0, 0);
Robot.text("Start", 5, 5);
Robot.stroke(0, 0, 0); // choose color for the text
Robot.text("Time passed:", 5, 21); // write some text to the screen
timerOrigin = millis(); // keep track of the elapsed time
while (!Robot.isActionDone()) { //wait for the finish signal
  Robot.debugPrint(millis() - timerOrigin, 5, 29); // show how much time has passed
Robot.stroke(0, 0, 0);
Robot.text("Done!", 5, 45);
void loop() {
//nothing here, the program only runs once. Reset the robot
//to do it again!