Arduino 012: Wall-Following Robot

This will help you:

Learn to make a robot follow along a wall

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

  • 2x 10k Ohm resistors

  • A distance sensor like this one

  • 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 Distance Sensor

Mount the sensor on the side of the car body. Connect the 'VCC' pin to 5v power, the 'GND' pin to ground, the 'TRIG' pin to pin 9 on your Arduino, and the 'ECHO' pin to pin 10. The pin numbers don't really matter; they don't need to be PWM pins, so you could change this if you wanted to change the pin assignments in the script as well.

The sensor should be mounted on the Arduino car so that it faces to the left side. If you need to test which direction the car moves in, use the test code from the Car Startup activity. As the car travels forward, this sensor will act like an outstretched hand following the wall, making sure the robot doesn't get too close or too far.

Step 2: Calibrate the Code

Open distance_sense.ino and upload the code to the Arduino. With the Arduino still connected to the computer, hold a hand in front of the sensor. Slowly move your hand closer and farther from the sensor. It may give you better results to use a hard, flat object instead of your hand. Open up the serial monitor (Ctrl Shift M) or the serial plotter (Ctrl Shift L) to see what values correspond to what distance. Write down the values and distances every 6 inches or so, and calculate a number that multiplies the readings to determine the distance. Remember this number, you will need it to calibrate your code.

Step 3: Understand the (Bad) Code

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

while running
  get sensor value
  calculate distance: sensor value times conversion factor
  if distance > 12 inches:
    turn 15 degrees left
  otherwise, if distance < 6 inches:
    turn 15 degrees right
  otherwise:
    go forward a bit
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 wall_follow_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 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.

Step 5: Fail-Proofing Your Robot

Think about and test your robot, and see if the behavior accounts for these circumstances. Does the robot do what you want when:

  • A wall drops off at a 90-degree angle

  • A wall turns inward at a 90-degree angle in front of the robot

  • A wall section ends (i.e., there's a face that's only as thick as the wall)

  • The robot is placed in the middle of a room Try to figure out how these situations could be taken care of in your code. Follow the same suggestions as in Step 4 for improving your code.

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