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.
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 Module | ESP8266 |
|---|---|
| VCC | 5V |
| GND | GND |
| IN | D0 |
Connect 4 LEDs
Each LED connects through a 220 Ohm resistor on the negative leg (shorter leg).
| LED | ESP8266 |
|---|---|
| LED 1 (long leg) | D1 |
| LED 2 (long leg) | D2 |
| LED 3 (long leg) | D5 |
| LED 4 (long leg) | D6 |
Install Library for IR Remote
- Download the library from GitHub (search IRremoteESP8266) or Arduino Library Manager
- Extract and copy the folder to
Documents/Arduino/libraries/ - 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
- Select Board as NodeMCU 1.0 (ESP-12E Module)
- Select the connected Port
- Press Upload
- Wait for Done uploading
- Open Serial Monitor at 9600 baud
- Press buttons on remote and observe results
Button Summary
| Button | Function |
|---|---|
| 1 | Toggle LED 1 |
| 2 | Toggle LED 2 |
| 3 | Toggle LED 3 |
| 4 | Toggle LED 4 |
| 9 | Turn on all 4 LEDs |
| 0 | Turn off all 4 LEDs |
Reference Video
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย