Examples > Robot?

Runaway Robot

Is your robot bumping into walls? This quickly solves that problem. By attaching a ultrasonic rangefinder, the robot can find out if it's too close to obstacles, and turn around to avoid collision.

Hardware Required

  • Arduino Robot
  • ultrasonic rangefinder
  • cable to plug the Ultrasonic sensor


  1. Connect the Ultrasonic ranger to TK1
  2. Fix the sensor to the front of the robot
  3. Upload the example, unplug USB and turn on power
  4. Put the robot on the floor
  5. The robot will move around, avoiding obstacles in front of it
  6. If you want to change the detection range, in the code below, find the line @@while (getDistance()<20)**, 20 here means 20cm; change it to another value to try out other sensitivities.
  7. You can also change the robot speed, look at motorWrite() for a hint on how to do that.

Try it out

Runaway robot


SORRY, There is an error at our code repository, please inform to web@arduino.cc

See Also: