Posted in Raspberry Pi â€ĸ â€ĸ

How to use Raspberry Pi as Infrared (IR) Remote

Using the Raspberry Pi as an IR remote allows you to send and receive infrared signals.

How to use Raspberry Pi as Infrared (IR) Remote

Raspberry Pi is an awesome device compared to its credit card size. Like any other remote controlled devices we can send infrared signal using Raspberry Pi to any of our remote controlled home appliances. The most interesting part about the Raspberry pi and IR combination is we can write scripts to automate several repetitive tasks we do everday using remote.

Preparation

Fire up your Raspberry Pi terminal and install lirc

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 device to /dev/lirc0 and driver to default :

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

Output of sudo /etc/init.d/lircd status

If it’s running, you will see **active (running)** mentioned in the terminal’s output.

Record IR signal in Raspberry Pi

Hardware

Along with Raspberry Pi, we need an IR receiver sensor to receive and record IR codes. You can search for "IR receiver" in your nearby tech shops or from the links here:

Usually in an IR receiver, we have 3 pins:

  1. VCC or +
  2. GND or - (ground)
  3. S or Signal

Now, connect the VCC pin with any of the 3.3v pin of Raspberry pi, GND pin with any gound pin and the S pin with Raspberry Pi’s GPIO pin 17.

Software

To send IR signal, we need to have remote’s signal data to instruct LIRC module about the signal first. Before jumping into recording the signal from our ‘real’ remote we can search for the remote in LIRC’s IR remote database because it’s much easier to download the remote file and start using right away instead of recording every button manually; and trust me, it’s not an easy process and lots of noises come in while recording.

First of all, search for your remote in the LIRC remote database where bunch of good people have already uploaded their remote files. If you don’t find your desired remote there, let’s make it by ourselves!

From 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:

#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

and uncomment only the first line to enable receiver at GPIO PIN 17:

dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

Now restart the Raspberry Pi by running sudo reboot.

Now, we are going to record the IR signals from our remote. Pick up the remote, for example LG TV remote and run this command in terminal:

sudo irrecord -d /dev/lirc0 --disable-namespace

The irrecord script will show instructions like the ones mentioned below and you need to follow them accordingly.

...
Please take the time to finish the file as described in
https://sourceforge.net/p/lirc-remotes/wiki/Checklist/ an send it
to  <lirc@bartelmus.de> so it can be made available to others.

Press RETURN to continue.

Output of sudo irrecord -d /dev/lirc0 --disable-namespace

Press ENTER, then it will check for ambient light noises (keep the sensor in darker environment):

Checking for ambient light  creating too much disturbances.
Please don't press any buttons, just wait a few seconds...

No significant noise (received 0 bytes)

No ambient noise was found

Then enter a remote name.

It is very important that you press many different buttons randomly
and hold them down for approximately one second. Each button should
generate at least one dot but never more than ten dots of output.
Don't stop pressing buttons until two lines of dots (2x80) have
been generated.

Press RETURN now to start recording.

Now press ENTER. It will then ask you to press buttons randomly and hold each of them for one second. And if everything goes well you'll see dots . in the output: .............

In my case, the irrecord script didn’t work and it kept receiving signals (dots .) and never stopped. So, I followed an alternative approach to capture the raw IR codes.

To capture raw code, run this:

sudo mode2 -m -d /dev/lirc0

While the script is running, press buttons in your remote from a close distance to the IR receiver. If everything goes well, you will see raw codes in the output:

Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0
Running as regular user pi

   683779

     9216     4495      550      567      547      571
      544     1683      548      568      547      569
      548      567      546      568      551      564
      548     1683      578     1655      548      567
      549     1682      548     1684      549     1683
      548     1684      548     1685      547      569
      549      564      547      569      547      594
      521      567      548      593      524      572
      543      569      545     1684      548     1684
      547     1685      552     1680      547     1689
      558     1672      546     1684      572     1660
      549    39422

     9218     2255      548   132646

Raw code for a single button press

We have to take only the numbers from the second row (here, it is 9216) and until the second last number of the second last row (here, it is 549; omit 39422 as outlier) for each button press from the remote:

     9216     4495      550      567      547      571
      544     1683      548      568      547      569
      548      567      546      568      551      564
      548     1683      578     1655      548      567
      549     1682      548     1684      549     1683
      548     1684      548     1685      547      569
      549      564      547      569      547      594
      521      567      548      593      524      572
      543      569      545     1684      548     1684
      547     1685      552     1680      547     1689
      558     1672      546     1684      572     1660
      549

Take only this part

Now, create a text file named TV_REMOTE.lircd.conf (TV_REMOTE can be anything):

nano TV_REMOTE.lircd.conf

and structure the file in the following format and list every remotes in between begin raw_codes and end_raw_codes :

begin remote

  name   TV_REMOTE
  flags RAW_CODES
  eps            25
  aeps          100

  ptrail          0
  repeat     0     0
  gap    20921

  begin raw_codes

    name POWER_BUTTON
     9216     4495      550      567      547      571
      544     1683      548      568      547      569
      548      567      546      568      551      564
      548     1683      578     1655      548      567
      549     1682      548     1684      549     1683
      548     1684      548     1685      547      569
      549      564      547      569      547      594
      521      567      548      593      524      572
      543      569      545     1684      548     1684
      547     1685      552     1680      547     1689
      558     1672      546     1684      572     1660
      549

    name VOL_UP
     8468     3960      527     1441      525      421
      526      423      526      423      527     1441
      524      424      525      422      526      423
      528     1436      526     1439      526      423
      525      423      527      422      526      422
      526      423      526      422      526      422
      527      422      526      423      528      422
      525      428      522     1438      527      427
      522     1439      529      421      526      421
      528      422      528     1435      527

  end raw_codes
end remote

TV_REMOTE_lircd.conf

💾 General Format:

begin raw_codes  
 name  <button_name_1> 
 <paste raw codes here>  
  
 name <button_name_2> 
 <paste raw codes here> 
end raw_codes

Now copy the file to LIRC directory:

sudo cp TV_REMOTE.lircd.conf /etc/lirc/lircd.conf.d/

Now, list all of the available remotes:

$ irsend LIST "" ""

devinput-32
TV_REMOTE
devinput-64

We can also list all of the available buttons for a specific remote, irsend LIST <remote_name> "":

$ irsend LIST TV_REMOTE ""

0000000000000001 POWER_ON
0000000000000002 VOL_UP

Send IR signal from Raspberry Pi

First of all, let’s set up the hardware. To send infrared signal we need to have infrared transmitter sensor or in short IR transmitter. You can search by IR transmitter in your nearby tech shop or from the shops mentioned above.

Hardware

There will be 3 pins in the IR transmitter:

  1. VCC
  2. GND (ground)
  3. DAT (data)

Connect the VCC pin with Raspberry Pi’s 5V VCC output, and the GND pin with any of the ground pins of Raspberry Pi, finally the DAT pin to Raspberry pi’s GPIO pin 18.

Software

Previously, we commented out a line in /boot/config.txt file, we need to uncomment it now:

dtoverlay=gpio-ir,gpio_pin=17
dtoverlay=gpio-ir-tx,gpio_pin=18

And, run sudo reboot.

Now, let’s send IR signal. As we have already recorded or downloaded our remote file now, we can now send the signals in this format irsend SEND_ONCE <remote_name> <button_name>:

irsend SEND_ONCE TV_REMOTE POWER_ON

And see your TV turning on without any remote! This opens up lots of possibilites to programmatically control our remote controlled electronics devices. I'll talk about building API using python or nodejs to send IR signals in the next blog.