Table of Contents

Abstract

This article contains open-source Arduino code for a line-following robot which follows a black line on the ground. This code is free to modify as necessary. The original code was found in the book “Arduino Robotics” by John-David Warren et al. It has been modified to work with the newest version of Adafruit's Arduino-based motorshield.

Line-following robots are relatively simple for the average hobbyist to build. If you would like instructions on how to build the robot mechanically, you can find these instructions all across the great global web. A good place to start would be the aforementioned book. It has step-by-step instructions, a parts list, and a list of places to get the parts.

For my robot, I purchase a pre-designed robot chassis and an electronic line sensor. The line sensor unit had five pairs of LED emittors and detectors, which is what the code is designed to handle. Your sensor unit — whether you build it or purchase it — may have more or less sensors, but five sensors is a pretty good number for the purposes of this robot. If you need fewer sensors, just comment out the unneeded sensors. If you need more sensors in the code, you can add more easily. It shouldn't be too hard to do if you have any experience with Arduino programming. If you don't know how to do it, I don't know why you are building this robot at your current level of knowledge. Time to learn something new! Visit Arduino's home website to learn about programming, and then try again.

Something to note: For the particular robot that I built, mechanical difficulties prevented me from using two of the sensors on the sensor unit. You will notice that in the comments. However, the code was easier to handle when these sensors were left in the program. Instead, I soldered the two unused sensor wires together with the center sensor on the array so that the three centermost sensors functioned as one sensor. Otherwise, I would have had to rewrite the entire original program. So this code should end up working fine on a robot with three sensors or five sensors. But you will have to troubleshoot for yourself. I am just posting this to get you started, and to prevent you from having to think out the whole program from beginning to end. As long as you can define your sensors properly, and you are using an Arduino Uno microprocessor and an Adafruit motorshield version 2, you should be golden.