Send Infrared (IR) remote signal with Python
In today's connected world, we find ourselves surrounded by countless devices, each with its own remote control. Wouldn't it be amazing if we could harness the power to control these devices ourselves, using just a single tool? Well, with the magic of Python and Raspberry Pi, the ability to send and automate Infrared (IR) remote signals, becomes a reality.
In our increasingly digitized lives, Python, Raspberry Pi—provides a gateway to unlocking the potential of IR remote control. By leveraging their capabilities, we can tap into the fascinating world of IR communication, enabling us to interact with a wide range of devices such as TVs, air conditioners, audio systems, and more.
Prepare Raspberry Pi
First of all, we need to prepare the hardware and record IR codes in case we need them. I have already discussed about them here.
Now, in our Raspberry Pi terminal, We need to modify boot configuration to enable IR transmission, to do so edit the boot config file by running:
sudo nano /boot/config.txt
There, find these lines:
And uncomment only those lines to enable IR transmission:
Now restart the Raspberry Pi by running
Install LIRC module
From your Raspberry Pi terminal, install
sudo apt update sudo apt install lirc -y
Verify installation by checking
lirc version, in my case it’s
version 0.10.1 :
$ lircd --version lircd 0.10.1
Now, we need to configure the lirc module. Open the options file in nano
sudo nano /etc/lirc/lirc_options.conf
and change the value of
driver = default device = /dev/lirc0
Now reboot the system by running
sudo reboot and after rebooting, check whether the LIRC daemon is running or not.
sudo /etc/init.d/lircd status
If it’s running, you will see
active (running) mentioned in the terminal’s output.
You may create a
virtualenv for this project or install lirc globally. Let's install
lirc for python:
pip install lirc
Now, make a python file, say
lirc there and initialize LIRC Client :
import lirc client = lirc.Client() print(client.version())
If you see the version name printed, then everything is going well.
Now, get the
list of remotes available in your system by running:
You can also get a
list of available keys/buttons for a remote by running:
Now, to send IR signals to our device, we can call
send_once(remote: str, key: str, repeat_count: int = 0) method where
remote is the remote name,
key is the key/button name,
reapeat_count is the number of times the signal will be sent (default is 0; for 0, signal will be sent once, for 1, signal will be sent twice and so on).
We want to send
POWER_ON signal for
client.send_once('LG_TV', 'POWER_ON', 0)
As, we are talking with hardware it's always better to wrap our code with
try except for unexpected exceptions.
from lirc.exceptions import LircdCommandFailure try: client.send_once('LG_TV', 'POWER_ON', 0) except LircdCommandFailure as e: print(e)
See the list of all possible LIRC exceptions
LircError, LircdSocketError, LircdConnectionError, LircdInvalidReplyPacketError, LircdCommandFailureError, UnsupportedOperatingSystemError
Finally, don't forget to close the connection by
You can find the detail API specification here.