Raspberry Pi LIRC IR Remote

Posted on Thu 28 June 2018 in RaspberryPi

I had just finished building a 433MHz remote control, and also needed an infra-red controller to complete the PiHome remote. I'd built one before so had a rough idea of what needed doing.

Before you get started, this is a guide explaining how to use an IR transmitter and receiver. I ended up using neither.

I used my existing Broadlink device instead, it's more powerful.

If you're doing a close range or for fun project read on, else, read this:

Pi Home 433MHz

Credits to Alexba.in and sanchezjjose of instructables for LIRC install guides.

alexba.in

Instructables

• Usual Pi setup
• Install LIRC
• Build Circit
• Scripts

You'll need some components:

• LED Transmitter
• LED Receiver 
• PN2222 Transistor
• 10k Ohm Resistor
• Breadboard Wires
• Breadboard

Amazon and eBay are my go to places. They're cheap, but need to be purchased in multiples. Hence, I had the bits I needed laying around.

You can get starter kits for under a tenner. Usually well overpriced, but will have the things you need to keep you going for a while.

Install LIRC

sudo apt-get install lirc

Edit /etc/modules

sudo nano /etc/modules

Add the below to the end

lirc_rpi gpio_in_pin=23 gpio_out_pin=22

Change your /etc/lirc/hardware.conf to the below

########################################################
# /etc/lirc/hardware.conf
#
# Arguments which will be used when launching lircd
LIRCD_ARGS="--uinput"

# Don't start lircmd even if there seems to be a good config file
# START_LIRCMD=false

# Don't start irexec, even if a good config file seems to exist.
# START_IREXEC=false

# Try to load appropriate kernel modules
LOAD_MODULES=true

# Run "lircd --driver=help" for a list of supported drivers.
DRIVER="default"
# usually /dev/lirc0 is the correct setting for systems using udev
DEVICE="/dev/lirc0"
MODULES="lirc_rpi"

# Default configuration files for your hardware if any
LIRCD_CONF=""
LIRCMD_CONF=""
########################################################

Restart LIRC

sudo /etc/init.d/lirc stop

sudo /etc/init.d/lirc start

Edit your /boot/config,txt and add:

dtoverlay=lirc-rpi,gpio_in_pin=23,gpio_out_pin=22

Reboot the Pi and break out your components.

Wiring Up

Check one of the links above for really detailed schematics and pictures, I've tried my best.

Essentially you want power and ground with a resistor between the PN2222 base pin and the power.

The 433MHz remote is still hooked up.

alt text alt text alt text alt text

Recording Remotes

Two things to note before you start, hundreds have been here before you. They may also have been kind enough to upload the remote. check github https://github.com/probonopd/lirc-remotes

You'll also need to check the namespace. When recording a key, you need to use a pre-defined name. Check the namespace.

LIRC Namespace

I have a 21 button remote, as well as the obvious KEY_POWER and KEY_BRIGHTNESSDOWN, I used the alphabet for most.

Once everything is wired up

sudo /etc/init.d/lirc stop

mode2 -d /dev/lirc0

This will stop lircd and start the raw data output. Point your IR remote (quite close) and mash some buttons. You should see codes start appearing. If you don’t double check your wiring.

Start recording a remote

When you're recording remotes, use a one second push. Not a push and hold, not a quick tap.

Stop lirc 
sudo /etc/init.d/lirc stop

Create a new remote, save output to ~/lircd.conf
irrecord -d /dev/lirc0 ~/lircd.conf

Backup the original config file
sudo mv /etc/lirc/lircd.conf /etc/lirc/lircd_original.conf

Copy new configuration file
sudo cp ~/lircd.conf /etc/lirc/lircd.conf

Start lirc 
sudo /etc/init.d/lirc start

Testing and Scripting

I called my remote Jayool, it had that written on it. Use the irsend command as below, replacing name/key.

This is where it my get tricky, it did for me. Direct sunlight, objects, line of sight - all play a part in how effective evrything will be.

Also, you can't see IR. Point your phone camera to the LED, and run the commands you'll see a purplish light to confirm its working.

irsend SEND_ONCE Jayool KEY_POWER

irsend SEND_ONCE Jayool KEY_POWEROFF

As I said at the beginning, I didn’t end up using this, I use my Broadlink RM Pro instead however if you wanted to create a python script it may go something like this:

import os
os.system ("irsend SEND_ONCE Jayool KEY_POWER")