กลับหน้าหลัก
views
How to Use ESP8266 IR Receiver Module KY-022 to Control LEDs
Last updated on

How to Use ESP8266 IR Receiver Module KY-022 to Control LEDs


How to Use ESP8266 IR Receiver Module KY-022 to Control LEDs

This guide explains how to connect the IR Receiver Module KY-022 to ESP8266 and use a remote to toggle 4 LEDs on and off. You will see the wiring, library setup, and how to customize the code for your own remote.

Complete circuit diagram showing ESP8266, KY-022, and 4 LEDs connected on a breadboard with labeled wires

Parts List

ComponentQuantity
NodeMCU ESP8266 V2 (CP2102)1 board
Micro USB Cable1 cable
Power Adapter 5V 2A1 unit
Breadboard MB-102 830 points1 board
IR Receiver Module KY-0221 module
IR Remote Control + battery1 set
5mm Red LED5 pieces
220 Ohm 1/4W Resistor10 pieces
Jumper Wire M-M 20cm40 pcs
Jumper Wire M-F 20cm40 pcs
Jumper Wire F-F 20cm40 pcs

Wiring ESP8266, KY-022, and LEDs

1. Connect KY-022 to ESP8266

KY-022ESP8266
Pin + (VCC)5V
Pin - (GND)GND
Pin S (Signal)D0

2. Connect Each LED

For each LED, connect as follows (long leg = anode, short leg = cathode)

FunctionESP8266
LED 1 SignalD1
LED 2 SignalD2
LED 3 SignalD5
LED 4 SignalD6
Common GNDThrough 220 Ohm resistor to GND
Close-up of single LED connection showing 220 Ohm resistor between short leg and GND, long leg connected to digital pin

3. Assemble on Breadboard

  • Place ESP8266 on the breadboard and route 5V and GND to the power rails
  • Insert KY-022 correctly oriented (+, -, S) onto the breadboard
  • Wire the 4 LEDs with their 220 Ohm resistors to the designated pins
Top-down view of full breadboard layout showing ESP8266 at center, KY-022 on left, 4 LEDs on right with color-coded wires

Installing the IR Library

  1. Download IRRemote_DIYables_IRcontroller library from the provided MediaFire link
  2. Extract the RAR file using WinRAR or WinZip
  3. Copy the extracted folder to
    This PC > Documents > Arduino > libraries
  4. Restart Arduino IDE

ESP8266 IR LED Control Code

This is a skeleton sketch ready to use. You only need to replace the placeholder remote codes with the actual codes from your remote.

#include "DIYables_IRcontroller.h"

// === Pin Definitions ===
#define IR_PIN        D0       // KY-022 Signal pin
#define LED1_PIN      D1
#define LED2_PIN      D2
#define LED3_PIN      D5
#define LED4_PIN      D6

// === LED States ===
bool led1State = LOW;
bool led2State = LOW;
bool led3State = LOW;
bool led4State = LOW;

// === Replace these with YOUR remote codes ===
// How to find: Open Serial Monitor, press buttons, copy the codes shown
const uint64_t KEY_1 = 0xFF6897;   // Button 1 turns LED1 on
const uint64_t KEY_2 = 0xFF9867;   // Button 2 turns LED1 off
const uint64_t KEY_3 = 0xFF30CF;   // Button 3 turns LED2 on
const uint64_t KEY_4 = 0xFF18E7;   // Button 4 turns LED2 off
const uint64_t KEY_5 = 0xFF7A85;   // Button 5 turns LED3 on
const uint64_t KEY_6 = 0xFF10EF;   // Button 6 turns LED3 off
const uint64_t KEY_7 = 0xFF42BD;   // Button 7 turns LED4 on
const uint64_t KEY_8 = 0xFF4AB5;   // Button 8 turns LED4 off
const uint64_t KEY_9 = 0xFF52AD;   // Button 9 turns all LEDs on
const uint64_t KEY_0 = 0xFF6897;   // Button 0 turns all LEDs off

