Raspberry Pi Robot: Avoiding Obstacles Automatically (Part 4)

Our robot can now do a lot and can be controlled remotely. However, we also want to create an autopilot mode so that the Raspberry Pi robot can find its way independently. Cleaning robots etc. use comparable methods to avoid getting stuck at an obstacle.

We use a servo motor and an ultrasonic sensor with which we measure the distance. Then we expand the previous code and let the robot go to where there is the most space.



Required Hardware Parts

This extension basically only uses two components, namely the motor for turning and also the ultrasonic module for determining the distance. In particular, I used these parts:

In addition, we also need the jumper cables, hot glue and possibly the breadboard.



Attachment of the Motor and Ultrasonic Sensor

Before we proceed with the connection, we need to attach the two modules to the robot body. For this, we use the hot glue, with which we first attach the servo motor to the front (above the front wheel and in front of the IR line-follow sensors). My body had a paper cover over the plastic, which I removed from the area to be glued so that the glue sticks better.

Then I took one of the attachments of the servo motor and attached the HC-SR04 module to it with hot glue (see pictures). The attachment can also be easily attached to the motor and removed again. You can run the cables of the servo on the lower side of the body to the Pi (there is a hole in the middle). I glued the jumper cables from the ultrasonic sensor together and fixed them in one place with a little space. However, you should wait until we test the functionality and thus find the best position for the cables.


Construction and Wiring

In the previous tutorials, we have already occupied some of the GPIOs with the previous modules. For this tutorial, however, we only need three additional GPIOs, whereby I am using GPIO12, 13 (US sensor) and 22 (servo). If you want to use other IO pins, you have to adapt this accordingly later in the code.

The ultrasound module is supplied with power via the 5V of the Pi, whereas the servo motor should be connected via the batteries. Ground of the Raspberry Pi and the batteries are (as before) still connected to each other.

Raspberry Pi robot servo + ultrasonic breadboard

In addition to the previously connected modules, there are also servo motors and ultrasonic sensors (left).

The ultrasonic sensor (ECHO) is connected to GPIO13 via a 330Ω resistor and also to GND via a 10kΩ resistor. I have already described the exact functionality of the US sensor in a previous tutorial. The same applies to the use of the servomotor via PWM. You can find details on both modules in the corresponding tutorials.


Expand Raspberry Pi Robot Code

In order to be able to measure distances from different directions in the following, we create a new file in which we define a class for the movable ultrasound module:

sudo nano ultrasonic.py

The class has only a few functions (for rotating and measuring) and is therefore not too long:

As always, the editor is saved with CTRL + O and closed with CTRL + X.

Before we continue, we want to position the ultrasonic sensor correctly. To do this, you can open the Python console as a test (sudo python) and enter the following lines (close with exit()):

We let the servo motor rotate by 90° and now fix the ultrasonic module in the middle of the servo motor so that the “eyes” look straight ahead. We do this because the motor will rotate 90° to the left and right and measure the distances to which we then let the robot react.

Then we have to extend our previous robot.py file. To do this, we first import the class we just created:

In addition, the class must be initialized. In the robot.py file, we add the parameters of the __init__ function and add a line so that the function looks like this:

Now we can add the function that reacts to the measured distances and controls the robot accordingly at the end of our Robot-class:


Testing the Autopilot

It is time to test the autopilot mode, which is why we are creating a small test file in which the robot class is loaded and the ultrasonic autopilot is started:

sudo nano test_us.py

The file only contains the following content:

After the file has been saved, we place the robot on the floor and call the Python file from the console (can be canceled with CTRL + C):

sudo python test_us.py

Important: If your robot turns too fast or too slowly, it is most likely due to an incorrectly set speed. In the first tutorial in the series, we have already mentioned defines the speed on a straight line (DIST_PER_SEC) and for a rotation (SEC_PER_TURN). However, since this depends on the substrate, it may have to be adjusted. I had e.g. during testing, the speed was measured on the table, but it differs from the actual speed on the floor, which is why the functions were only able to work properly after an adjustment.


It continues with following your own voice.

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!