กลับหน้าหลัก
views
How to Use ESP8266 with IR Remote to Control LED
Last updated on

How to Use ESP8266 with IR Remote to Control LED


How to Use ESP8266 with IR Remote to Control LED

This tutorial shows how to use ESP8266 to receive signals from an IR Remote (infrared remote) to control 4 LEDs based on which button is pressed.

Diagram showing ESP8266 NodeMCU V2 connected to IR Module and 4 LEDs on Breadboard

Required Components

  • NodeMCU ESP8266 V2 (CP2102)
  • IR Infrared Remote Control Kit (receiver module + remote)
  • Red 5mm LEDs x4
  • 220 Ohm resistors x4
  • MB-102 Breadboard (830 points)
  • Jumper wires (M-M, M-F, F-F)
  • Micro USB cable
  • Power Adapter 5V 2A

Wiring ESP8266 with IR Module and LEDs

Connect IR Module

IR ModuleESP8266
VCC5V
GNDGND
IND0
Circuit showing IR Module connected to 5V, GND, D0 pins of ESP8266

Connect 4 LEDs

Each LED connects through a 220 Ohm resistor on the negative leg (shorter leg).

LEDESP8266
LED 1 (long leg)D1
LED 2 (long leg)D2
LED 3 (long leg)D5
LED 4 (long leg)D6
Circuit showing each LED connected via 220 Ohm resistor to D1, D2, D5, D6

Install Library for IR Remote

  1. Download the library from GitHub (search IRremoteESP8266) or Arduino Library Manager
  2. Extract and copy the folder to Documents/Arduino/libraries/
  3. Open Arduino IDE and go to Sketch > Include Library > IRremoteESP8266

Step 1: Read Remote Codes

Before using the remote, you need to know what code each button sends. Copy the code below to Arduino IDE and upload.

#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <ir_Gree.h>

const uint16_t RECV_PIN = D0;  // IR Module IN pin

IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop() {
  if (irrecv.decode(&results)) {
    Serial.print("Code: 0x");
    Serial.println(results.value, HEX);
    irrecv.resume();  // Continue receiving
  }
  delay(100);
}

Open Serial Monitor at 9600 baud, press different buttons on the remote, and record the displayed values. Use these values in the next step.

Step 2: Main Code to Control LEDs with IR Remote

Replace 0xXXXXXXXX with actual codes from Step 1.

#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <ir_Gree.h>

const uint16_t RECV_PIN = D0;

IRrecv irrecv(RECV_PIN);
decode_results results;

// LED pins
const int LED1 = D1;
const int LED2 = D2;
const int LED3 = D5;
const int LED4 = D6;

// Replace with actual codes from Step 1
const uint32_t CODE_1 = 0xXXXXXXXX;  // Button 1
const uint32_t CODE_2 = 0xXXXXXXXX;  // Button 2
const uint32_t CODE_3 = 0xXXXXXXXX;  // Button 3
const uint32_t CODE_4 = 0xXXXXXXXX;  // Button 4
const uint32_t CODE_5 = 0xXXXXXXXX;  // Button 5
const uint32_t CODE_6 = 0xXXXXXXXX;  // Button 6
const uint32_t CODE_7 = 0xXXXXXXXX;  // Button 7
const uint32_t CODE_8 = 0xXXXXXXXX;  // Button 8
const uint32_t CODE_9 = 0xFFFFFFFF;  // Button 9 (all on)
const uint32_t CODE_0 = 0xFFFFFFFF;  // Button 0 (all off)

bool led1State = false;
bool led2State = false;
bool led3State = false;
bool led4State = false;

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
}

void loop() {
  if (irrecv.decode(&results)) {
    uint32_t code = results.value;
    
    // Check and toggle LED states
    if (code == CODE_1) {
      led1State = !led1State;
      digitalWrite(LED1, led1State);
      Serial.println(led1State ? "LED 1 ON" : "LED 1 OFF");
    }
    else if (code == CODE_2) {
      led2State = !led2State;
      digitalWrite(LED2, led2State);
      Serial.println(led2State ? "LED 2 ON" : "LED 2 OFF");
    }
    else if (code == CODE_3) {
      led3State = !led3State;
      digitalWrite(LED3, led3State);
      Serial.println(led3State ? "LED 3 ON" : "LED 3 OFF");
    }
    else if (code == CODE_4) {
      led4State = !led4State;
      digitalWrite(LED4, led4State);
      Serial.println(led4State ? "LED 4 ON" : "LED 4 OFF");
    }
    else if (code == CODE_9) {
      // Turn on all 4 LEDs
      led1State = led2State = led3State = led4State = true;
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      Serial.println("ALL LED ON");
    }
    else if (code == CODE_0) {
      // Turn off all 4 LEDs
      led1State = led2State = led3State = led4State = false;
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      Serial.println("ALL LED OFF");
    }
    
    irrecv.resume();
  }
  delay(100);
}

What to modify before uploading: Replace CODE_1 through CODE_8 with actual values from your remote.

How to Upload and Test

  1. Select Board as NodeMCU 1.0 (ESP-12E Module)
  2. Select the connected Port
  3. Press Upload
  4. Wait for Done uploading
  5. Open Serial Monitor at 9600 baud
  6. Press buttons on remote and observe results

Button Summary

ButtonFunction
1Toggle LED 1
2Toggle LED 2
3Toggle LED 3
4Toggle LED 4
9Turn on all 4 LEDs
0Turn off all 4 LEDs

Reference Video

https://www.youtube.com/embed/3PT4LdS9xGA

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

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

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

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

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...