How to Use ESP32 with KY-022 IR Receiver to Control LEDs with Remote
How to Use ESP32 with KY-022 IR Receiver to Control LEDs with Remote
This tutorial shows how to use ESP32 with the KY-022 infrared receiver module to control 4 LEDs with a remote control. The principle is simple: when you press a button on the remote, ESP32 receives the IR signal and turns LEDs on or off accordingly.
![ESP32 circuit connected to KY-022 and 4 LEDs on breadboard]
Required Components
| Component | Quantity |
|---|---|
| NodeMCU ESP32 | 1 board |
| Infrared Receiver Module KY-022 | 1 unit |
| IR Remote Control + battery | 1 set |
| 5mm Red LED | 4 units |
| 220 Ohm Resistor | 4 units |
| Breadboard MB-102 830 points | 1 unit |
| Jumper wires (M-M, M-F, F-F) | at least 10 pieces |
| Micro USB cable | 1 piece |
ESP32 Wiring: KY-022 and LEDs
KY-022 Pin Connections
| KY-022 | ESP32 |
|---|---|
| + (VCC) | 5V |
| - (GND) | GND |
| S (Signal) | D23 |
LED Pin Connections
| LED | ESP32 |
|---|---|
| LED 1 Long leg (Anode) | D22 |
| LED 2 Long leg (Anode) | D21 |
| LED 3 Long leg (Anode) | D19 |
| LED 4 Long leg (Anode) | D18 |
| All LEDs Short leg (Cathode) | Through 220 Ohm resistor to GND |
![Breadboard layout showing positions of 4 LEDs, resistors, and wiring]
Installing Library for KY-022
- Download IRremote_DIYables_IRcontroller from the provided Mediafire link
- Extract the file using WinRAR or WinZip
- Place the folder in
Documents/Arduino/libraries - Restart Arduino IDE and check that the library is installed via Sketch > Include Library
Note: If you prefer to use the standard Arduino IRremote library instead, you can, but the code below is written for DIYables_IRcontroller.
ESP32 Code: Read IR and Control LEDs
This code is a Skeleton that requires you to input your actual remote’s code values. Steps are explained below.
#include "DIYables_IRcontroller.h"
// ====== LED Pin Definitions ======
#define LED_1 22
#define LED_2 21
#define LED_3 19
#define LED_4 18
// ====== KY-022 Signal Pin ======
#define IR_RECEIVER_PIN 23
// Create IR Receiver object
DIYables_IRcontroller irController(IR_RECEIVER_PIN, LED_BUILTIN);
// LED state variables
bool led1State = false;
bool led2State = false;
bool led3State = false;
bool led4State = false;
void setup() {
Serial.begin(9600);
// Set LED pins as Output
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
pinMode(LED_4, OUTPUT);
// Turn all LEDs off initially
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
// Initialize IR Controller
irController.begin();
Serial.println("ESP32 IR LED Control Ready");
Serial.println("Press remote buttons and check values here");
}
void loop() {
// Read value from remote
Key_t key = irController.readKey();
if (key != Key_t::NONE) {
// TODO: Replace KEY_XXX with your actual remote button codes
// View values in Serial Monitor when pressing each button
if (key == Key_t::KEY_1) { // Replace with your actual remote code
led1State = !led1State;
digitalWrite(LED_1, led1State ? HIGH : LOW);
Serial.println(led1State ? "LED 1 ON" : "LED 1 OFF");
}
else if (key == Key_t::KEY_2) { // Replace with your actual remote code
led1State = false;
digitalWrite(LED_1, LOW);
Serial.println("LED 1 OFF");
}
else if (key == Key_t::KEY_9) { // Replace with your actual remote code
// Turn on all 4 LEDs
digitalWrite(LED_1, HIGH);
digitalWrite(LED_2, HIGH);
digitalWrite(LED_3, HIGH);
digitalWrite(LED_4, HIGH);
led1State = led2State = led3State = led4State = true;
Serial.println("ALL LED ON");
}
else if (key == Key_t::KEY_0) { // Replace with your actual remote code
// Turn off all 4 LEDs
digitalWrite(LED_1, LOW);
digitalWrite(LED_2, LOW);
digitalWrite(LED_3, LOW);
digitalWrite(LED_4, LOW);
led1State = led2State = led3State = led4State = false;
Serial.println("ALL LED OFF");
}
// You can add buttons 3-8 to control LEDs 2-4 as needed
}
}
How to Find Your Remote’s Code Values
Important: Each remote’s button values are different. Follow these steps:
- Upload the code to your board, open Serial Monitor at 9600 baud
- Press each button on your remote and note the values shown in Serial Monitor
- Replace
KEY_XXXin the code with your actual values - Upload the updated code
![Serial Monitor showing Hex values of remote buttons when pressed]
Upload Steps
- Select the correct Port for your ESP32 (Tools > Port)
- Select Board as ESP32 Dev Module (Tools > Board > ESP32 Arduino)
- Click Upload (Ctrl+U)
- Wait for “Done uploading”
- Open Serial Monitor and test with your remote
Remote Button Summary
| Button | Function |
|---|---|
| 1 | Toggle LED 1 on/off |
| 2 | Turn off LED 1 |
| 3 | (Addable) Control LED 2 |
| 4 | (Addable) Control LED 2 |
| 5-8 | (Addable) Control LEDs 3-4 |
| 9 | Turn on all 4 LEDs |
| 0 | Turn off all 4 LEDs |
Troubleshooting
Remote not working
- Check that the battery is inserted correctly (+/-)
- Try replacing with a new battery
- Verify KY-022 wiring is correct
Serial Monitor shows nothing
- Verify Baud Rate is set correctly (9600)
- Check the correct Port is selected
LED not turning on
- Check LED long/short leg orientation
- Verify resistors are connected
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย