To avoid obstacles, you can place sensors around the robot's perimeter to activate whenever an object is encountered. Depending on how you want your robot to behave, one avoidance strategy is to just reverse any action last taken (with an additional turn for forward/backward actions) that caused one of the sensors to be activated.
Avoiding objects and obstacles
Getting ready
You will need some micro switches to be triggered when there is an impact with objects. Depending on the type you have, you need to place enough switches to detect any object around the outside (if required, you can use an additional length of wire to extend the reach of the switch). Shown in the following photo are two possible sensors that will...