Control Raspberry Pi Robots via Infrared Remote Control (Part 3)

After our robot has already learned to drive and to follow a line, we now want to let it be controlled remotely. For this, we will first use an infrared remote control and later possibly an Xbox 360 controller. In addition to controlling the driving ability, the modes (follow line, autopilot, etc.) should also be set.

In this part of the Raspberry Pi robot tutorials, we program the IR receiver and create the commands for remote control. The remote control is also shown in the video.



Required Hardware Parts

In addition to the previous components, we also need an IR remote control and infrared diode for this tutorial (available here as a kit). Usually, the jumper cables are already included and the diode can be connected directly. If you buy the diodes individually (or still have them available), please have a look at the datasheet to be on the safe side with the following wiring.

You can also use the IR diode with remote control for general remote control of your Raspberry Pi, be it in a multimedia system such as KODI or just to start scripts.


Connection of the IR receiver

The connection of the infrared receiver to the Raspberry Pi is very easy, as the module only has three pins. If you are using a receiver including a PCB (see above), the connections will be named. Otherwise, you would have to look in the datasheet for the correct assignment, as this may differ.

In principle, the VCC pin is connected to a free 5V pin of the Raspberry Pi, the GND pin to ground/GND of the Pi and the pin labelled IN/DATA goes to GPIO 26.

Raspberry Pi Roboter Infrarot IR Steuerung

Previous circuit of the Raspberry Pi robot (including IR receiver)



To use the infrared receiver, we need the Lirc program. I have already shown the installation and setting of Lirc in a previous tutorial. I assume that this has been implemented because we need a Lirc in the following. The lower section (infrared Raspberry Pi universal remote control – start programs) is about creating the lircrc file. In order to control the robot and recognize the commands later in our script, we need to create such a file.

As soon as you have installed and tested Lirc, we create this file in the folder in which the rest of the robot scripts are located:

cd robot/
sudo nano lircrc

The following content is written to the file. You can of course also choose a different assignment, I have chosen this one:

In a moment we will wait for the remote control codes. To do this, the character string is transferred to the program under “config”. You can also assign other keys here later, e.g. to make other actions available later. In my case, the mode for following a line is started with the Play/Pause button.

In order to be able to use Lirc via Python, we then install a corresponding library:

sudo apt-get install python-lirc

If you are using Python 3, the package is python3-lirc.



Code to Receive Infrared Signals

We are expanding our previously created robot class. To do this, we create the IRControl() function after the end of the lineFollowModeOn(self)function.


You can also see the complete (previous) code here. In the Nano Editor, you save with CTRL + O and close with CTRL + X.


Background Process for Controlling the Robot

Now we want to test the functionality. We have two options for this. The simpler one is that we just enter the following Python code in the console (after sudo python). The other variant is to create a script with the appropriate code and start it in the background after booting. This is e.g. especially important on Raspberry Pi Zero if you don’t have a WLAN adapter connected. Tip: If you also have a “big” Raspberry Pi 3/2B, you can first play the code on the Pi via SSH or FTP and then just insert the Micro SD card into the Zero.

The code for the test file is quite simple (sudo nano

Either we just start the file (sudo nano or we let it open automatically when you restart.

The easiest way to do this is to use crontab:

crontab -e

At the end we write the following line:

@reboot ~/robot/

From now on, the file will be called upon restart and you can control the Raspberry Pi robot remotely via infrared remote control.

In the next tutorial, we will give the robot an “autopilot” so that it automatically moves to where there is no object in the way. Part 6 of the series also shows controlling it via the Xbox 360 controller.

Leave a Comment

Your email address will not be published.

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