I recently got my hands on a IR Receiver and Transmitter module from a ELEGOO Upgraded 37 in 1 Sensor Modules Kit V2.0. I wanted to learn how the technology works and figure how to the IR transmitter with my Raspberry Pi 4.
EEVblog has an excellent video describing how the IR transmitter and receiver work in excellent detail. EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
The IR transmitter works by using OOK (on off keying) to modulate a carrier frequency. The carrier frequency is generated by producing a 32 kHz square wave that is transmitted by turning the transmitter LED on and off. A logical 1 is present when the carrier frequency is present, this is called a pulse. A logical 0 is present when the carrier frequency is absent, this is called a space. Data bits are encoded by a pulse of 600 us followed by either a space of 1600 us or 600 us. If a space of 1600 us follows the pulse then this is considered a 1. If a space of 600 us follows the pulse then this is considered a 0.
The key press from the IR transmitter has three parts: the header, pre-data, and payload. The header consists of a 9000 us pulse followed by a space of 4500 us. The pre-data is made up of 16 data bits that construct a value of 0x00ff. The payload is made up of 16 bits of payload data. This data is different for every button on the transmitter.
Raspberry Pi Setup
First the IR driver must be enabled. Un-comment the following line in the file /boot/config.txt
Then make sure to install the lirc package. The Linux Infrared Remote Control. This is used to provide user space access to the IR driver and allow for easy integration into desktop tools.
The tool provides a debug executable to see raw data coming in from the infrared receiver. This program is called mode2 and output the values for all the pulses and spaces the driver receives.
LIRC provides a tool called irrecord which is used to make a configuration file for a remote transmitted and map all the payload values to a specific key value. This program unfortunately did not work for the several IR remotes I had lying around the apartment.
I instead created a python program to parse the output from mode2 to generate the config file. Here is the output of the python script. It decodes the payload for each button press.
Now that I have the payload of each key press from the IR transmitter I can create the configuartion script that lirc needs to process the raw payload into events that the rest of the OS can listen for. I copied a configuration script from another remote and modified the payload to match the keys. Here is the result.
header 9008 4466
one 598 1645
zero 598 550
repeat 9005 2204
This config file then just needs to be copied to /etc/lirc/lircd.conf.d with a file name of elegoo.lircd.conf and then the irw executable can be used to process key press events.
0000000000ff30cf 00 KEY_1 elegoo
0000000000ff30cf 01 KEY_1 elegoo
0000000000ff30cf 02 KEY_1 elegoo
0000000000ff30cf 03 KEY_1 elegoo
0000000000ff38c7 00 KEY_5 elegoo
0000000000ff38c7 01 KEY_5 elegoo
0000000000ff38c7 02 KEY_5 elegoo
0000000000ff5aa5 00 KEY_6 elegoo
0000000000ff5aa5 01 KEY_6 elegoo