KY-022 Infrared receiver module

From SensorKit X40 Wiki

Revision as of 19:07, 20 February 2017 by Webmaster Wiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



Technical data / Short description

Carrier frequency: 38kHz Can receive infrared signals and gives it as a digital signal out.

Additional to that the LED of the module will short flash up if a infrared signal was detected.



Code example Arduino

With help from the sensor modules KY-005 and KY-022 you can build up an infrared-remote + infrared-receiver system. For this, you need additional to the two modules, two Arduinos. The first one will send the signal and the other one will receive the signal and shows it at the serial terminal.

An Additional library will be needed for the following code example:

- [Arduino-IRremote] from Ken Shirriff | published under LGPL

The library is in the package and needs to be coopied to the arduino lirbrary folder before you start the Arduino IDE.

You will find it standartly under the following path of your windows installation.


Infrared send systems have different protocolls to send data. In this example we use the RC5 protocol, the used library "Arduino-IRremote" converts the data independently. The library has additional protocolls, they are marked in this documentation.

Code for the receiver:

// Arduino-IRremote library will be added

// You can declare the input pin for the signal output of the KY-022 here
int RECV_PIN = 11;

// Arduino-IRremote library will be initialized
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
  irrecv.enableIRIn(); // Infrared receiver will be started

// main program loop
void loop() {
  // It will be checked if the receiver has gotten a signal.
  if (irrecv.decode(&results)) {
    // At signal input, the received and decoded signal will show via serial console.
    Serial.println(results.value, HEX);

Code for the transmitter:

//Arduino-IRremote library will be added

//...and here initialized
IRsend irsend;
<br />// The configuration of the output pin will be made by the library<br />// The output pin is a different one for different arduinos<br />// Arduino UNO:  Output = D3<br />// Arduino MEGA: Output = D9<br />// You will find a full list of output pins on the website;
void setup()

// main program loop
void loop() {
        // The transmitter sends in this example the signal A90 (hex. dezimal form) in the endcoding "RC5"
        // It will be transmitted 3 times, after that it will make a 5 second break.
	for (int i = 0; i < 3; i++) {
		irsend.sendRC5(0xA90, 12); // [0xA90] signal | [12] Bit-length signal (hex A90=1010 1001 0000)
	delay(5000); // 5 second break between the sending impulses

Example program download:

Connections Arduino 1 [Receiver]:


Signal = [Pin 11]
+V = [Pin 5V]
GND = [Pin GND]

Connections Arduino 2 [Transmitter]:


Signal = [Pin 3 (Arduino Uno) | Pin 9 (Arduino Mega)]
GND+Widerstand = [Pin GND*]
GND = [Pin GND]
  • *Only if a resistor was soldered to the circuit board.

Codebeispiel Raspberry Pi

Because of its progressive processor architecture the Raspberry Pi has a big advantage to the Arduino.

It can run a full Linux OS by itself. With help of an infrared-receiver, it could not only transmit simple data signals, furthermore it can control complete programs via remote.

To setup an infrared control system you should use the Linux software "lirc" ( published under the LGPL-Website ). In the following section, we show how you can teach lirc a remote and how the remote sends the learned signals via infrared ( for example to make the Raspberry Pi to a software controlled remote ).

On this purpose, the module KY-005 will be used as an infrared-transmitter and the Ky-022 will be used as infrared-receiver.

Connections Raspberry Pi:


Signal = GPIO17 [Pin 11]
GND+Widerstand = GND* [Pin 9]
GND = GND [Pin 6]
  • *Only if a resistor was soldered to the module


Signal = GPI18 [Pin 12]
+V = 3,3V [Pin 17]
GND = GND [Pin 25]

Lirc Installation

Open a terminal at the desktop or use SSH to login to your Raspberry Pi. To install lirc use following command:

sudo apt-get install lirc -y

[For this the Raspberry Pi has to be connected to the internet]

To use the lirc module immediately after the OS boot, you have to add the following string to the end of the file "/boot/config.txt":


The "gpio_in_pin=18" will be defined as input pin of the IR-receiver and the "gpio_out_pin=17" as output pin of the IR-transmitter.

The file could be editted with the command:

sudo nano /boot/config.txt

You can save and close the file with the key sequence [ctrl+x -> y -> enter]

You also have to modify the file "/etc/lirc/hardware.conf" with the command:

sudo nano /etc/lirc/hardware.conf

in this file you have to change following lines:


The modified file should now look like:

# /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

After that, we reboot the Raspberry Pi with the following command:

sudo reboot

IR-Receiver Test

To test the connected receiver, you have to close the lirc first with the following command:

sudo /etc/init.d/lirc stop

After that, you can test if signals could be detected on the Raspberry Pi by using the following command:

mode2 -d /dev/lirc0

and by pressing random keys on an infrared remote. Now you should see numbers in the following form:

space 95253
pulse 9022
space 2210
pulse 604
space 95246
pulse 9019
space 2211
pulse 601
space 95252
pulse 9019
space 2210
pulse 603
space 95239
pulse 9020
space 2208
pulse 603

You can restart lirc with the following command:

sudo /etc/init.d/lirc start

Teaching Remote

To register an infrared remote in the "lirc" system you have to configure the file "/etc/lirc/lircd.conf". The infrared codes for receiving are saved in this file.

You can use the lirc-software assistant to create a lircd.conf which will create the file automatically for you. To use the assistant follow these steps:

First you have to stop the lirc service

sudo /etc/init.d/lirc stop

With the next command you will start the assistant:

irrecord -d /dev/lirc0 ~/MeineFernbedienung.conf

The assistant will first initialize the remote, for that you have to press multiple keys after another so that the lirc system could learn the coding of the remote. Please follow the instructions of the assistant for that. The assistant will ask you for the key assignments which have to get a new infrared code after the end of the initializing. For this you can choose the key assignments from the following file: 


After you gave these to the assistant and confirmed it with enter, the recording of the infrared code for the chosen key will start.

Example:  type in [KEY_0]  -> confirm with enter -> press key "0" from the remote -> waiting for the assistant to confirm the recording.

You can end the assistant with the enter key if you don't want to assign another infrared code to a key. After this the configuration file will be created but you have to choose a name for the learned remote. For this we will open the file with the editor:

sudo nano ~/MeineFernbedienung.conf

Here you can change the line 17 from

    name  /home/pi/MeineFernbedienung.conf


    name  MeineFernbedienung

Please keep in mind to not use any special characters within the name. You can save and close the file with the key sequence [ctrl+x -> y -> enter].

After creating the configuration file you can make a backup from the original lircd.conf with the following command:

sudo mv /etc/lirc/lircd.conf /etc/lirc/lircd.conf.bak

You can start to use the before created configuration file with the command:

sudo cp ~/MeineFernbedienung.conf /etc/lirc/lircd.conf

Now you can start the lirc system again with the following command:

sudo /etc/init.d/lirc start

Now the new learned remote is registered at the system and can used with compatible software like Mediencenter OpenElec. Alternatively you can use the following command to test the key assignments and function of the remote.


It is possible that you have to enable the remote in the settings of OpenElec before you can use it.


Transmitting command via infrared transmitter

If you want to control devices like TV with the Raspberry Pi via infrared, you can now transmit the before learned commands. You can build up a software controlled infrared control system or you can start and stop single devices via network/internet.

First we check the key assignment with the following command:

irsend LIST MeineFernbedienung "" 

Now we can send the command [KEY_0] with the following command line:

irsend SEND_ONCE MeineFernbedienung KEY_0

You should now see a reaction at the device (TV or something you wanted to control). You can have another variation of the command above which starts a loop and constantly repeats the sending of the signal.

irsend SEND_START MeineFernbedienung KEY_0

The code will be constantly sended until you use the command:

irsend SEND_STOP MeineFernbedienung KEY_0

This will normally used for increasing and decreasing the volume.