Relay Control by IR Remote- Receiver KY-022

Today I will show you, how to control Arduino outputs via an infrared (IR) transmitter - for example, a remote control to a TV or air conditioning. The remote control I tested in this experiment is universal from SILVERCREST, model KH 2159. The remote control is available mostly in the LIDL chain, works on most TVs, home theaters, players.

A lotof available sensors can be used to receive the IR signal, such as the KY-022 module.

Technical specifications KY-022:

  • Sensor type: IR receiver
  • Operating voltage: 2.7 - 5.5V
  • Consumption: 0.4 - 1.5mA
  • Reception angle: ± 45°
  • Frequency: 38kHz
  • Filter: 500lux
  • Potential reach: 18m
  • Receiver diode type: TL1838
  • Supported protocols: NEC, RC5, RC6, JVC, SONY, Panasonic, AIWA RC-T501, WHYNTER a iné

The program implementation uses the most used IRremote library for this IR receiver. The library is designed only for AVR processors, ie AtMega328P used in Arduino and related. For use with other architectures such as ESP32 / ESP8266, IRremoteESP8266 must be used. Both libraries are available on Githube. Libraries can also be used to transmit an IR signal (with a suitable module). In combination with the receiver in one program it is necessary to reinitialize the loading state - known "bug". In the Arduino program, I used the library functions to retrieve the protocol and subsequently send the code from the remote control.

When I press the Up button on the remote control, Arduino reads the NEC protocol and the value 820. When I press the button again, the same value was always transmitted. When you press the button separately, the opposite of these two values. Programmatically I then set a simple condition that the output terminal should be written logical 0, logical 1 - opposite state as current. With a series of conditions or switches, we can assign an action to each code you load. In my case, I controlled the relay, namely 8 on the 8-channel relay board, which is equipped with electromagnetic relays SRD-5VDC-SL-C, which can be switched up to 10A at 230V, ie a total power of 2300W per relay.

The control can be used for various actions, for example to control the Christmas lights on the house, control the RGB colors of LED strips, modes, relays, control DC / AC motor, door opening, gate. Additional control of measurements from sensors, further communication with other peripherals via Ethernet, SPI, UART, RS232, etc. Such remote control can also find a place in automation.
Program implementation:

/*|----------------------------------------------------------|*/
/*|SKETCH PRE NACITANIE KODOV Z DIALKOVYCH OVLADACOV         |*/
/*|VYHOTOVIL: MARTIN CHLEBOVEC                               |*/
/*|EMAIL: martinius96@gmail.com                              |*/
/*|DOSKA:  Arduino Uno, Mega, Nano, NEFUNGUJE NA ESP8266     |*/
/*|WEB: https://arduino.php5.sk                              |*/
/*|----------------------------------------------------------|*/
#include <IRremote.h> //kniznica pre senzor KY-022
int led = 13;
int ledstate = LOW;
int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN); //KY-022 na RECV VYVODE
decode_results results;
void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  irrecv.enableIRIn(); //prijimaj signaly
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println("Zaznamenany kod: ");
    Serial.println(results.value, HEX);
    String abc = String(results.value, HEX);
    if ((abc == "820") || (abc == "20")) { //hodnoty tlacidla hore na ovladaci
      ledstate = !ledstate;
      digitalWrite(led, ledstate);
    }
    irrecv.resume(); // Povol nacitavanie dalsich hodnot
  }
  delay (100); // kratky delay, odrusenie citania chyb
}

Wiring diagram: