กลับหน้าหลัก
views
How to Use Arduino IR Remote Control with KY-022 to Control LEDs
Last updated on

How to Use Arduino IR Remote Control with KY-022 to Control LEDs


How to Use Arduino IR Remote Control with KY-022 to Control LEDs

This guide explains how to use Arduino to receive infrared signals from a remote control and use them to toggle 4 LEDs on and off using the KY-022 infrared receiver module, a popular component in IoT and remote control projects.

Circuit diagram showing Arduino UNO connected to KY-022 module and 4 LEDs with 220Ω resistors on a breadboard

Required Components

  • Arduino UNO R3 with USB cable
  • Infrared Receiver Module KY-022
  • IR Remote control (any TV or device remote will work)
  • 5mm Red LEDs x 4
  • 220 Ohm resistors x 4
  • MB-102 Breadboard (830 Point)
  • Jumper wires: Male-to-Male, Male-to-Female, Female-to-Female, 20cm each

Wiring: Arduino with KY-022 and LEDs

Connect KY-022 to Arduino

Arduino UNOKY-022
5V+ (VCC)
GND- (GND)
Pin 11S (Signal)

Connect Each LED to Arduino

For each LED, wire as follows (one resistor per LED):

Arduino UNOComponent
Pin 2Long leg (Anode) of LED 1
Pin 3Long leg (Anode) of LED 2
Pin 4Long leg (Anode) of LED 3
Pin 5Long leg (Anode) of LED 4
GND220Ω resistor → Short leg (Kathode) of LED

Note: The long leg is the Anode (+), and the short leg is the Kathode (-). Make sure to wire them with correct polarity.

Close-up diagram showing LED pin identification with clear labeling of Anode (long leg) and Kathode (short leg)

Installing the IRremote Library

  1. Download IRremote.zip
  2. Extract the file using WinRAR or WinZip
  3. Place the extracted folder in Documents/Arduino/libraries
  4. Restart Arduino IDE

Note: You need a file extraction program (WinRAR, WinZip, or 7-Zip) installed on your computer before extracting.

Step 1: Reading Your Remote’s Button Codes

Each remote has different hexadecimal codes for each button. You must read the codes from your specific remote before using them in the control sketch.

Upload the following code to read button codes (this is a read-only sketch, not the LED control code):

#include <IRremote.h>

const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
  Serial.println("Press remote buttons to see codes");
}

void loop(){
  if (irrecv.decode(&results)){
    Serial.println(results.value, HEX);
    irrecv.resume();
  }
}

How to Read Button Codes

  1. Open Serial Monitor (Tools → Serial Monitor)
  2. Set Baud Rate to 9600
  3. Press buttons on your remote - each press will display a Hex code

Tip: Stable codes will be consistent 8-digit numbers. Press repeatedly until you get the same value each time. If the code keeps changing, keep pressing until it stabilizes.

Serial Monitor window showing hexadecimal codes displayed when remote buttons are pressed

LED Control Sketch with Remote

After obtaining your button codes, insert them into the sketch below. Replace all 0xFFFFFFF placeholders with the actual codes you read in the previous step.

Example: If pressing button 1 gives code 0xFF6897, replace the placeholder in case 0xFF6897:

#include <IRremote.h>

const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

// Define LED pins
const int LED1 = 2;
const int LED2 = 3;
const int LED3 = 4;
const int LED4 = 5;

void setup(){
  Serial.begin(9600);
  irrecv.enableIRIn();
  
  // Set LED pins as OUTPUT
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  
  // Turn all LEDs off initially
  digitalWrite(LED1, LOW);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
}

void loop(){
  if (irrecv.decode(&results)){
    Serial.println(results.value, HEX);
    
    switch(results.value){
      // Button 1 toggles LED 1
      case 0xFF6897:  // Replace with your remote's button 1 code
        digitalWrite(LED1, !digitalRead(LED1));
        Serial.println(digitalRead(LED1) ? "LED 1 ON" : "LED 1 OFF");
        break;
        
      // Button 2 toggles LED 2
      case 0xFF30CF:  // Replace with your remote's button 2 code
        digitalWrite(LED2, !digitalRead(LED2));
        Serial.println(digitalRead(LED2) ? "LED 2 ON" : "LED 2 OFF");
        break;
        
      // Button 3 toggles LED 3
      case 0xFF18E7:  // Replace with your remote's button 3 code
        digitalWrite(LED3, !digitalRead(LED3));
        Serial.println(digitalRead(LED3) ? "LED 3 ON" : "LED 3 OFF");
        break;
        
      // Button 4 toggles LED 4
      case 0xFF7A85:  // Replace with your remote's button 4 code
        digitalWrite(LED4, !digitalRead(LED4));
        Serial.println(digitalRead(LED4) ? "LED 4 ON" : "LED 4 OFF");
        break;
        
      // Button 9 turns all LEDs ON
      case 0xFF9867:  // Replace with your remote's button 9 code
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        Serial.println("ALL LED ON");
        break;
        
      // Button 0 turns all LEDs OFF
      case 0xFF4AB5:  // Replace with your remote's button 0 code
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        digitalWrite(LED3, LOW);
        digitalWrite(LED4, LOW);
        Serial.println("ALL LED OFF");
        break;
    }
    
    irrecv.resume();
  }
}

How to Upload the Sketch

  1. Open the sketch in Arduino IDE
  2. Select Board: Arduino UNO (Tools → Board)
  3. Select the connected Port (Tools → Port)
  4. Click the Upload button (right arrow icon)
  5. Wait for “Done uploading” message

Testing the Setup

After successful upload:

  1. Open Serial Monitor at Baud 9600
  2. Point your remote toward the KY-022 module
  3. Press button 1 → LED 1 toggles on/off
  4. Press button 2 → LED 2 toggles on/off
  5. Press button 3 → LED 3 toggles on/off
  6. Press button 4 → LED 4 toggles on/off
  7. Press button 9 → All 4 LEDs turn on together
  8. Press button 0 → All 4 LEDs turn off together

Customization Points

  1. Remote Button Codes - Replace all case 0x...: values with codes from your specific remote
  2. LED Pins - Modify LED1, LED2, LED3, LED4 constants if using different pins
  3. KY-022 Signal Pin - Change RECV_PIN if using a different Arduino pin

Summary

The KY-022 module reliably receives infrared signals and integrates easily with Arduino. Simply install the IRremote library, connect the signal pin correctly, and you can use any remote to control devices. The only requirement is knowing your remote’s button codes, which you can read using the code reader sketch provided above.

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

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

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

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

ความคิดเห็น