Infrared (IR) communication is one of the lowest-cost ways to control electronics. Additionally, a Raspberry Pi is one of the cheapest ways to explore electronics.
The Raspberry Pi microcomputer has built-in support for IR communication in the form of LIRC which stands for Linux Infrared Remote Control.
However, for anybody that wants to interact with infrared communication directly (e.g. in a Python program) there are not many options.
For instance, if someone wanted to use a vintage remote control to control a webserver, LIRC would be less than ideal.
There are a variety of infrared protocols in existence. A commonly used one is the NEC Infrared Transmission Protocol.
This protocol is robust and a good place to start for anybody interested in learning how IR communication works.
The complete message (which is sent when a button is pressed) consists of the following (in order):
So an example command would look like:
This image shows the command
16 sent to the address
59 (both in hex).
Mapping the bits to the actual command can be tricky as they are sent in binary, with the least significant bit first.
The logical inverse of the address and command is sent with each frame for error correction purposes.
There are also other messages that can be transmitted for repeat buttons or if the controller is operating with the "extended" NEC protocol (which doesn't use the logical inverse of the address).
As an IR remote only flashes light, a logical question would be, how are the flashes interpreted as numbers? IR communication uses 38khz modulation and the length of time in a burst determines which binary digit is being sent. Graphically this looks like: Where the longer spaces between pulses indicate a 1 and shorter spaces indicate a 0.
Because the command and address inverses are sent with each frame, each message should take the same amount of time to transmit.
The protocol itself is very straightforward. The most challenging aspect was dealing with the timing tolerances from both the remotes and and from the Raspberry pi.
During research the following equipment was used:
This end result of this exploration was the irreceiver Python library which is available on PyPi. It is fully tested and well commented and a good place for someone interested in using IR communication with a Raspberry Pi or just interested in exploring the protocol more.
All images and video in accompanying this post are courtesy of SB-Projects which is a great resource for this topic.