### Infrared protocol analysis with PC soundcard

Infrared communication is widespread among electronic devices that use remote controls. Because of this, there are more protocols in use and the data (bytes) sent to the device depend on manufacturer. This post will show you how to view the waveform of an infrared signal emitted by a remote control, how to analyze and decode it. Then it will be possible to reproduce it. In this way you can program your universal remote control or your mobile device (smartphone with IR transmitter) with the right code for the best results.

Instead of using an oscilloscope for signal analysis, due to the low frequency of the IR bit stream, an ordinary sound card will be used to view the signal. The signal is amplified, filtered and demodulated by the IR receiver, so only the bit stream is sent to the sound card input. This is sampled at the usual audio rate and displayed in various software tools.

To build the IR adapter we'll use a common receiver module. This is preferred over an IR diode because it minimizes the number of parts needed. More than that, it demodulates the signal. The raw signal is modulated over a 38kHz carrier.
 How the signal appears at receiver output
These kind of modules can be found in any device that could be controlled using an IR remote. The part number is usually TSOP1738, SFH506, TSOP33138 etc. You will recognize them by the black capsule (sometimes placed in a metal frame) with three pins, located on front panels.

The three pins of the module are the power lines (VCC and GND) and one signal output line. The working voltage is usually 5V, but newer modules tolerate 2.5 to 5.5 V. Here is the schematic of the simplest sound card IR adapter.
This needs an external power supply. It can be powered from USB port too. The 1N4148 diode chops the signal to a maximum level equal to the voltage drop on this diode (0.5-0.7 volts). You can connect this directly to Line In or use a decoupling capacitor of more than 2 microfarads, electrolytic. Here is the breadboard prototype.
Let's connect this to computer. The signal can be split to feed both channels of the audio input.

## View the waveform

The simplest to use is Audacity. It is a cross-platform software that can record and display the signal.

 Audacity displaying an IR signal waveform
First you must select the right device (Line In in my case) (1) and optionally choose mono input. Do a few test records (4 and 5) and if the level is too low adjust it (2) - if there's no signal at all check input and adapter. Set the project sample rate to the audio card maximum value (3) - note that after the record is done, the real sample rate will be displayed in the bottom right corner. Now start zooming in (6) to see the actual protocol.

Using San Bergmans' IR Remote Control page (use the top right menu to see each protocol) I was able to tell that this is some kind of NEC protocol. And it is inverted (that's normal, the receiver module did this). Luckily, in Audacity, you can go to Effect menu and choose Invert to fix it. Now, if I analyze this it has the following meaning:

 IR waveform analysis and decoding
The waveform starts with a long burst needed by the receiver IC to calibrate its gain. Then the remote sends four bytes in total but only two or three are useful. First is the address byte. Then it should be the inverted address byte for error control or for newer devices, the second address byte (16 bit address). No matter what button you press on the same remote, address remains consistent. The third byte is the actual command and the fourth is the inverted command byte. NEC protocol shown in this example is sent with LSB first.

Some other software solutions for viewing, processing and recording the signal waveform are Soundcard Oscilloscope (Windows only), Nethome Protocol Analyzer (cross platform) and (Win)LIRC (cross platform). Note that with all these you must first choose the audio input device. Always choose the highest sample rate possible and preferably mono input. Note that some Windows versions disable audio devices when nothing is detected plugged in.

## Decode the signal

Decoding the signal can be performed with Nethome Protocol Analyzer, (Win)LIRC and SlyControl RCExplorer (Windows only). I'll emphasize on using LIRC because it outputs data ready for irplus, the Android remote control app.

Here is how to use LIRC (and its Windows equivalent WinLIRC).

On Linux it's easy to install LIRC. Just run sudo apt install lirc in a Terminal on Ubuntu distros. You'll be prompted to choose your hardware. I couldn't get the receiver working in Ubuntu with the ALSA audio input source so I built a serial port transceiver like this. To record the signals open a Terminal and run (the configuration file will be created in the directory you are running irrecord from):
irrecord -d /dev/lircd test.cfg
On Windows just extract the downloaded archive and start winlirc.exe. Click OK at the error message and when the Setup dialog appears choose AudioCapture.dll plugin. Configure it with the right input and the maximum sample rate. Click the Create Config button and close the CMD windows that appears. Click OK at the error message and when prompted to open the Setup dialog again select Cancel. Now go to plugins folder and open WinLIRC.ini. You should see the audio configuration in this file. If it doesn't exist try to configure WinLIRC again by launching it. Here's my audio configuration:
[AudioInputPlugin]
AudioDeviceName=Line in at rear panel (Blue) (R
AudioFormat=65536
LeftChannel=1
Polarity=2
NoiseValue=16
Now open a command prompt in the folder where winlirc.exe and irrecord.exe are and execute the command:
irrecord -d AudioCapture.dll ../test.cfg
All OSes
Follow on-screen instructions (press Enter a few times, then press and hold remote buttons and name them). After you finish open test.cfg with a text editor and find there the decoded bytes.

## Reproduce the signal

### Android

IRplus by Binarymode is probably the most versatile application of this type. Besides having a large database that you can contribute to, it can import LIRC files you recorded earlier. You can also edit straight from the app every button (command, text label and color). The default configuration files used by irplus are based on XML.

 irplus main screen showing an example remote control
To import the LIRC configuration file, go to Options (the top left icon), then tap on Import.

 Import LIRC file in irplus
More than this, irplus can be used with devices that don't have an IR blaster. You need a simple to make adapter plugged into the audio jack.