Friday, December 25, 2015

HOWTO - Create a Raspberry Pi Infrared remote control

If you have the need to control all your home equipment which has a remote available, look no further, the Raspberry Pi is your ultimate low-cost universal remote control tool available.

My Raspberry Pi Infrared Remote Control Appliance

When I have some time to spare, you can find me at home messing around with Linux, Raspberry Pi's and some home automation domotics.
Recently I was looking to extend my home domotics system to be able to switch every device which has a remote control in my house.  
When I started searching for a solution, I was suprised how easily it was to build a solution consisting of a Raspberry Pi and some locally acquired electronic components.

Before I start explaining how to build the solution, let me first give u an overview of all the components which I used to build my remote.

Putting the Hardware together.

Component List:

1x Raspberry Pi, model B+ (look on second hand market, or internet, can be acquired for as low as 20€)

1 x Transistor BC557C TO92 KEC
Conrad article: 155959
Price: 0,1 € / piece

4 x IR-LED 5mm HE3-290AC
Conrad article: 181713
Price: 0,35 € / piece

1 x IR Receiver 2,7-5,5v OS-0038G
Conrad article: 184296
Price: 0,67 € / piece

100 x Resistor 56 Ohm 1/4w (you only need 4)
Conrad article: 408018 - 89
Price: 3 € / 100 pcs

The Schematics:

To make it able to send and receive with our home built remote control, we  have to connect two circuits to our Raspberry Pi, one send circuit and one receive circuit.
The send circuit consists of a few parts while the receiver circuit (the ir receiver) can be soldered directly onto the raspberry.

The send circuit:

Since there are not too many components in the send circuit, no separate print plate is necessary to solder the components on.  
I took my Raspberry Pi housing, drilled 4 holes in it to fit my IR leds, glued these to the casing with epoxy, soldered a resistor on each led's anode, then lead two wires to my Raspberry Pi board where the leds cathode wire was soldered to the emitter of my transistor and the anode wire directly to pin 2 on the Raspberry Pi board (+5v).
I had soldered the collector of my transistor directly to pin 9 (GND) and the Base of my transistor to pin 11 (GPIO17)

The receive circuit:

The IR receiver can be soldered directly onto the Raspberry Pi's pins 4 (+5v) - 5 (GND) - and 12 (GPIO18) respectively.

Configuring the Software.

The Raspberry Pi operating system.

Take the latest Raspbian version, as of writing, this is Raspbian Jessie from November 2015, download it from the Raspberry foundation website, put it to SD card and start off.

The Infrared software driver.

The linux platform (Raspbian in this case) already has packaged support for lirc - infrared control software for linux.
To interface this software with the hardware on the Raspberry Pi, a driver was written called lirc_rpi.  
This driver is provided as an overlay kernel snapshot file which needs to be enabled in the configuration of the Raspberry Pi.  Once enabled it loads when the Raspberry Pi boots up, no further loading via modules or modprobe is necessary.

To enable the lirc_rpi driver, we need to make a modification in the /boot/config.txt file

In the config file there is an option called: 

It needs to be uncommented and a small addition needs to be made to invert the IO logic to make this work properly.  The config file is on a read only partition, so we first need to remount it read/write to edit the config file.

  • sudo mount -o remount rw /boot
  • sudo nano /boot/config.txt
  • remove: #dtoverlay=lirc-rpi
  • add: dtoverlay=lirc-rpi,invert=on
  • Ctrl-x to save

Reboot your Raspberry Pi, it will load the lirc-rpi driver on boot now.

  • sudo reboot

The infrared control software.

As stated previously, the linux platform (Raspbian in this case) already has packaged support for lirc - infrared control software for linux, setting this up in Raspbian is just installing a package.

  • sudo apt-get -y install lirc
This will set up the required binaries and services for our remote control.
For a detailed description and manuals about this software, you can visit the lirc project website

configure your lirc hardware config file to reflect below configuration:

# /etc/lirc/hardware.conf
# Arguments which will be used when launching lircd

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

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

#Try to load appropriate kernel modules

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

# Default configuration files for your hardware if any

The binaries that we will be using for our remote control are:
  • mode2 -> it can visualise ir reception on our ir sensor
  • irrecord -> this binary can sample keys from existing remote controls and record them to a config file
  • irsend -> send out ir control signal
Once the software is installed, a daemon called lircd will be running, it reads it's configuration from /etc/lirc/lircd.conf - this file contains our remote control defenitions that we can send out amongst some other stuff.
To work manually with the above mentioned binaries we first need to stop the lircd daemon because it will interfere with our work otherwise:
  • service lircd stop
To make sure our ir receiver is working properly, we will perform a small test, enter below command and point a remote control to your ir sensor and press a button, it should display remote control codes:
  • mode2 -d /dev/lirc0

Once verified that our ir sensor is working, we can now start recording existing remote control buttons, launch below command and follow instructions, it will record a configuration file which we can feed to our ir daemon afterwards.

  • irrecord -d /dev/lirc0 ~/lircd.conf
Once recorded, copy your remote control file to /etc/lirc/lircd.conf and start your lircd service:
  • service lircd start
Once that the daemon is running with the recorded remote control configuration, you should be able to send out IR remote control codes, U can do a test by using below example.
  • irsend -# 2 -d /var/run/lirc/lircd SEND_ONCE telenet KEY_OK (read irsend manpage)

Now that we have configured the Infrared hardware and software, it's time to make it a bit more user friendly by installing a website for all this so we can easily use the remote control from any device which has a web browser.
There is a nice project on Github written by Alex Bain which just does all this:

As a prerequisite you will have to install node.js webserver on your Raspberry Pi

Install Node.js
Download Node.js source

Raspberry Pi Model A, B, B+ and Compute Module
tar -xvf node-v4.0.0-linux-armv6l.tar.gz
cd node-v4.0.0-linux-armv6l

Raspberry Pi 2 Model B
tar -xvf node-v4.0.0-linux-armv7l.tar.gz
cd node-v4.0.0-linux-armv7l
Copy to /usr/local

sudo cp -R * /usr/local/

Verify the version of your node installation with:
node -v

cd ~/
sudo npm install -g lirc_web

Test the website by running it manually:

sudo apt-get install -y nginx
curl | sudo tee /etc/nginx/sites-available/default

sudo service nginx restart