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.
Parts List
| Component | Quantity |
|---|---|
| NodeMCU ESP8266 V2 (CP2102) | 1 board |
| Micro USB Cable | 1 cable |
| Power Adapter 5V 2A | 1 unit |
| Breadboard MB-102 830 points | 1 board |
| IR Receiver Module KY-022 | 1 module |
| IR Remote Control + battery | 1 set |
| 5mm Red LED | 5 pieces |
| 220 Ohm 1/4W Resistor | 10 pieces |
| Jumper Wire M-M 20cm | 40 pcs |
| Jumper Wire M-F 20cm | 40 pcs |
| Jumper Wire F-F 20cm | 40 pcs |
Wiring ESP8266, KY-022, and LEDs
1. Connect KY-022 to ESP8266
| KY-022 | ESP8266 |
|---|---|
| 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)
| Function | ESP8266 |
|---|---|
| LED 1 Signal | D1 |
| LED 2 Signal | D2 |
| LED 3 Signal | D5 |
| LED 4 Signal | D6 |
| Common GND | Through 220 Ohm resistor to GND |
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
Installing the IR Library
- Download IRRemote_DIYables_IRcontroller library from the provided MediaFire link
- Extract the RAR file using WinRAR or WinZip
- Copy the extracted folder to
This PC > Documents > Arduino > libraries - 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:
- Replace
KEY_1throughKEY_0values with your actual remote’s codes - If you use different LED pins, update
LED1_PINtoLED4_PIN - If you use a different resistor value (e.g., 330 Ohm), the LED will be dimmer but still functional
Upload and Test Procedure
- Select the correct Port for your board (check Device Manager)
- Select Board as NodeMCU 1.0 (ESP-12E Module)
- Upload the sketch (Sketch > Upload)
- Open Serial Monitor and set baud rate to 9600
- Press each button on the remote and note the codes shown in Serial Monitor
- Replace the placeholder codes in the sketch with the actual codes
- Upload the updated sketch
Expected Results
| Button | Result |
|---|---|
| 1 | LED 1 turns on |
| 2 | LED 1 turns off |
| 3 | LED 2 turns on |
| 4 | LED 2 turns off |
| 5 | LED 3 turns on |
| 6 | LED 3 turns off |
| 7 | LED 4 turns on |
| 8 | LED 4 turns off |
| 9 | All 4 LEDs turn on |
| 0 | All 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
จ้างทำโปรเจคเลย