جدول المحتويات
ToggleIn this project, we will build an Arduino IR remote LED controller. First, we will toggle an LED with the kit’s IR remote. Then, we will extend this setup to control a servo-driven door. This tutorial builds on our previous project, the Arduino Password Door Lock. In that project, we created a door mechanism that opens and closes with a servo and is controlled by a keypad.
What You’ll Build
- LED-only controller: press one remote key → LED toggles on D8.
LED + Servo upgrade: two more keys to OPEN/CLOSE a door via a micro servo on D9; the LED shows door status automatically.
Components Required
For LED-only
Arduino Uno (or Nano)
IR receiver module (VS1838B / TSOP1838, 3-pin)
LED + 220 Ω resistor
Breadboard & jumper wires
Add for LED + Servo
Micro servo (SG90/MG90S)
External 5 V supply ≥ 1 A for the servo (recommended)
Simple latch/mechanism (3D-printed, wire, or cardboard)
Power tip: Servos draw current spikes. Power the servo from a separate 5 V supply and tie grounds together (servo GND ↔ Arduino GND).
Circuit Diagram — LED-only
Logic wiring (5 V):
IR receiver OUT → D2, VCC → 5 V, GND → GND
LED anode → 220 Ω → D8, cathode → GND

Programming the Arduino to control the LED with an IR remote
Step 1 — Install the Library
Open Arduino IDE → Tools → Manage Libraries… and install IRremote (by Arduino-IRremote). We use the modern API:
#include <IRremote.hpp> // ... IrReceiver.begin(pin, ENABLE_LED_FEEDBACK);
Step 2 — Scan Your Remote (IR Code Scanner)
Map one button for LED toggle (later you’ll add two for OPEN/CLOSE).
Open Serial Monitor (115200) and press each key a few times.
// IR Code Scanner (IRremote >= 3.x) #include <IRremote.hpp> const uint8_t IR_RECEIVE_PIN = 2; void setup(){ Serial.begin(115200); IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); Serial.println(F("IR Scanner ready. Press remote buttons...")); } void loop(){ if(IrReceiver.decode()){ IrReceiver.printIRResultShort(&Serial); // concise one-liner Serial.print(F("Protocol=")); Serial.print(IrReceiver.decodedIRData.protocol); Serial.print(F(" Command=0x"));Serial.print(IrReceiver.decodedIRData.command, HEX); Serial.print(F(" RAW=0x")); Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); IrReceiver.resume(); } }
Write down the Command (or RAW) value that appears consistently. With most NEC-style kit remotes, command
is stable; otherwise use RAW
in the final sketch.
Step 3 — Arduino IR Remote LED (Final LED-only Code)
// IR Code Scanner (IRremote >= 3.x) #include <IRremote.hpp> const uint8_t IR_RECEIVE_PIN = 2; // IR OUT → D2 const uint8_t LED_PIN = 8; // LED → 220Ω → D8 // Replace with your scanned value: const uint32_t CMD_LED_TOGGLE = 0x45; // example NEC command // Alternative if command is unstable: // const uint32_t RAW_LED_TOGGLE = 0x00FFA25D; // example 32-bit RAW bool ledOn = false; void setLED(bool on){ ledOn = on; digitalWrite(LED_PIN, ledOn ? HIGH : LOW); } void setup(){ pinMode(LED_PIN, OUTPUT); setLED(false); Serial.begin(115200); IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); Serial.println(F("LED-only mode. Press your toggle key.")); } void loop(){ if(!IrReceiver.decode()) return; if(!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)){ uint32_t cmd = IrReceiver.decodedIRData.command; uint32_t raw = IrReceiver.decodedIRData.decodedRawData; if (cmd == CMD_LED_TOGGLE) setLED(!ledOn); // else if (raw == RAW_LED_TOGGLE) setLED(!ledOn); // use if needed Serial.print(F("[IR] cmd=0x")); Serial.print(cmd, HEX); Serial.print(F(" raw=0x")); Serial.println(raw, HEX); } IrReceiver.resume(); }
How the LED-only Version Works
- The IR receiver demodulates the 38 kHz signal into a digital frame.
- The library provides
protocol
,command
وdecodedRawData
. - On a match with your saved button, the sketch toggles the LED on D8.
- Repeat frames are ignored to prevent multiple triggers on long press.
Upgrade — Arduino IR Remote LED + Servo Door
Now extend the build: keep the LED logic, add a servo, and map two more keys (OPEN/CLOSE). The LED will automatically show the door status (ON = open, OFF = closed).
Extra Wiring (add to LED-only)
- Servo signal → D9 (PWM)
- Servo +5 V → external 5 V, GND → external GND
- Common ground: connect the servo supply GND to Arduino GND.

