Infrared sensors are a convenient tool for sending commands to the copter and are easily configured to work with Raspberry Pi 3 using the Python language. Most IR receivers have three pins: ground (GND), 5V power supply (VCC) and signal output (OUT). The connection of the signal output to a particular GPIO pin of the Raspberry Pi does not have to be to GPIO 17 – this parameter can be changed during configuration.
To work with the IR receiver, the LIRC (Linux Infrared Remote Control) library is used, which is supported by the Raspbian system and provides sending and receiving commands via the infrared channel. Installing it requires connecting the Raspberry Pi to the Internet and running a few commands in the terminal, including updating packages and installing lirc and python-lirc, as well as the py-irsend module for Python.
After installation, you need to edit the system files: in /etc/modules add the lirc_dev and lirc_rpi modules specifying the GPIO pins for receive and transmit, and in /boot/config.txt include the appropriate overlay with the pin parameters. The /etc/lirc/hardware.conf file specifies the driver loading parameters, and /etc/lirc/lirc_options.conf specifies the driver and device. After all the settings, the Raspberry Pi is rebooted.
To test LIRC operation you can use the lircd daemon status command, and to test the signal you can stop the daemon and run the mode2 command with the device specified. If it is working properly and the IR transmitter is pointed at the receiver, the terminal will display pulsing signals.
If you want to use your own remote control, you will need to record its configuration using the irrecord utility, executed when the lircd daemon is stopped. In the process of recording the remote control you should carefully follow the instructions and at the final stage set the names of buttons. The result will be a file with settings, which should be moved to the /etc/lirc/ directory and restart the daemon. After that, the irw command allows you to see which buttons are being pressed.
Sometimes there is a problem with the bit descriptions of the buttons in the configuration file – if the second numbers in the codes are repeated, they must be removed to make the button names unique. After fixing and restarting the daemon, the system correctly recognizes keystrokes and outputs them to the terminal.
To work with IR signals in custom Python scripts, the python-lirc package is used. In the directory where the script is run from, a .lircrc file is created with a description of the buttons and their corresponding commands that the program will process. The file format contains blocks specifying the name of the program, the button, and the configuration the program receives when clicked.
The Python script itself initializes work with LIRC and processes incoming push codes, performing certain actions depending on the received signal.
To work with the IR transmitter, the connection is made to the same GPIO pins as during setup. With the irsend command, you can send pre-recorded signals by specifying the name of the remote and the button. In Python, the py_irsend module is used for this purpose, allowing you to programmatically send IR commands via the send_once function, which opens up the possibility to integrate control via infrared signals directly from your programs.