Experiment 5 Obstacle Avoidance

Share for us

This experiment adds an obstacle avoidance module based on the first experiment. In this experiment, when the car detects an obstacle, it will turn automatically in case of crash. After the assembly and circuits are completed, if you feel the wires are too messy, you may wrap them with the heat shrink tubing so the car can be more organized.

The physical diagram is as shown below:


-1 * Car model

-2 * Obstacle Avoidance Module

-1 * Photoelectric Switch

-1 * Small Servo (blue),

-2 * 18650 Battery

-1 * USB Cable

-Several jumper wires

Experimental Principle

When the car detects no obstacle, the MG90 servo with a photoelectric switch module rotates back and forth between 60 and 120 degrees. When it detects an obstacle, the car will stop and the MG90 servo rotates from 0 to 180 degrees.

From the perspective of programming, the range of 0-180 degrees is divided into two parts. For example, 0-90 degree is one part, and 91-180 degree is the other part. The photoelectric switch module can detect obstacles constantly in a certain angle range. Then the results are divided into two parts, and the low level 0 is counted – the number means how many times the obstacle is detected in the range. The MG995 servo then turns to the side with the smaller number. The motors of the car start to drive the wheels moving.

At the same time, the MG90 servo still rotates constantly to detect the obstacle on the other side until it cannot detect any obstacle. This means the car turns away successfully. In order to assist that process, two short-distance IR obstacle avoidance modules are added to set a secure turning distance for car turning. Since the car is likely to hit the obstacle due to a short turning distance, the car backs in the case until it reaches a secure turning distance, and then runs forward bypassing the obstacle.


Since the obstacle avoidance module and photoelectric switch are sensed via IR light, it is not suggested to apply on a black background or use a black obstacle for better effects.

Experimental Procedures

Step 1. Assemble the mount

1. Fasten the following rocker arm to the acrylic plate with M1.2*5 self-tapping screws.The rocker arm is packaged with the MG90 servo together.  
2. After assembly, it is as shown below:   

Step 2. Connect the MG90 servo, as shown in the following table:

Servo MG90Sensor Shield
Red (Power supply)5V
Brown (Ground)GND
Orange (Signal line)3

Connect the SunFounder Uno board to the computer via a USB cable. Then upload the sketch under Smart Car for Arduino\Code\Servo to the board, remove the USB cable and then press the button on the switch module.

Step 3. Assemble the servo

1. Fasten SG90 servo to the following acrylic plate with M2*10 screws and M2 nuts.   2. After assembly, it is as shown below:     
3. Fasten the acrylic plate with a round hole by M3*12 screws and M3 nuts.   4. After assembly, it is as shown below:     
5. Fasten the MG90 servo and its base acrylic plate with servo screw.Packaged with MG90 servo, the servo screw is within the shorter ones of three screws. The two plates should be in parallel as shown by the arrows.   6. After assembly, it is as shown below:NoteThough the acrylic plate and servo can be assembled directly, the screw should still be used to avoid the gear wear.    

Step 5. Connect the photoelectric switch

Photoelectric SwitchSensor Shield
Red (Power Supply)5v
Green (Ground)GND
Yellow (Signal line)12

After the photoelectric switch is connected, you need to adjust it for use. Place it flat on the desk, set an obstacle in white in its front. Since its detection distance is farther than that of the two obstacle avoidance modules, you’re suggested to place the obstacle 30-40cm away from switch. Adjust the potentiometer at its back. When it detects obstacles, the LED at its back will brighten. Therefore, you need to do the adjustment that it detects objects steadily at a distance of 30-40cm, and detection completed at the instant when the obstacle appears. So you can calibrate the switch by the LED. Move the obstacle forward, backward, to the left and to the right. During this course, if the LED dims at once when it’s moved away and brightens again when it’s placed back, it means it’s adjusted successfully.

Step 6. Assemble the photoelectric switch

1. Unscrew one nut on the photoelectric switch. 
2. After unscrewing, it is as shown below: 
3. Stick the photoelectric switch to the mount and screw the nut back.
4. After assembly, it is as shown below: 

Step 7. Assemble Mount

Fasten the mount to the smart car with M3*12 screws and M3 nuts. 
After assembly, it is as shown below: 

 Step 8. Connect two obstacle avoidance modules

Pin OUT of the two obstacle avoidance modules is respectively connected to pin 4 and pin 7 of the sensor shield.

Obstacle Avoidance ModuleSensor Shield


After the IR obstacle avoidance sensor module is wired to the car, you need to adjust its sensitivity.

There are two LEDs on the obstacle avoidance module: power indicator LED, which brightens when the module is powered, and signal indicator LED, which does so when the module encounters an obstacle. There are also two potentiometers on the module; facing the front of the car, the left pot is to adjust the sensibility and the right one is to do the detection range.

Place the obstacle avoidance sensor module on a flat surface, and an object (better in white) in its front, about 7 or 8 cm away (not suggest to set it too far from the modules).

First adjust the left pot. Move the obstacle left and right, out of the detection range and move it back. Check whether the LED on the right side (i.e. the signal indicator, when you’re facing the car front) of the module brightens steadily when an obstacle appears in front, and dims after the obstacle is gone.

Then adjust the pot at the right. Place the obstacle in front of the module, move it closer and farther. Check whether the signal LED simply brightens and dims as the obstacle approaches and moves away. Repeat the steps until the car can detect an obstacle beyond 7 or 8 cm stably.

Step 9. Assemble the two obstacle avoidance modules

Place the two obstacle avoidance modules below the chassis on two directions and fix them with M3*8 screwsM3*20 copper standoffs, and M3*6 screws.  
After assembly, it is as shown below:  

The figure of physical objects:

Front View

Top View

The car when the assembly of this experiment is done (no wire):

Top View

Now that the assembly is done, switch off the car. Organize the wires – for elegant appearance, you may wrap the wires with the cable pipe.

Plug in the USB cable, upload the sketch under Smart Car for Arduino\Code\5.Car_avoidobstacle\Car_avoidobstacle to the board. Press the button on the switch module, and you can see the photoelectric switch sways when there’s no obstacle detected, and the car goes forward continuously; it will stop and scan when obstacles detected.