[HOWTO] Send IR commands to your TV or other devices from your Pi

I got tired of keeping a second remote control out just to turn my TV on and off, so I went in search of a way to get the Pi to do it for me. Here’s what I came up with. This should work with any IR device. This should work with any TV, but if your TV is CEC-capable, I think there’s much easier ways to do this.

LEARNING THE CODES

Stop lirc and record the keys from the remote you’d like to imitate:

sudo systemctl stop lircd_helper@lirc0
irrecord -d /dev/lirc0 ~/newlircd.conf

Follow the instructions in irrecord carefully and it will create a new file named newlircd.conf with the keys you just learned with it. You may want to use LIRC Key Names.

Here’s what my newlircd.conf came out looking like, I only learned the one key:


begin remote

  name  /home/osmc/newlircd.conf
  bits           16
  flags SPACE_ENC|NO_HEAD_REP|CONST_LENGTH
  eps            30
  aeps          100

  header       8464  4223
  one           549  1564
  zero          549   506
  ptrail        549
  gap          46191
  min_repeat      2
#  suppress_repeat 2
#  uncomment to suppress unwanted repeats
  toggle_bit_mask 0x0

      begin codes
          KEY_POWER2                0xC0E8
      end codes

end remote

I renamed the remote from "name /home/osmc/newlircd.conf" to "name JVC" to make it friendlier and added my existing remote to the newlircd.conf:



begin remote

  name  XBOX-ONE
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9061  4460
  one           596  1662
  zero          596   527
  ptrail        582
  repeat       9032  2232
  pre_data_bits   16
  pre_data       0x11B
  gap          107260
  toggle_bit_mask 0x0

      begin codes
          KEY_HOME                 0x26D9
          KEY_ZOOM                 0x7689
          KEY_TITLE                0xF609
          KEY_UP                   0x7887
          KEY_DOWN                 0xF807
          KEY_LEFT                 0x04FB
          KEY_RIGHT                0x847B
          KEY_OK                   0x44BB
          KEY_BACK                 0xC43B
          KEY_INFO                 0x649B
          KEY_VOLUMEUP             0x08F7
          KEY_VOLUMEDOWN           0x8877
          KEY_MUTE                 0x708F
          KEY_CHANNELUP            0x48B7
          KEY_CHANNELDOWN          0xC837
          KEY_REWIND               0xA857
          KEY_FASTFORWARD          0x28D7
          KEY_PLAY                 0x0EF1
          KEY_PREVIOUS             0xD827
          KEY_NEXT                 0x58A7
          KEY_STOP                 0x9867
      end codes

end remote

begin remote

  name  JVC
  bits           16
  flags SPACE_ENC|NO_HEAD_REP|CONST_LENGTH
  eps            30
  aeps          100

  header       8464  4223
  one           549  1564
  zero          549   506
  ptrail        549
  gap          46191
  min_repeat      2
#  suppress_repeat 2
#  uncomment to suppress unwanted repeats
  toggle_bit_mask 0x0

      begin codes
          KEY_POWER2                0xC0E8
      end codes

end remote

Start lirc back up:

sudo systemctl start lircd_helper@lirc0

Now, you can go into the remote section in My OSMC and browse to newlircd.conf in your Home folder to start using it.

If you like you can fire up irw to see if the keys you just learned are being recognized. Start irw and press a key:

osmc@osmc:~$ irw
164 0 KEY_POWER2 linux-input-layer
164 1 KEY_POWER2 linux-input-layer
164 0 KEY_POWER2_UP linux-input-layer

SENDING THE CODES

For this part you’ll need a IR LED hooked up to the GPIO pins on your Pi. I used GPIO17(Pin 11) and the Ground(Pin 9) right next to it. http://www.raspberrypi-spy.co.uk/wp-content/uploads/2012/06/Raspberry-Pi-GPIO-Layout-Model-B-Plus-rotated-2700x900.png

To send the codes I’m using irsend, but I have to specify the device I want to use because OSMC doesn’t use the default:

osmc@osmc:~$ irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE JVC KEY_POWER2

You’ll need to edit JVC to whatever you named your remote and KEY_POWER2 to whatever you named the key you want to send.

Next we have to decide how we want to send that key code to our device. I used a key on my main remote to trigger it. We’re going to use irexec to do it, so we need to make a quick config file for it.

sudo nano ~/.lircrc

and tell it what you want to do

    begin
        prog   = irexec
        button = KEY_HOME
        config = irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE JVC KEY_POWER2
    end

Any time I press KEY_HOME on my main remote while irexec is running, it will issue the command to send the power code to my TV.

Lastly, we need to auto-start irexec. I use cron to do this. Edit yours with crontab -e and add this line to auto-start irexec:

* * * * * ps aux|grep -v grep|grep -q irexec || /usr/bin/irexec -d &

As long as it’s running it will watch for you to press the key on your main remote and issue the command to send out an IR code when you do. I’ve had problems with irexec either crashing or closing unexpectedly since I started using it. The above updated line checks if irexec is running and starts it if it’s not.

We should probably explore the possibility of having irexec on by default or have a proper service for it.

2 Likes

Hi, unfortunately I have problems with this. I connected my LED like this. When I use irw, the keys are recognized. I added dtparam=gpio_out_pin=25 to the config.txt, so that I can use Pin 22 instead of PIN 11. When I use the command
irsend SEND_START samsung KEY_POWER2
or
irsend -d /var/run/lirc/lircd-lirc0 SEND_START samsung KEY_POWER2
nothing happens. When I look at the LED through a camera, I can’t see that it’s working. Because of this I think my LED is wired badly or the command doesn’t work.

Why are you using send_start instead of send_once? Can you post the lircd.conf you’re using?

I had some problems getting my led connected properly, I think I gave up on using the resistors and just wired it directly. Not sure what difference it makes.

Thanks for the fast reply :slight_smile: I am using start instead of once because I can then look at the LED with my camera and see if it lights up. And you didn’t have any issues with wiring it directly? I will try that if the software part is correct. Which end did you solder to GND? Also did you use a transistor? My lircd.conf looks like this:

begin remote

  name  XBOX-ONE
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       9086  4468
  one           613  1644
  zero          613   515
  ptrail        613
  repeat       9089  2211
  pre_data_bits   16
  pre_data       0x11B
  gap          108680
  toggle_bit_mask 0x0

      begin codes
          KEY_GREEN                0x26D9
          KEY_RECORD               0x7689
          KEY_LANGUAGE             0xF609
          KEY_UP                   0x7887
          KEY_LEFT                 0x04FB
          KEY_RIGHT                0x847B
          KEY_DOWN                 0xF807
          KEY_OK                   0x44BB
          KEY_BACK                 0xC43B
          KEY_YELLOW               0x649B
          KEY_VOLUMEUP             0x08F7
          KEY_VOLUMEDOWN           0x8877
          KEY_MUTE                 0x708F
          KEY_SUBTITLE             0x48B7
          KEY_BLUE                 0xC837
          KEY_PLAY                 0x0EF1
          KEY_STOP                 0x9867
          KEY_REWIND               0xA857
          KEY_FASTFORWARD          0x28D7
          KEY_NEXT                 0x58A7
          KEY_PREVIOUS             0xD827
      end codes

end remote

begin remote

  name  samsung
  bits           16
  flags SPACE_ENC|CONST_LENGTH
  eps            30
  aeps          100

  header       4560  4471
  one           597  1647
  zero          597   525
  ptrail        597
  pre_data_bits   16
  pre_data       0xE0E0
  gap          108132
  toggle_bit_mask 0x0

      begin codes
          KEY_POWER2                0x40BF
		  KEY_F1           0xD02F
      end codes

end remote



begin remote

  name  teufel
  flags RAW_CODES|CONST_LENGTH
  eps            30
  aeps          100

  repeat       9027  2214
  gap          107657

      begin raw_codes

          name KEY_F2
             9055    4396     614     534     621     534
              626     529     626     530     625     534
              621    1633     626     534     626     530
              621    1633     625    1634     625    1636
              624    1633     639    1620     621     534
              626    1633     621    1640     620     534
              621    1642     617    1638     621    1633
              626    1634     625     534     621     539
              616     534     621    1639     621     533
              621     534     621     534     626     529
              626    1638     620    1634     625    1633
              622

          name KEY_F3
             9050    4396     617     534     621     534
              626     529     626     529     652     503
              653    1609     624     529     651     504
              652    1607     626    1633     626    1634
              625    1633     622    1637     622     534
              621    1634     625    1638     622    1633
              626     529     652    1608     626    1633
              626    1633     626     529     626     529
              626     530     625     530     651    1608
              626     529     651     504     652     503
              652    1608     625    1633     626    1633
              626

          name KEY_F4
             9057    4389     625     529     626     531
              650     508     647     508     621     534
              622    1637     622     533     622     533
              622    1644     615    1638     621    1633
              626    1633     626    1633     630     525
              652    1607     626    1633     626     530
              625     530     625    1635     624    1633
              626    1633     626     529     652     504
              651     508     647    1608     625    1634
              626     529     652     504     653     506
              647    1608     625    1639     620    1634
              625

          

      end raw_codes

end remote

The path of the lircd.conf is \\OSMC\osmc under Windows 10 (where also the Movies, Music, Pictures, … folders are).

This is how I have my IR LED connected. Do you have your gpio configured in My OSMC/Pi Config? Not sure if that would prevent it from working, just trying to eliminate problems. Like, is Lirc trying to send the signal out on the wrong GPIO pin, for instance.

Edit:. Nevermind, just reread your earlier post, saw that you do have it configured.

Edit 2: One thing I noticed: GPIO pin 25 is a ground. While GPIO25 is actually pin 22. Could that be your issue?

Thanks again :slight_smile: I think dtparam=gpio_out_pin=25 should be correct, I read it in a manual. As far as I understand it, it changes the GPIO Pin directly, not the PIN. So to use GPIO25 or PIN22 I think this line is correct. I really think it is my hardware. I am having an issue with your link, I can only find the wiring for the sensor, not the IR LED.

Edit: I rewired everything like I showed it before and I can see the LED light up. But the range seems to be very low (under 3,5m), my TV doesnt care. But anyway a big improvement :slight_smile:

Edit 2: The range is even under 5cm :joy: I think there is something wrong… Sometimes I works for low distance, however.

Edit 3: Would be nice if you knew how you soldered it and what range you can achieve. I will play around with the hardware, it is definitely a problem there, was able to achieve 1m with one setup. Still weak.

Solution: I thought all BC547B Transistors have the same Base, Collector, Emitter position. But this is not the case! Reference to this Video and your data sheet to get your IR blaster to work. Seeing if it lights up does not necessarily mean what you did with the hardware is correct! Katze thanks for your help :slight_smile: