Easy Setup IR Remote Control Using LIRC for the Raspberry PI (RPi) - July 2019 [Part 1]

Introduction: Easy Setup IR Remote Control Using LIRC for the Raspberry PI (RPi) - July 2019 [Part 1]

After much searching I was surprised and dismayed about the conflicting information on how to setup IR remote control for my RPi project. I thought it would be easy but setting up Linux InfraRed Control (LIRC) has been problematic for a long time but much changed in June 2019 with the release of the Buster version of Raspbian making much of the tutorials out there worthless. Many tutorials have you create a hardware.conf file but LIRC does not need nor want it and the lirc-rpi module has been replaced with the gpio-ir module.

The tutorial assumes the RPi is running raspbian (version Buster Jun 2019). Also a working knowledge of the terminal, basic commands from the command prompt and editing text files is assumed.

NOTE: LIRC setup will fail if run as Root user and lead to the dreaded "cannot import name RawConnection" error when running python code. This tutorial assumes you are logged in as a user (ie: PI)

Goal: To enable IR remote control of RPi projects

1) Setup LIRC on RPi [Part 1]

2) Demonstrate LIRC using python [Part 2]


Supplies:

--- Raspberry Pi (3,4,zero)

--- Dupont wire (female-female)

--- VS 1838b IR receiver

Step 1: Assemble Hardware

Using the VS1838b IR receiver couldn't be easier. Just hook up the sensor to the RPi directly with Dupont wire (Female-Female). You could also use a breadboard (not shown) or solder on a protoboard (photo)

Looking at the VS1838b IR Receiver from the front (with the big X facing you)

---- Left leg is out

---- Center leg is ground

---- Right leg is 3.3v

1) Attach the left leg to BCM pin 17 on the RPi (yellow wire)

2) Attach the center leg to ground (black wire)

3) Attach the right leg to 3.3v (red wire)

Step 2: Install Lirc

1) Open terminal window and install LIRC. Be forewarned that this will likely raise an error "Failed to start Flexible IR remote input/output application support" as the installed files now have .dist appended and the suffix must be removed as noted below. Not hard but frustrating.

$ sudo apt-get update

$ sudo apt-get install lirc

----DON'T WORRY! as this will likely raise an error "Failed to start Flexible IR remote input/output application support" as the .dist suffix needs to be deleted from lirc_options.conf. Just rename the file as shown.

$ sudo mv /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf

2) Reinstall lirc now that the lirc_options.conf file has been renamed

$ sudo apt-get install lirc

Step 3: Edit Lirc_options.conf

Edit /etc/lirc/lirc_options.conf as follows by changing these two lines:

:

:

driver = default

device = /dev/lirc0

:

:

Step 4: Perform Move to Remove .dist Suffix From Lircd.conf.dist

Remove suffix .dist from /etc/lirc/lircd.conf.dist

$ sudo mv /etc/lirc/lircd.conf.dist /etc/lirc/lircd.conf

Step 5: Edit Config.txt

Edit /boot/config.txt by adding one line in the lirc-rpi module section as follows. This example assumes the RPi is 'listening' on BCM Pin 17 for the IR receiver but any RPi IO pin can be used. I have not tried it yet but if you want to send commands from the RPi then add and uncomment the 4th line shown below to send IR commands on BCM pin 18

:

:

:

# Uncomment this to enable the lirc-rpi module

#dtoverlay=lirc-rpi

dtoverlay=gpio-ir,gpio_pin=17

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

:

:

:

Step 6: Check Status and Reboot

1) Stop, start and check status of lircd to ensure there are no errors!

$ sudo systemctl stop lircd.service

$ sudo systemctl start lircd.service

$ sudo systemctl status lircd.service

2) Reboot

$ sudo reboot




Step 7: Test Remote

This step assumes you have have a IR receiver hooked up to your RPi on the pin specified in config.txt.

1) stop LIRCD service and test remote using mode2 command

$ sudo systemctl stop lircd.service

$ sudo mode2 -d /dev/lirc0

3) Point the remote at the receiver and press some buttons. You should see something LIKE this:

:

:

space

pulse

:

:

4) Press Ctrl-C to exit

5) Your IR receiver is setup and ready to proceed to Part 2 and be accessed in python.

3 People Made This Project!

Recommendations

  • Hand Tools Only Challenge

    Hand Tools Only Challenge
  • Modify It Speed Challenge

    Modify It Speed Challenge
  • Remix Contest

    Remix Contest

33 Discussions

0
migas.eust
migas.eust

Question 14 days ago

Hello, I've been trying to get lirc working for the past few weeks and I cant't manage to do it. I've already read countles tutorials and reset my pi many times and couldn't get anywhere.
This is one of the most simple ones I've seen and I'm still messing something up. Could anyone help me?
I've followed the steps very carefully and there are a few things that were different for me.
I'm using a Raspberry Pi 4B and the last Raspberry OS with Linux raspberrypi 5.4.51-v7l+ #1333 SMP Mon Aug 10 16:51:40 BST 2020 armv7l GNU/Linux.
When I first install Lirc there are no errors like there suppose to be and there was no /etc/lirc/lirc_options.conf.dist but there was already a /etc/lirc/lirc_options.conf so i skipped that step.
There was no problem then until I get to sudo mode2 -d /dev/lirc0, which the output was:
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Please use the --raw option to access the device directly instead through the abstraction layer

I've checked the pins and config.txt and they're fine. Can someone give me some clarity pls?

0
ymltej
ymltej

Question 5 weeks ago

$ sudo mv /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf after this command i am getting an error "mv: cannot stat '/etc/lirc/lirc_options.conf.dist': No such file or directory" can you please help me. As the above step is blocking me from proceeding further.
Thanks in advance

0
Rich101101
Rich101101

Answer 5 weeks ago

This command simply renames a file. Use the file manager to navigate to the folder and inspect the directory contents. Either the file is missing (and would then start over) or a typo.

0
Dandy_Don
Dandy_Don

2 months ago

I have some of this working.... I need some help, please.

I installed lirc correctly. When I enter sudo mode2 -d /dev/lirc0 I get the following OUTPUT:
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Using device: /dev/lirc0
Running as regular user pi

When I aim my IR LED at the receiver, I get:
space <numbers>
pulse <numbers>
space <numbers>
pulse <numbers>
.
.
.

The issue I'm having is the space & pulse numbers never repeat. I thought the space & pulse numbers would repeat if the same button was pushed repeatedly.....

I can connect my IR receiver up to an oscilloscope & capture the output. I see a pulse train that I can't figure out. See attached image.

The IR LED comes from a laser tag gun. The pulse train is the same each time I pull the trigger.

I tried a TV remote aimed at the IR receiver & the oscilloscope trace shows multiple pulses with spaces between each section, as expected. The 'code' is different depending on which button is pressed.

So, in theory, the laser tag gun should output a single 'code' every time it is fired. But when I aim the gun at the RPi (IR receiver connected to pin 17) I get random numbers....

I'm confused.....

Any suggestions?

team1.png
0
Dandy_Don
Dandy_Don

Reply 2 months ago

Thanks for your quick response!!

i tried the Python program in part 2.... I get an error when I run the program:

ImportError: cannot import name 'RawConnection'

I'm not sure what's causing this error.....

Also, I don't have a remote control config file for the laser tag gun.... Based on your instructions, it is difficult to create a file for the laser tag IR LED.

Any other suggestions?

0
Rich101101
Rich101101

Reply 2 months ago

Not sure this will help but.... The import error means the module is not present for your active python version. Look to make sure the module is present for your python version or just Try compiling for Python2 or Python3.

0
Dandy_Don
Dandy_Don

Reply 2 months ago

I'm using Geany to code the Python & I told it to use Python3.

Where do I look to see if the module is present?

0
Dandy_Don
Dandy_Don

Reply 2 months ago

I am able to get the codes for the laser tag gun using another python program.

"TEAM1": 2945, 6037, 2945, 2041, 955, 2041, 955, 2041, 955, 2041, 955, 2041, 1952, 2041, 955, 2041, 955
"TEAM2": 2945, 6037, 2945, 2041, 955, 2041, 955, 2041, 955, 2041, 1952, 2041, 955, 2041, 955, 2041, 955

I still can't get LIRC working, however....

Going back thru the steps above, I get an error with the line in Step 1:
$ sudo mv /etc/lirc/lirc_options.conf.dist /etc/lirc/lirc_options.conf

My pi says no such file or directory '/etc/lirc/lirc_options.conf.dist'

I think this might be the problem, but I'm not sure how to fix it..... the first 2 lines in Step 1 work:
$ sudo apt-get update
$ sudo apt-get install lirc

After install & setup, I enter:
sudo /etc/init.d/lircd status
This shows LIRC is active & running. It says
"Info: Cannot configure the rc device for /dev/lirc0"
"Error: error in configfile at line 2"
"Error: reading of file '/etc/lirc/lircd.conf' failed"

Maybe I don't have the config file set up right......

0
Rich101101
Rich101101

Reply 2 months ago

Unfortunately I was unable to get IRRecord to work which is suuposed to create a config file by recording commands from your IR device. I hzve not tried this but This link may be helpful.
[Stretch/Buster] Using LIRC with kernel 4.19.X and gpio-ir - Raspberry Pi Forums
https://www.raspberrypi.org/forums/viewtopic.php?t=235256

0
Dandy_Don
Dandy_Don

Reply 2 months ago

I am using kernel 4.14.98-v7. Should I upgrade to 4.19?

0
Gottfrid
Gottfrid

11 months ago

It worked quite good until I run the command "sudo mode2 --raw -d /dev/lirc0".
I got the following response in LXTerminal (I using RPI4):
---
Using raw access on device /dev/lirc0
Problems: this device is not a LIRC kernel device (it does not
support LIRC_GET_REC_MODE ioctl). This is not necessarily a
problem, but mode2 will not work. If you are using the --raw
option you might try using without it and select a driver
instead. Otherwise, try using lircd + irw to view the decoded
data - this might very well work even if mode2 doesn't.
---
I have no idea how to come further. What shall I do in orderto be able to collect the IR signals fro my IT remote controll?

0
Rich101101
Rich101101

Reply 11 months ago

It appears that You typed in the wrong command. As noted in the tutorial the correct command is:
$ sudo mode2 -d /dev/lirc0

0
Gottfrid
Gottfrid

Reply 11 months ago

Sorry I inserted the wrong input and outputs fromto/from my RPI4. What I inserted was my effort after the initial problem. Below is what problem I initially got after following the article above.
My command:
sudo mode2 -d /dev/lirc0

Reply in LXTerminal:
Using driver default on device /dev/lirc0
Trying device: /dev/lirc0
Please use the --raw option to access the device directly instead through the abstraction layer

What does it mean that RPI4 ask me to use raw options? Apperently it does not help to insert --raw in the mode2 command.

0
Rich101101
Rich101101

Reply 11 months ago

According to LIRC.org raw mode is used if there is no driver installed.

"When given the correct driver mode2 should always work. Without a driver, the --raw option should always work for /dev/lirc* devices."

http://www.lirc.org/html/mode2.html

Your problem suggests the driver is not configured correctly. Please review ALL of the previous steps (and particularly STEP 3). Then recheck the configuration as outlined in Step 6. There should be NO errors or warnings before procedding to Step 7.

0
Leon5x
Leon5x

Reply 10 months ago

I have the same problem as Gottfried. I have reviewed all the previous steps and there are no errors or warnings in the lircd service status. I even tried to set it up on a newly installed raspbian, which didn't work. I am using an Raspberry Pi 4, did you also use one?

Edit: I found the solution to the problem:
I wanted to use the IR LED as well. After commenting out the line in the /boot/config.txt for the LED (#dtoverlay=gpio-ir,gpio_pin=XX) and rebooting, mode2 worked again. I have no idea why, but several reboots and commenting/uncommenting the gpio-ir lines helped also bringing the LED back to work.
Does anybody have any idea why it doesn't work or how to get both working together?

Edit: Fixed typo

0
therealitem
therealitem

Reply 9 months ago

Thanks for that comment. It saved me a huge headache.
Did you in the end manage to get both sending and receiving to work?

0
Leon5x
Leon5x

Reply 9 months ago

I didn't try to get it working, because I am happy with only the IR led working, I don't need the IR receiver.
But if you manage to get it running I would be happy to know how :)

2
therealitem
therealitem

Reply 9 months ago

I got it to work.
The problem was that when tx is enabled lirc0 is used to send. The receiver is then lirc1.
Try:
sudo mode2 -d /dev/lirc1

0
brcent
brcent

Reply 3 months ago

I looked everywhere on how to fix this and this worked. Thanks