As an Amazon Associate we earn from qualifying purchases.
Most homes have a drawer full of remotes, and most of them sit unused because the TV remote is already in your hand. With an Arduino and a cheap infrared (IR) receiver, you can reuse that remote to switch other devices on and off, such as an LED, a lamp or a fan. This article shows the idea, the wiring and a working sketch.
The video below shows the project working. The code and the library link follow underneath.
How a TV remote controls other devices
A TV remote has an infrared LED inside. When you press a button, it sends a short burst of pulses that is unique to that button. The pulses are switched on and off very quickly at a carrier frequency, which is commonly around 38 kHz, so the receiver can ignore normal room light.
An IR receiver module on the Arduino picks up these pulses and turns them into a clean digital signal. The IRremote library then decodes that signal into a number. Your sketch compares that number with the value you expect and decides what to do, for example turn a pin on.
The Arduino does not care what the remote was originally made for. Any button on any remote that the library can decode will work, so you can use the remote of an old TV, a set-top box or a DVD player.
What you need
- An Arduino board (the sketch below works with an Uno-style board)
- A 3-pin IR receiver module, or a bare receiver such as the common 38 kHz types
- Any TV or similar IR remote
- An LED, which can be the one built into pin 13 on most Arduino boards
- Jumper wires and a USB cable
- The IRremote library (link at the bottom of this article)
Wiring the IR receiver
IR receiver modules usually have three pins: signal output, ground and power. The order is not the same on every part, so check the markings on your module or its datasheet before you connect it.
- Connect the receiver’s power pin to 5V on the Arduino.
- Connect the receiver’s ground pin to GND.
- Connect the receiver’s signal pin to digital pin 11, which is the pin used in the sketch.
- Use the LED on pin 13. On most boards it is already built in, so no extra wiring is needed.
The Arduino sketch
The sketch listens for IR codes and prints every code it receives to the Serial Monitor. If the code matches the one stored in the sketch, it blinks the LED on pin 13.
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
if(results.value==16713975)//checks a value in my remote.look serial monitor for you remote //value
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000);
}
Serial.println(results.value);
irrecv.resume(); // Receive the next value
}
}
Download ir header from https://github.com/shirriff/Arduino-IRremote
How the code works
- IRrecv irrecv(RECV_PIN) creates a receiver object on pin 11.
- irrecv.enableIRIn() starts the receiver in setup().
- irrecv.decode(&results) returns true when a new code has arrived and stores it in results.
- results.value holds the decoded number. The sketch compares it with 16713975.
- irrecv.resume() tells the library to get ready for the next button press. Without it, the sketch only reads one code.
The number 16713975 belongs to one button on the remote used by the author. Your remote will almost certainly send a different number, so you need to find your own.
Find the code for your own remote
- Upload the sketch to the board.
- Open the Serial Monitor and set it to 9600 baud.
- Point the remote at the receiver and press the button you want to use.
- Note the number that appears in the Serial Monitor.
- Replace 16713975 in the sketch with that number and upload it again.
If you hold a button down, many remotes send a special repeat code instead of the original one. If you see the same odd number over and over, that is probably the repeat code. Tap the button once to see its real value.
Going from an LED to real devices
Once the LED responds to your button, you can switch something larger. An Arduino pin cannot power a lamp or a fan directly, so you need a relay module or another suitable switching circuit between the pin and the load.
Use a different button for each device and add one if block per button. If you want to see how to control a light and fan this way, we have a full project on it. The same idea also works on other boards, and our home automation build shows an MSP430 with an IR remote and a relay.
Safety note: mains voltage can cause serious injury. If you are not experienced with it, test with low-voltage loads such as LEDs or a small 5V or 12V device, and use an enclosed, properly rated relay module before you connect anything to the wall supply.
Tips for better results
- Avoid long delays. The delay() calls in the sample sketch freeze the Arduino for two seconds, so it misses any button pressed in that time. For real projects, switch a pin on or off and leave it, or use millis() for timing.
- Keep a clear line of sight. IR needs a fairly direct path, and strong sunlight can disturb the receiver.
- Keep the wires short between the receiver and the board to reduce noise.
- Store the codes as named constants at the top of the sketch so the code is easier to read when you add more buttons.
A note on library versions
The sketch above is written for the older IRremote library linked in this article, which uses decode_results and results.value. Newer releases of IRremote changed how decoding works, and the same code may not compile with them. If you installed the library through the Library Manager and get errors, either use the older version from the link or follow the examples shipped with the newer version.
Other ways to control devices
IR is simple and cheap, but it only works in the same room. For radio-based options, read our guide to Arduino wireless remote control. If you want to switch devices when you are away from home, see Arduino remote control over the internet.
FAQ
Can I use any TV remote with an Arduino?
Most remotes that use infrared will work, because the receiver only sees pulses. The library must be able to decode the remote’s protocol. If the Serial Monitor shows a code when you press a button, you can use it.
Why does my sketch show a different number than 16713975?
Each remote and each button sends its own code. The number in the sample belongs to one button on the author’s remote. Read your own value from the Serial Monitor and put it in the sketch.
Can I control a lamp or fan with this?
Yes, but through a relay module or another proper switching circuit, not straight from an Arduino pin. Take care with mains voltage and test with a low-voltage load first.
Why does the Arduino sometimes ignore a button press?
The most common cause is the delay() calls in the sketch, which stop it from reading the receiver. Weak aim, bright sunlight and a wrong receiver pin order can also cause missed presses.