Final Combined Code — LED + Servo Door
// Arduino IR remote LED + Servo Door (IRremote >= 3.x) #include <IRremote.hpp> #include <Servo.h> const uint8_t IR_RECEIVE_PIN = 2; // IR OUT → D2 const uint8_t LED_PIN = 8; // LED → 220Ω → D8 const uint8_t SERVO_PIN = 9; // Servo signal → D9 // === Replace with your scanned values === const uint32_t CMD_LED_TOGGLE = 0x45; // LED toggle const uint32_t CMD_OPEN = 0x46; // door open const uint32_t CMD_CLOSE = 0x47; // door close // RAW fallback (uncomment if needed): // const uint32_t RAW_LED_TOGGLE = 0x00FFA25D; // const uint32_t RAW_OPEN = 0x00FF629D; // const uint32_t RAW_CLOSE = 0x00FF22DD; const int OPEN_ANGLE = 20; // tune to your linkage const int CLOSE_ANGLE = 90; const int STEP_DEG = 2; const int STEP_MS = 10; Servo door; bool ledOn = false; int curAngle = CLOSE_ANGLE; void setLED(bool on){ ledOn = on; digitalWrite(LED_PIN, on ? HIGH : LOW); } void moveServoTo(int target){ int dir = (target > curAngle) ? 1 : -1; while(curAngle != target){ curAngle += dir * STEP_DEG; if((dir > 0 && curAngle > target) || (dir < 0 && curAngle < target)) curAngle = target; door.write(curAngle); delay(STEP_MS); } } void setDoorOpen(bool open){ moveServoTo(open ? OPEN_ANGLE : CLOSE_ANGLE); setLED(open); // LED shows door status: ON=open, OFF=closed } void setup(){ Serial.begin(115200); pinMode(LED_PIN, OUTPUT); setLED(false); door.attach(SERVO_PIN); door.write(CLOSE_ANGLE); IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); Serial.println(F("LED + Servo mode. Use TOGGLE / OPEN / CLOSE keys.")); } void loop(){ if(!IrReceiver.decode()) return; if(!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)){ uint32_t cmd = IrReceiver.decodedIRData.command; uint32_t raw = IrReceiver.decodedIRData.decodedRawData; if (cmd == CMD_LED_TOGGLE) setLED(!ledOn); else if (cmd == CMD_OPEN) setDoorOpen(true); else if (cmd == CMD_CLOSE) setDoorOpen(false); // RAW fallback: // if (raw == RAW_LED_TOGGLE) setLED(!ledOn); // if (raw == RAW_OPEN) setDoorOpen(true); // if (raw == RAW_CLOSE) setDoorOpen(false); } IrReceiver.resume(); }
How the Upgrade Works
- The IR receiver reads your three keys (LED, OPEN, CLOSE).
- The sketch moves the servo between OPEN and CLOSE using small steps to reduce noise and stress.
- The LED mirrors the door state, so the status is visible at a glance.
- Repeat frames from long presses are filtered to prevent chattering.
Build & Test Procedure
- Run the scanner and note your keys (LED, OPEN, CLOSE).
- Upload LED-only code → verify clean toggling on D8.
- Add servo wiring, then upload the combined sketch.
- Test the servo off the mechanism; attach the horn and fine-tune angles.
- Check that the LED follows the door state in servo mode.
Downloads & Links
- Previous project (internal): Arduino Password Door Lock
- Code in this post: Use the Scanner, LED-onlyو Combined sketches above.
- External: Official IRremote documentation/repository.
الخاتمة
You now have a low-voltage Arduino IR remote LED controller and a straightforward upgrade to a servo door that reuses your earlier door mechanism. The LED provides a clear status indicator, and the modular approach makes it easy to extend.