Arduino 011: Line-Following Robot

This will help you:

Learn to make a robot follow a line drawn on the ground.

Time: 2-3 hours / Level: C2

You should already:

You Will Need:

  • A computer

  • An Arduino

  • An A-to-B USB cable

  • A half-size breadboard

  • 10k ohm potentiometer

  • 2x 10k Ohm resistors

  • A light sensor or color sensor

  • An assembled Arduino car

Get the code and resources for this activity by clicking below. It will allow you to download the files from a Google Drive folder. Unzip the folder and save it in a sensible location.

Step 1: Using the Sensor

Mount the sensor as shown in the image below. Connect the white wire to pin 9 on the motor shield, the red wire to 5v power, and the black wire to ground.

[image]

The sensor should be centered between the wheels, but ideally an inch or two in front of them. If you need to test which direction the car moves in, use the test code from the Car Startup activity.

Step 2: Calibrate the Code

Open light_sense.ino and upload the code to the Arduino. With the Arduino still connected to the computer, place the car on the ground (or driving surface) and move the sensor on and off of the marked line to figure out what the sensor reads in that environment. Open up the serial monitor (Ctrl Shift M) or the serial plotter (Ctrl Shift L) to see what values correspond to the mark and the plain surface. Take note of the typical peak height, the typical valley height, and a value which is a reliable threshold between on and off.

Step 3: Understand the (Bad) Code

Open line_following_bad.ino and read the code. It's a "line-following algorithm", but it's really, really bad. This is what it does:

while running
  get sensor value
  if sensor value > mark threshold:
    go forward a bit
  otherwise:
    turn 15 degrees right
end

In case you can't tell, this is a very bad algorithm and it could take forever. Upload and run the code, and think about these questions:

  • What's bad about it?

  • What information that the robot has is it not using?

  • How can you get the information that would be helpful from the sensor data?

  • What stored values would help you make decisions?

  • What functions would help the robot?

Seriously, think about these answers and write down an answer for every one. Then, go to the next step.

Step 4: Improve the Code

Open line_following_new.ino. This is your chance to make the code better! Try to change the set of steps the robot does so that it is better at finding the line quickly and going forward without getting too far off track. Remember these very important guidelines:

  • Make small changes or additions each time, and test them

  • Use the serial monitor to have the robot print out what it's thinking

  • If you have a new idea, copy the file, don't delete your work

  • Small changes to parameters can change performance Remember, the robot will only check the sensor when you tell it to. It won't check continuously.

If you get stuck, try drawing a diagram of what needs to happen, including ways the robot could get off track. Or, explain the problem to a friend and see what they think.

Good Luck! If you get your robot to work (Or not! failure is useful and sometimes funny) submit a video of it via the submission button below.