• Arduino
  • Raspberry Pi
  • Raspberry Pi Pico
  • Micro:Bit

This module can receive infrared signals and outputs them at the signal output as a digital sequence.

In addition, the LED integrated on the module flashes briefly when an infrared signal is detected.

Technical data

Operating voltage 3,3 V - 5 V
Operating current 0,4 - 1,5 mA
Reception range 18 m
Reception angle ± 45 °
Carrier frequency 38 kHz

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Receiver
Pin 2 signal
5 V +V
GND GND

With the help of the sensor module KY-022 an infrared receiver can be built. For this only the infrared receiver module and a single Arduino are needed. The Arduino acts as a receiver and outputs the signals to the serial console.

For the following code example an additional library is needed:

Arduino-IRremote by Ken Shirriff | published under the MIT license.

Download the library and unzip it to your Arduino library folder. This is located by default under: C:\User[username]\Documents\Arduino\libraries. Alternatively, it is also included in the download package below.

With infrared transmitter and receiver systems, there are different protocols in which the data can be sent and received. In the following example the modified SimpleReceiver example is used for receiving - the used library "Arduino-IRremote" takes care of the conversion to the correct data sequence on its own. However, there are also other protocols/encodings within the library - these are marked in the documentation/code of the library. Documentation.

Please note: For the execution of the code example an additional file is necessary. This opens automatically as soon as you open the sample code from the Arduino IRremote library. Therefore, first open the example code via the following path: File -> Examples -> IRremote -> SimpleReceiver. Now you can replace the example code with our modified example.

Code for the receiver

/*
 * SimpleReceiver.cpp
 *
 * Demonstriert den Empfang von NEC-IR-Codes mit IRrecv
 *
 * Copyright (C) 2020-2021 Armin Joachimsmeyer
 * armin.joachimsmeyer@gmail.com
 *
 * Diese Datei ist Teil von Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
 *
 * MIT-Lizenz
 */

/*
 * Geben Sie an, welche(s) Protokoll(e) für die Dekodierung verwendet werden soll(en).
 * * Wenn kein Protokoll definiert ist, sind alle Protokolle aktiv.
 */
//#define DECODE_DENON // Enthält Sharp
//#define DECODE_JVC
//#define DECODE_KASEIKYO
//#define DECODE_PANASONIC // dasselbe wie DECODE_KASEIKYO
//#define DECODE_LG
#define DECODE_NEC // Beinhaltet Apple und Onkyo
//#define DECODE_SAMSUNG
//#define DECODE_SONY
//#define DECODE_RC5
//#define DECODE_RC6

//#define DECODE_BOSEWAVE
//#define DECODE_LEGO_PF
//#define DECODE_MAGIQUEST
//#define DECODE_WHYNTER

//#define DECODE_DISTANCE // Universaldecoder für Impulsbreiten- oder Impulsabstandsprotokolle
//#define DECODE_HASH // Spezialdecoder für alle Protokolle

#include <Arduino.h>

/*
 * Makros für Eingangs- und Ausgangspin definieren etc.
 */
#include "PinDefinitionsAndMore.h"

#include <IRremote.h>

void setup() {
    Serial.begin(115200);
    // Nur um zu wissen, welches Programm auf meinem Arduino läuft
    Serial.println(F("START " __FILE__ " von " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));

    /*
     * Starten des Empfängers, Aktivieren der Feedback-LED und Abgreifen des LED-Feedback-Pins von der internen Kartendefinition
     */
    IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);

    Serial.print(F("Bereit zum Empfang von IR-Signalen an Pin "));
    Serial.println(IR_RECEIVE_PIN);
}

void loop() {
    /*
     * Prüfen, ob empfangene Daten vorhanden sind und wenn ja, versuchen, diese zu dekodieren.
     * Dekodiertes Ergebnis steht in der Struktur IrReceiver.decodedIRData.
     *
     * Z.B. Befehl ist in IrReceiver.decodedIRData.command
     * Adresse ist in Befehl ist in IrReceiver.decodedIRData.address
     * und bis zu 32 Bit Rohdaten in IrReceiver.decodedIRData.decodedRawData
     */
    if (IrReceiver.decode()) {

        // Drucken einer kurzen Zusammenfassung der empfangenen Daten
        IrReceiver.printIRResultShort(&Serial);
        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            // Wir haben hier ein unbekanntes Protokoll, drucken Sie weitere Informationen
            IrReceiver.printIRResultRawFormatted(&Serial, true);
        }
        Serial.println();

        /*
         * !!!Wichtig!!! Aktivieren Sie den Empfang des nächsten Wertes,
         * da der Empfang nach dem Ende des aktuell empfangenen Datenpakets gestoppt wurde.
         */
        IrReceiver.resume(); // Empfang des nächsten Wertes freigeben

        /*
         * Abschließend prüfen Sie die empfangenen Daten und führen Aktionen entsprechend dem empfangenen Befehl aus
         */
        if (IrReceiver.decodedIRData.command == 0x34) {
            Serial.println("Signal empfangen");
        } else if (IrReceiver.decodedIRData.command == 0x36) {
            Serial.println("Signal empfangen und dieses Mal ist es ein anderes");
        } else {
            Serial.println("Signal empfangen, aber leider nicht das richtige");
        }
    }
}

Beispielprogramm Download

KY022-Arduino.zip

This module can receive infrared signals and outputs them at the signal output as a digital sequence.

In addition, the LED integrated on the module flashes briefly when an infrared signal is detected.

Technical data

Operating voltage 3,3 V - 5 V
Operating current 0,4 - 1,5 mA
Reception range 18 m
Reception angle ± 45 °
Carrier frequency 38 kHz

Pin assignment

Code example Raspberry Pi

Pin assignment Raspberry Pi:

Raspberry Pi Receiver
GPIO15 [Pin 10] Signal
3,3 V [Pin 17] +V
GND [Pin 25] GND

For the initial setup, first open your config.txt file using the following command:

sudo nano /boot/config.txt

Add the following configurations to the end of the file here:

dtoverlay=gpio-ir,gpio_pin=15
dtoverlay=gpio-ir-tx,gpio_pin=14

With the key combination [CTRL+O] you can save the file. Confirm this with [Enter] and exit the editor with the combination [CTRL+X]. Now restart your Raspberry Pi with the following command:

sudo reboot

Now install the ir-keytable module:

sudo apt-get install ir-keytable -y

With the following command you can determine the device identification. This is necessary to be able to address the receiver in the further course:

ir-keytable

The first line of the output should look something like this:

Found /sys/class/rc/rc0/ (/dev/input/event0) with:

Here it can be read that we can address our receiver via the identification rc0. Therefore we start the reception now via the following command:

ir-keytable -t -s rc0

This module can receive infrared signals and outputs them at the signal output as a digital sequence.

In addition, the LED integrated on the module flashes briefly when an infrared signal is detected.

Technical data

Operating voltage 3,3 V - 5 V
Operating current 0,4 - 1,5 mA
Reception range 18 m
Reception angle ± 45 °
Carrier frequency 38 kHz

Pin assignment

Code example Micro:Bit

Port assignment Micro:Bit:

Micro:Bit Receiver
Pin 1 Signal
3 V +V
GND GND

For the following code example an additional library is needed:

pxt-makerbit-ir-receiver from 1010Technologies | published under the MIT License.

You must add this library to your IDE before using the code.

Add the library to your IDE by clicking on "Extensions" and entering the following URL in the search box: https://github.com/1010Technologies/pxt-makerbit-ir-receiver.git. Confirm the search with Enter.

Sample program download

microbit-KY-022-Remote.zip

This module can receive infrared signals and outputs them at the signal output as a digital sequence.

In addition, the LED integrated on the module flashes briefly when an infrared signal is detected.

Technical data

Operating voltage 3,3 V - 5 V
Operating current 0,4 - 1,5 mA
Reception range 18 m
Reception angle ± 45 °
Carrier frequency 38 kHz

Pin assignment

Code example Raspberry Pi Pico

Pin assignment Raspberry Pi Pico

Raspberry Pi Pico Receiver
GPIO16 Signal
3.3V +V
GND GND

With the help of the two sensor modules KY-005 and KY-022 a system with infrared remote control and infrared receiver can be built. But here we will only use the transmitter with a pico.

For the following code example 4 additional libraries are needed:

micropython-ir by Peter Hinch | published under the MIT license.

micropython-stubs by Jos Verlinde | published under the MIT License

smbus2 by Karl-Petter Lindegaard | published under the MIT license.

micropython-async by Peter Hinch | published under the MIT license.

In infrared transmission systems, there are different protocols in which the data can be sent. In the following example different protocols are used for sending. documentation

Code for the receiver

# Run this to characterise a remote.
# Load libraries
from sys import platform
import time
import gc
from machine import Pin, freq
from ir_rx.print_error import print_error  # Optional print of error codes

# Import all implemented classes
from ir_rx.nec import NEC_8, NEC_16, SAMSUNG
from ir_rx.sony import SONY_12, SONY_15, SONY_20
from ir_rx.philips import RC5_IR, RC6_M0
from ir_rx.mce import MCE

# Define the pin that will receive the data
p = Pin(16, Pin.IN)

# **** DISPLAY GREETING ****
s = """Test for IR receiver.
CheckIRData() for NEC 8 bit protocol,
CheckIRData(1) for NEC 16 bit,
CheckIRData(2) for Sony SIRC 12 bit,
CheckIRData(3) for Sony SIRC 15 bit,
CheckIRData(4) for Sony SIRC 20 bit,
CheckIRData(5) for Philips RC-5 protocol,
CheckIRData(6) for RC6 mode 0.
CheckIRData(7) for Microsoft Vista MCE.
CheckIRData(8) for Samsung.

Hit ctrl-c to stop, then disconnect and reconnect your Pico board. Press Stop/Restart afterwards."""

# User callback (function to print the received data)
def cb(data, addr, ctrl):
    if data < 0:  # For when the Data received is of the NEC protocol.
        print("Repeat code.")
    else:
        print(f"Data 0x{data:02x} Addr 0x{addr:04x} Ctrl 0x{ctrl:02x}")
        
    # The received data can be used here to decide what is to be printed for example
    if data == 0x16:
        print("Button 0 was pressed")
    elif data == 0x0c:
        print("Button 1 was pressed")
    elif data == 0x18:
        print("Button 2 was pressed")
    elif data == 0x5e:
        print("Button 3 was pressed")
    elif data == 0x08:
        print("Button 4 was pressed")
    elif data == 0x1c:
        print("Button 5 was pressed")
    elif data == 0x5a:
        print("Button 6 was pressed")
    elif data == 0x42:
        print("Button 7 was pressed")
    elif data == 0x52:
        print("Button 8 was pressed")
    elif data == 0x4a:
        print("Button 9 was pressed")
        
def CheckIRData(proto=0):
    # List of all possible protocols
    classes = (NEC_8, NEC_16, SONY_12, SONY_15, SONY_20, RC5_IR, RC6_M0, MCE, SAMSUNG)
    ir = classes[proto](p, cb) # Instantiate receiver
    ir.error_function(print_error) # Show debug information
    # ir.verbose = True
    try:
        while True:
            print("running")
            time.sleep(5)
            gc.collect()
    except KeyboardInterrupt:
        ir.close()

print(s)

# Continuous loop for continuous operation
while True:
    CheckIRData()

Example program download

KY022-Pico.zip