Skip to content

Instantly share code, notes, and snippets.

@billpatrianakos
Last active October 18, 2024 13:12
Show Gist options
  • Save billpatrianakos/cb72e984d4730043fe79cbe5fc8f7941 to your computer and use it in GitHub Desktop.
Save billpatrianakos/cb72e984d4730043fe79cbe5fc8f7941 to your computer and use it in GitHub Desktop.
How to set up LIRC on Raspbian Buster (2019, 4.x kernel)

Infrared and LIRC

Beware: These instructions have been cobbled together from all of the sources I found that got my specific unit working. I can verify it works on a Pi 3B running the latest OS as of summer 2019. Everyone's hardware and system will be a bit different so your mileage may vary. Before you go crazy trying to debug issues sending a signal, grab an existing lirc config for any remote and use the lirc CLI to send a signal out and verify your hardware is sending the signal by viewing the IR LED through a front-facing smartphone camera (almost all smartphone front cameras - even new iPhones - will not filter out IR light and you should see the LED flash when sending a signal). If you see your hardware is sending a signal and/or you can see that the IR input is seeing some signal, then you know your setup works and the issue may be that the remote you're trying to learn simply won't work with LIRC. I know for a fact that many Comcast/Xfinity cable remotes are actually RF devices and need to have IR mode turned on manually. Furthermore, I know for a fact that some TV and cable box remotes, for whatever reason, are too complex to work with LIRC. Try a very very simple remote before moving on to fancier devices.

In 2019 lirc_rpi, the Linux kernel module provided with Raspbian before, was replaced with gpio-ir and gpio-ir-tx. This tutorial is updated for Raspbian Buster. If you need to setup ANAVI Infrared pHAT on an older version of Raspbian, for example from 2018-04-18, please have a look at the old user's manual.

Setting up LIRC

Perform the steps below to build LIRC from source, to patch it and to enable the infrared receiver and transmitter on ANAVI Infrared pHAT:

  • Install dependencies
sudo su -c "grep '^deb ' /etc/apt/sources.list | sed 's/^deb/deb-src/g' > /etc/apt/sources.list.d/deb-src.list"
sudo apt update
sudo apt install -y vim devscripts dh-exec doxygen expect libasound2-dev libftdi1-dev libsystemd-dev libudev-dev libusb-1.0-0-dev libusb-dev man2html-base portaudio19-dev socat xsltproc python3-yaml dh-python libx11-dev python3-dev python3-setuptools
  • Download LIRC source code
mkdir ~/lirc-src
cd ~/lirc-src
apt source lirc
  • Apply a patch to fix LIRC for Raspberry Pi
wget https://raw.githubusercontent.com/neuralassembly/raspi/master/lirc-gpio-ir-0.10.patch
patch -p0 -i lirc-gpio-ir-0.10.patch
cd lirc-0.10.1
debuild -uc -us -b
  • Install LIRC (built on the previous step)
cd ~/lirc-src
sudo apt install ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb

NOTE: the installation is expected to fail the first time when you run it. After applying changes to some configurations LIRC will be installed again in the next steps.

  • Deploy LIRC configurations:
sudo cp /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf
sudo cp /etc/lirc/lircd.conf.dist /etc/lirc/lircd.conf
  • Edit /etc/lirc/lirc_options.conf and make sure that driver and device are set as:
driver          = default
device          = /dev/lirc1

NOTE: Device /dev/lirc1 is the receiver and device /dev/lirc0 is the transmitter. Initially /dev/lirc1 is used to scan a remote control. After that the configuration has to be updated to /dev/lirc0 in order to send infrared commands.

  • Edit /boot/config.txt (with sudo or as root) and configure kernel extensions by adding the following line to the end of the file:
dtoverlay=gpio-ir,gpio_pin=18
dtoverlay=gpio-ir-tx,gpio_pin=17
  • Run the LIRC installation again:
cd ~/lirc-src
sudo apt install -y --allow-downgrades ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb
  • Reboot Raspberry Pi:
sudo shutdown -r 0

Using IR Receiver

Follow the steps below to verify that the IR receiver is working as expected:

  • Stop LIRC systemd service:
sudo systemctl stop lircd
  • Start outputting raw data from the IR receiver
mode2 -d /dev/lirc1
  • Point a remote control at the IR receiver on ANAVI Infrared pHAT and press its buttons. If the IR receiver is configured successfully you will see similar output:
space 3662230
pulse 2428
space 594
pulse 1201
space 596
pulse 1230
space 595
pulse 1209
space 590
pulse 1204

Using IR LED

Follow the steps below to create LIRC configuration file and test the infrared transmitter:

  • Stop LIRC systemd service
sudo systemctl stop lircd
  • List all available names for buttons supported by LIRC:
irrecord --list-namespace
  • Type in the following command to create new LIRC control configuration file and follow the on screen instructions to scan a remote control:
irrecord -d /dev/lirc1 ~/lircd.conf

Example configuration output with name hifi:

Using driver default on device /dev/lirc1

irrecord -  application for recording IR-codes for usage with lirc
Copyright (C) 1998,1999 Christoph Bartelmus(lirc@bartelmus.de)

This program will record the signals from your remote control
and create a config file for lircd.

A proper config file for lircd is maybe the most vital part of this
package, so you should invest some time to create a working config
file. Although I put a good deal of effort in this program it is often
not possible to automatically recognize all features of a remote
control. Often short-comings of the receiver hardware make it nearly
impossible. If you have problems to create a config file READ THE
DOCUMENTATION at https://sf.net/p/lirc-remotes/wiki

If there already is a remote control of the same brand available at
http://sf.net/p/lirc-remotes you might want to try using such a
remote as a template. The config files already contains all
parameters of the protocol used by remotes of a certain brand and
knowing these parameters makes the job of this program much
easier. There are also template files for the most common protocols
available. Templates can be downloaded using irdb-get(1). You use a
template file by providing the path of the file as a command line
parameter.

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.

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)

Enter name of remote (only ascii, no spaces) :hifi
Using hifi.lircd.conf as output filename

Now start pressing buttons on your remote control.

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.
................................................................................
Got gap (45034 us)}

Please keep on pressing buttons like described above.
...............................................................................

Please enter the name for the next button (press <ENTER> to finish recording)
KEY_POWER

Now hold down button "KEY_POWER".

Please enter the name for the next button (press <ENTER> to finish recording)

Checking for toggle bit mask.
Please press an arbitrary button repeatedly as fast as possible.
Make sure you keep pressing the SAME button and that you DON'T HOLD
the button down!.
If you can't see any dots appear, wait a bit between button presses.

Press RETURN to continue.
..............................Cannot find any toggle mask.
You have only recorded one button in a non-raw configuration file.
This file doesn't really make much sense, you should record at
least two or three buttons to get meaningful results. You can add
more buttons next time you run irrecord.


Successfully written config file hifi.lircd.conf
  • Backup the original LIRC configuration file:
sudo mv /etc/lirc/lircd.conf /etc/lirc/lircd-backup.conf
  • Load the new configuration file, for example:

NOTE: The name of configuration depends on the selected name of remote. Please adapt the command below depending on your name of remote!

sudo mv hifi.lircd.conf /etc/lirc/lircd.conf
  • Switch LIRC configurations to the device for transmitting. Edit again /etc/lirc/lirc_options.conf and make sure that driver and device are set as:
driver          = default
device          = /dev/lirc0

NOTE: In this case the device is /dev/lirc0.

  • Launch LIRC systemd service again:
sudo systemctl start lircd
  • List all saved keys, for example:

NOTE: Please adapt the command below depending on your name of remote!

irsend LIST hifi ""
    • Test the configuration file by sending recorded IR command, for example POWER (please note the exact command may vary for different LIRC configuration files, IR devices and IR remote controls):

NOTE: Please adapt the command below depending on your name of remote!

irsend SEND_ONCE hifi KEY_POWER

NOTE: Please keep in mind that in rare cases some devices might require codes to be sent in short bursts, for example:

irsend send_once hifi KEY_POWER KEY_POWER KEY_POWER
@simanuel
Copy link

simanuel commented Aug 7, 2020

Hi Robin,

thank you so much for your efforts!
I'm able to install LIRC now, however, the step "Run the LIRC installation again" is resulting in errors now:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'liblirc0' instead of './liblirc0_0.10.1-6.2~deb10u1_armhf.deb'
Note, selecting 'liblircclient0' instead of './liblircclient0_0.10.1-6.2~deb10u1_armhf.deb'
Note, selecting 'lirc' instead of './lirc_0.10.1-6.2~deb10u1_armhf.deb'
liblircclient0 is already the newest version (0.10.1-6.2~deb10u1).
liblirc0 is already the newest version (0.10.1-6.2~deb10u1).
The following package was automatically installed and is no longer required:
  rpi-eeprom-images
Use 'sudo apt autoremove' to remove it.
Suggested packages:
  lirc-compat-remotes lirc-drv-irman lirc-doc lirc-x setserial ir-keytable
Recommended packages:
  gir1.2-vte
The following packages will be DOWNGRADED:
  lirc
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
Need to get 0 B/489 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 /home/pi/lirc-src/lirc_0.10.1-6.2~deb10u1_armhf.deb lirc armhf 0.10.1-6.2~deb10u1 [489 kB]
(Reading database ... 104354 files and directories currently installed.)
Preparing to unpack .../lirc_0.10.1-6.2~deb10u1_armhf.deb ...
Unpacking lirc (0.10.1-6.2~deb10u1) over (0.10.1-6.2~deb10u1) ...
Setting up lirc (0.10.1-6.2~deb10u1) ...
Job for lircd.service failed because the control process exited with error code.
See "systemctl status lircd.service" and "journalctl -xe" for details.
invoke-rc.d: initscript lircd, action "restart" failed.
* lircd.service - Flexible IR remote input/output application support
   Loaded: loaded (/lib/systemd/system/lircd.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-08-07 05:28:11 CEST; 21ms ago
     Docs: man:lircd(8)
           http://lirc.org/html/configure.html
  Process: 1551 ExecStart=/usr/sbin/lircd --nodaemon (code=exited, status=1/FAILURE)
 Main PID: 1551 (code=exited, status=1/FAILURE)

Aug 07 05:28:11 HomePi4 lircd-0.10.1[1551]: Info: lircd:  Opening log, level: Info
Aug 07 05:28:11 HomePi4 lircd-0.10.1[1551]: Notice: Version: lircd 0.10.1
Aug 07 05:28:11 HomePi4 lircd-0.10.1[1551]: Notice: System info: Linux HomePi4 5.4.51-v7l+ #1327 SMP Thu Jul 23 11:04:39 BST 2020 armv7l GNU/Linux
Aug 07 05:28:11 HomePi4 lircd-0.10.1[1551]: Info: Cannot open plugindir /usr/lib/lirc/plugins
Aug 07 05:28:11 HomePi4 lircd[1551]: Driver `default' not found or not loadable (wrong or missing -U/--plugindir?).
Aug 07 05:28:11 HomePi4 lircd[1551]: Available drivers:
Aug 07 05:28:11 HomePi4 lircd-0.10.1[1551]: Info: Cannot open plugindir /usr/lib/lirc/plugins
Aug 07 05:28:11 HomePi4 systemd[1]: lircd.service: Main process exited, code=exited, status=1/FAILURE
Aug 07 05:28:11 HomePi4 systemd[1]: lircd.service: Failed with result 'exit-code'.
Aug 07 05:28:11 HomePi4 systemd[1]: Failed to start Flexible IR remote input/output application support.
dpkg: error processing package lirc (--configure):
 installed lirc package post-installation script subprocess returned error exit status 1
Processing triggers for man-db (2.8.5-2) ...
Processing triggers for systemd (241-7~deb10u4+rpi1) ...
Errors were encountered while processing:
 lirc
E: Sub-process /usr/bin/dpkg returned an error code (1)

Tested on a fresh system again!
Thanks again for your help!

Greetings,
simanuel

@r-hede
Copy link

r-hede commented Aug 7, 2020

Hi @simanuel,

It's my fault, I completely forgot a step.

After the step Deploy LIRC configurations, you have to comment the line plugindir into the file /etc/lirc/lirc_options.conf:

The content of the file /etc/lirc/lirc_options.conf then becomes:

# These are the default options to lircd, if installed as
# /etc/lirc/lirc_options.conf. See the lircd(8) and lircmd(8)
# manpages for info on the different options.
#
# Some tools including mode2 and irw uses values such as
# driver, device, plugindir and loglevel as fallback values
# in not defined elsewhere.

[lircd]
nodaemon        = False
driver          = default
device          = /dev/lirc1
output          = /var/run/lirc/lircd
pidfile         = /var/run/lirc/lircd.pid
#plugindir      = /usr/lib/lirc/plugins
permission      = 666
allow-simulate  = No
repeat-max      = 600
#effective-user =
#listen         = [address:]port
#connect        = host[:port]
#loglevel       = 6
#release        = true
#release_suffix = _EVUP
#logfile        = ...
#driver-options = ...

[lircmd]
uinput          = False
nodaemon        = False

# [modinit]
# code = /usr/sbin/modprobe lirc_serial
# code1 = /usr/bin/setfacl -m g:lirc:rw /dev/uinput
# code2 = ...


# [lircd-uinput]
# add-release-events = False
# release-timeout    = 200
# release-suffix     = _EVUP

After that, you can go to the Run the LIRC installation again step and the error should be gone.

Best regards,
Robin Hède.

@simanuel
Copy link

Thank you so much, it's finally working!

@harvester-klirk
Copy link

Hey!
Thank you so much for the tutorial
i applied every step (including the updates which r-hede suggested )
i can record and everything is working fine until transmitting !
i double checked the connections "dtoverlay=gpio-ir-tx,gpio_pin=17"
i am using raspberry pi 4
please help !

@simanuel
Copy link

Have you changed the device to /dev/lirc0 for sending?

It's working for me, but sadly it's not as reliable as it was on my previous raspberry (before the changes).
Now i have to send commands again often, that really sucks... hope that will change in the near future.

@harvester-klirk
Copy link

thanks for the quick reply !
yes i have but still no luck
here's my connections
I am really in a dead end !
Thank you in advance!
unnamed

@pietchaki
Copy link

pietchaki commented Aug 28, 2020

Not all remotes are compatible or readable by LIRC. You may have a remote that either is using RF without you knowing it (I had a cable box remote that can be used in IR mode but defaults to RF) or for some reason just can’t be read by LIRC. My advice for situations like this is, assuming you got as far as setting up a working LIRC installation (which is seems like you did), try recording a different remote (the simpler the remote the better) or see if you can get a premade config file for the device you’re trying to use from the LIRC database. It looks like you have everything properly set up so I don’t think the issue is on the LIRC configuration end of things. I would think it’s something technical about how IR remotes work which is just outside my personal wheelhouse. Sorry I can't be of more help.

I have the same issue, where all buttons get the same code.
I'm pretty sure the problem is not the remote, as it's a kit to use with arduino and/or raspberry.
I'm using a KY-022 IR module, with a TL1838 sensor in it. It came with a simple small remote. I'm sure it is a IR remote, as it has no RF antenna inside, only the IR LED.
Also, I tested with 3 other remotes, from my old Samsung TV, Sony stereo and a generic DVD remote. This DVD remote I was using with LIRC on my old media PC and it worked without any problem there for 7 years. I used a simple serial IR adapter as described here: https://www.lirc.org/images/schematics.gif
PC was a very old and had a bunch of problems, so I changed to a rpi0w to stream from my local PLEX server.

EDIT:
Done the same steps again in a fresh RaspberryOS install and it worked.

PS.: In my install, there was no "/etc/lirc/lirc_options.conf.dist" file, it was already correctly named. Also, there was no failed install attempt.
PS2.: .deb filenames had changed, they are "_0.10.1-6.2" now

@pietchaki
Copy link

I believe it would be better to update the Tutorial and put the current instruction on an "_old" file.

@kangituser
Copy link

kangituser commented Sep 13, 2020

Hi all,

My name's Jonathan.

First of all thank you for this tutorial/wonderfull lib !

I've done the first step of installing LIRC.
That went through just fine. I need help recording signals. i've looked at harverster-klirk's comments in hopes to see a solution. since i have a similar setup (only the receiver is connected at this time, later i will connect the emmiter).

But as for receiving IR signals with mode2, i do see the terminal responds to this since i see the "Using device: /dev/lirc1" message, and the cursor thingy (have no clue what it's called) waits for input.

but, as i've only skimmed through these docs and myabe i missed a thing or two - i understoop just now that you use some kind of usb IR receiver/emmitter - whilst I'm using a war IR reciever component.
i have three outs :
Vcc, ground, signal.

I've put them in the co-responding pins (17 & others).
so i have no clue if this is not working due to the fact that i have implemented a different gpio connection that the usb dongle.

hope you've got some insights for me.

also - maybe it's been updated somwhere - but there have been some changes in the lib that i had to do manually (& that's fine) but i think that for future reference to other users things should be updated.

like the ./liblirc0_0.10.1-5.2_armhf.deb ./liblircclient0_0.10.1-5.2_armhf.deb ./lirc_0.10.1-5.2_armhf.deb files, at the moment after taking all the steps - the number is .1-6.2~deb10u1 so the code can't seem to recognize it.

there are a few more tweaks here & there i've done to compile the lib. i can't recall at the moment, but since this is an ongoing project i'll be doing this quite a lot and will be sure to specify what needs changes.

btw i'm using a RPI 4 with Raspbian Lite (atm i'm reading the old docs in hope to find some salvation to my issue).

thank you so much!

@hkrawczyk
Copy link

Hi there :) so I've installed Lircs with the provided instructions (big kudos!), but for some reason, I receive nothing on mod2 on /lirc1.
Any ideas about what might be the root cause? Have only one IR remote but sure it is IR cause when I put my finger over IR led its not working. It is Panasonic TV so should be pretty basic I guess.

I've followed all the steps from the instruction - and my hardware set up looks like that

@cbirchinger
Copy link

I just failed trying to learn a remote with "irrecord -d /dev/lirc1 ~/lircd.conf".

After the random button pressing phase it always ended up with the error "Cannot find any gap"
and the learned buttons after that had "0x0" as code.

What ended up fixing the issue was always a full reboot after setting the device to /dev/lirc1 (from lirc0)
As soon as lircd ran before with /dev/lirc0 (doesn't matter if it was stopped later), learning failed.

Maybe people getting 0x0 codes during the learning phase run into the same issue.

@thefantas
Copy link

Not work in Kernel 5.4.x

@billpatrianakos
Copy link
Author

I have had little success getting learning to work. Only the simplest remotes seem to work. Things like remotes for cable boxes are tough to get working and I recommend looking through the LIRC project's site to see if there's an existing config available for your specific device (there are a lot in there and in some cases you can get away with using a config for a device that's slightly different but in the same family like a cable box remote that's from the same manufacturer but not the exact model you have for example).

To check if LIRC is working at all, I would suggest using the phone camera trick. Get any lirc config online and then use the lirc CLI to output an IR signal manually. Use the front facing (selfie mode) camera on just about any phone, point it at the IR LED, run the commend to output a signal, and you should see the light visibly flash very quickly on your phone screen. That's the simplest and fastest way to see if you're getting output. As far as input, the command line should be able to tell you that when you test using mode2.

LIRC is very finicky and my instructions are incomplete. Use it as a starting point only. I had to cobble this together from multiple sources. Also remember that this is what worked on a Pi 3B back in 2019 using the latest OS available back then. Things may have changed.

This is by far the most talked about topic on any of my repos or gists and unfortunately it's the hardest to debug because everyone is going to be using slightly different hardware and the tutorials out there are not up to date (like this has now become).

But start with making sure the hardware is at least trying to send/receive signals, be sure to reboot after major config changes, and go from there. I wish I could be more helpful but that's all I have for now.

@dalepres
Copy link

dalepres commented Jul 26, 2021

I just wonder if anyone has used these instructions with a USB IR adapter such as irtoy2 or irdroid? Both worked with the old way in Raspbian Stretch but neither work now in Buster.

And just to be clear, I've followed these instructions from new images many times each for both modules but irrecord always times out without getting data and no IR found in a dark room using my camera for irsend though irsend completes without error.

One more thing, the receive LEDs flash on either device, whichever I am trying at the time, when I push buttons in irrecord.

@tscislo
Copy link

tscislo commented Dec 5, 2022

@billpatrianakos thanks for this tutorial.
However I have a problem with Raspberry PI 3 with Bullseye Raspberry OS.
Apparently with the newest version of lirc 0.10.1-6.3 this patch that you mentioned is already applied.

With my connfiguration when I run: mode2 -d /dev/lirc1
I can see IR codes normally:
image

However
irrecord -d /dev/lirc0 ~/lircd+ts.conf
Gives me incorrect output.
image

I read that with newest kernels LIRC is deprecated, have you had any success with LIRC on Bullseye?
What I should also mentioned that with Rapbian Jessie with the same hardware it all worked, but Jessie didn't use gpio

@Nosskirneh
Copy link

I read that with newest kernels LIRC is deprecated, have you had any success with LIRC on Bullseye?

Where did you read this? I've seen comments stating this, but nothing official. As far as I know, support for transmitting through IR has been added in the kernel without the need for LIRC, but I can't seem to find anything about invoking those function with the help of a library in C code or so, which LIRC supports and what I was looking to use it for.

@tscislo
Copy link

tscislo commented Dec 22, 2022

I failed with LIRC but I was successful with GPIO using this in abstraction in Python: https://pypi.org/project/PiIR/
Here is my code: https://github.com/tscislo/internet-radio-raspberry-pi/blob/master/threads/rcThread.py

@Nosskirneh
Copy link

Ah I see, guess that’s my only option too, but I would have loved to have this abstracted away from my own code. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment