Learning IR Codes with Raspberry


Read about InfraRed communication. In brief, we transfer data through IR light. Continuous On-Off generates binary stream. IR Transmitter and IR Receiver are two modules that enable this communication.

This is part of something large that I was trying to build and I needed a basic IR transceiver system that, after much experimentation, I decided to build with a Raspberry Pi because of high memory requirements of my solution.

First thing,

Setup Raspberry with Noobs, setup for remote access with a USB WiFi dongle. Remote login, since UI access is not needed anymore and

# updates
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo rpi-update

sudo apt-get clean

Some of the commands above may be redundant, I didn’t bother to look into them and understand. Good to be on top of tree…


Install LIRC

sudo apt-get install lirc
#Add following to /etc/modules
lirc_rpi gpio_in_pin=23 gpio_out_pin=18

GPIO Pin 22 corresponds to Pin 15 and GPIO Pin 23 corresponds to Pin 16 on RPi. All the configurations use GPIO pin numbers. Below PinOut diagrams from www.raspberrypi.org

Physical Pin Numbersphysical-pin-numbers

GPIO Pin Numbersgpio-numbers-pi2

Continue with changes…

#following changes to /etc/lirc/hardware.conf
#uncomment and update /boot/config.txt
#restart service to test
sudo /etc/init.d/lirc restart
[ ok ] Restarting lirc (via systemctl): lirc.service.


Keyes IR components PinOut

WP_20160726_23_23_20_Rich_LI (2)


$ sudo /etc/init.d/lirc stop
[ ok ] Stopping lirc (via systemctl): lirc.service.
$ mode2 -d /dev/lirc0

Point a remote to Rx module and press a key, on receiving IR signals an onboard LED blinks on the Rx module. You will see similar output as below

space 16777215
pulse 7704288
space 169
pulse 106
space 1903
space 10811
pulse 33
space 287800
pulse 103351

You need to sanitize these codes and then play them through your transmitter. There are tools to help learn and play these codes.

Note: Codes for a lot of remotes has been recorded and contributed by community and can be downloaded from Lirc Remotes. These needs to be copied to /etc/lirc/lircd.conf.

$ irsend --help
Usage: irsend [options] DIRECTIVE REMOTE CODE [CODE...]

SEND_ONCE         - send CODE [CODE ...] once
SEND_START        - start repeating CODE
SEND_STOP         - stop repeating CODE
LIST              - list configured remote items
SET_TRANSMITTERS  - set transmitters NUM [NUM ...]
SIMULATE          - simulate IR event

Name of remote whose configuration to use from lircd.conf

Key code as saved in lircd.conf


irsend might throw errors, you may want to run lircd -n to get details, -d if error says “could not get file information for /dev/lirc

$ sudo lircd -n -d /dev/lirc0
lircd-0.9.0-pre1[15822]: lircd(default) ready, using /var/run/lirc/lircd
lircd-0.9.0-pre1[15822]: accepted new client on /var/run/lirc/lircd
lircd-0.9.0-pre1[15822]: removed client
lircd-0.9.0-pre1[15822]: accepted new client on /var/run/lirc/lircd
lircd-0.9.0-pre1[15822]: removed client

Finally, to confirm if IR LED is blinking, see it through your phone camera! (most WebCams have IR Filters, phone cameras don’t).

Posted in: IoT

One thought on “Learning IR Codes with Raspberry

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s