Programmatically send IR remote signals using Python and Raspberry Pi
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.
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. You need to have:
- IR Infrared Transmitter Module Ir Digital 38khz Infrared Receiver
- Raspberry Pi: Model 3 B/B+ or Model 4 B
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
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
● lircd.service - Flexible IR remote input/output application support Loaded: loaded (/lib/systemd/system/lircd.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2023-05-18 14:43:25 +06; 2 days ago TriggeredBy: ● lircd.socket Docs: man:lircd(8) <http://lirc.org/html/configure.html> Main PID: 520 (lircd) Tasks: 2 (limit: 779) CPU: 139ms CGroup: /system.slice/lircd.service └─520 /usr/sbin/lircd --nodaemon
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:
TV_REMOTE is your remote name. Example Output:
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)
LircError LircdSocketError LircdConnectionError LircdInvalidReplyPacketError LircdCommandFailureError UnsupportedOperatingSystemError
Finally, don't forget to close the connection by
You can find the detail API specification here.