IRrecv irrecv(IR_PIN);
decode_results results;

void setup() {
  Serial.begin(9600);
  
  // Set LED pins as outputs
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  pinMode(LED3_PIN, OUTPUT);
  pinMode(LED4_PIN, OUTPUT);
  
  // Turn all LEDs off at startup
  digitalWrite(LED1_PIN, LOW);
  digitalWrite(LED2_PIN, LOW);
  digitalWrite(LED3_PIN, LOW);
  digitalWrite(LED4_PIN, LOW);
  
  irrecv.enableIRIn();
  Serial.println("IR Receiver Ready");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.print("Received code: 0x");
    Serial.println(results.value, HEX);
    
    switch (results.value) {
      case KEY_1:
        led1State = HIGH;
        digitalWrite(LED1_PIN, led1State);
        Serial.println("LED 1 ON");
        break;
      case KEY_2:
        led1State = LOW;
        digitalWrite(LED1_PIN, led1State);
        Serial.println("LED 1 OFF");
        break;
      case KEY_3:
        led2State = HIGH;
        digitalWrite(LED2_PIN, led2State);
        Serial.println("LED 2 ON");
        break;
      case KEY_4:
        led2State = LOW;
        digitalWrite(LED2_PIN, led2State);
        Serial.println("LED 2 OFF");
        break;
      case KEY_5:
        led3State = HIGH;
        digitalWrite(LED3_PIN, led3State);
        Serial.println("LED 3 ON");
        break;
      case KEY_6:
        led3State = LOW;
        digitalWrite(LED3_PIN, led3State);
        Serial.println("LED 3 OFF");
        break;
      case KEY_7:
        led4State = HIGH;
        digitalWrite(LED4_PIN, led4State);
        Serial.println("LED 4 ON");
        break;
      case KEY_8:
        led4State = LOW;
        digitalWrite(LED4_PIN, led4State);
        Serial.println("LED 4 OFF");
        break;
      case KEY_9:
        led1State = led2State = led3State = led4State = HIGH;
        digitalWrite(LED1_PIN, HIGH);
        digitalWrite(LED2_PIN, HIGH);
        digitalWrite(LED3_PIN, HIGH);
        digitalWrite(LED4_PIN, HIGH);
        Serial.println("ALL LEDs ON");
        break;
      case KEY_0:
        led1State = led2State = led3State = led4State = LOW;
        digitalWrite(LED1_PIN, LOW);
        digitalWrite(LED2_PIN, LOW);
        digitalWrite(LED3_PIN, LOW);
        digitalWrite(LED4_PIN, LOW);
        Serial.println("ALL LEDs OFF");
        break;
    }
    irrecv.resume();
  }
}

Customization points:

  1. Replace KEY_1 through KEY_0 values with your actual remote’s codes
  2. If you use different LED pins, update LED1_PIN to LED4_PIN
  3. If you use a different resistor value (e.g., 330 Ohm), the LED will be dimmer but still functional

Upload and Test Procedure

  1. Select the correct Port for your board (check Device Manager)
  2. Select Board as NodeMCU 1.0 (ESP-12E Module)
  3. Upload the sketch (Sketch > Upload)
  4. Open Serial Monitor and set baud rate to 9600
  5. Press each button on the remote and note the codes shown in Serial Monitor
  6. Replace the placeholder codes in the sketch with the actual codes
  7. Upload the updated sketch

Expected Results

ButtonResult
1LED 1 turns on
2LED 1 turns off
3LED 2 turns on
4LED 2 turns off
5LED 3 turns on
6LED 3 turns off
7LED 4 turns on
8LED 4 turns off
9All 4 LEDs turn on
0All 4 LEDs turn off

Summary

Connecting the KY-022 to ESP8266 and using the IRRemote_DIYables_IRcontroller library makes IR remote control straightforward. The key step is finding and inputting your specific remote’s codes into the sketch. Using 220 Ohm resistors with LEDs is essential to protect them from excessive current.

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น