How to Use IR Remotes With Arduino

574,587

454

392

19 year old hobbyiest and future EE.

I'm tired of these complicated tutorials on how to use certain things. I like simple, easy to understand, step by step instructions. My biggest problem was with IR and POV*. I've finally mastered how to control my project with any TV remote in a few minutes. In this i'ble I'm going to show you simple, step by step instructions on how to control just about anything with your IR remote.

By the way this tutorial is new and updated and works! Last update [ 10/12/2015 ]

IMPORTANT NOTICE:

With the latest release of the Arduino IDE there is a conflicting library that renders this tutorial void. If you'd still like to use this tutorial delete the library called "RobotIRemote". This file will be in the Arduino program. Instructions for deleting this file is in step 3.

This library is for a specific robot that Arduino/venders sell. Unless you intend on buying that shield you can delete that folder and continue with this set of instructions. You can always download it again later!

Step 1: Ingredients:

Electronics:

  • Arduino
  • Any IR remote
  • IR receiver
  • Breadboard
  • Jumper Cables
  • LED

Step 2: Downloads

Here is the link to all the downloads in this instructable.
(Sorry for some odd reason the link won't work so just copy and paste it in your browser)

http://www.mediafire.com/download/cv0r5191fvtq0y2/IR_I'ble_Package.zip

Step 3: Conflicting RobotIRremote Library

Arduino has been making some changes to the IDE, and now there is a conflicting IR library. I won't be using this library, because I still want to use the IRremote by Ken Sheriff. I've had great success with it in the past, and so have many others. So let's get this file deleted.

Mac: Applications/Arduino (right mouse click -->show package contents) /Contents/Java/libraries/RobotIRremote

Windows: C:\Program Files (x86)\Arduino\libraries\RobotIRremote

Once you have located the folder RobotIRremote, delete it. Restart the Arduino IDE and your RobotIRremote library should be gone.

Step 4: All Those Remotes!

TV Remotes, CD player remotes, heater remotes, DVD player remotes, all those remotes! Many people just have old remotes laying around because they item that the went to broke. I have collected quite a few remotes over the past week. I just asked all my friends if they had any old remotes laying around and sure enough I collected about 7 of them. So finding a remote isn't very hard. I good option if you want a professional looking one is to buy the specialty MP3 player remote. I have one because it came with my Arduino kit.

Here are a few good cheap remotes that you can get.

Step 5: Installing the IR Library

The very first thing that we need to do associating with Arduino is to download the IR library. To make things simpler, I have included a .zip of the IR library. Download it to your computer, unzip it, then place it in your Arduino libraries folder. Don't know where it is?

Open up the Arduino IDE and on the menu select Sketch>IncludeLibrary>Add Library and select the 'IRremote' folder. If you are on a PC you need to delete the mac content within the IRremote folder.

For manual placement go to

Mac: Applications/Arduino (right mouse click -->show package contents) /Contents/Java/libraries/
Windows: C:\Program Files (x86)\Arduino\libraries\

For resources here is the Github
https://github.com/shirriff/Arduino-IRremote

Here is the IR remote folder

http://www.mediafire.com/download/jd5j7911amju36g/IRremote.zip

Step 6: Recognizing IR Signals

You now need to download the IR decoder sketch.
http://www.mediafire.com/view/6qnsndqp9a838xe/Decode_IR.ino


I totally re-edited the github sketch to make it work. I put all the credits in the sketch. The sketch is attached to this step or you can get if from step 2. Upload this sketch to your Arduino. Now hook up the IR sensor.

The IR sensor's pins are attached to Arduino as so: (from left to right with the sensor's head facing you)


(Vout) Pin 1 to pin 11(Arduino)
(GND) Pin 2 to GND(Arduino)
(Vcc) Pin 3 to 5v(Arduino)


Now open up granola cereal, wait no, I meant serial monitor. Aim your remote at the sensor and press the POWER button. You should see a list of numbers show. Now you can see we got the numbers:

16753245
4294967295
4294967295
4294967295

Notice you if hold down whatever button you were pressing that the second number just repeats itself.

16753245
4294967295
4294967295
4294967295
4294967295
4294967295
4294967295
4294967295

Note what happens if you press another button

16736925
4294967295
4294967295
4294967295
4294967295

You get a different first number, and the same second number!
Obviously, we just need to use the first number. Try hitting different buttons on the remote. You will notice that each different button has a different first number.

So what you need to do is to open up serial monitor press each button, carefully recording the first number. For example: I press the power button and the mode button, so in my text editor program, I'll type,

Power button = 16753245
Mode button = 16736925

And you do this for every button you need!
With this knowledge we can construct some code!

Step 7: Arduino Test Code

Upload this sketch to your Arduino.
http://www.mediafire.com/view/hmv13ynbihed0eg/Test_LED.ino
/*
 Some Sample code of how to use your IR remote
 
 * Lets get started:
 
 The IR sensor's pins are attached to Arduino as so:
 Pin 1 to Vout (pin 11 on Arduino)
 Pin 2 to GND
 Pin 3 to Vcc (+5v from Arduino)

*/

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
int LED = 13;    // LED pin  
IRrecv irrecv(IRpin);
decode_results results;

boolean LEDon = true; // initializing LEDon as true

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED, OUTPUT);
}

void loop() 
{
   
  if (irrecv.decode(&results)) 
    {
      
      irrecv.resume();   // Receive the next value
    }
  
   if (results.value == 0)  // change zero to your IR remote button number
     {
       if (LEDon == true)   // is LEDon equal to true? 
         {
           LEDon = false;   
           digitalWrite(LED, HIGH);
           delay(100);      // keeps the transistion smooth
            
         }
         
        else
          {
            LEDon = true;
            digitalWrite(LED, LOW);
            delay(100);
            
          }
          
     }

}


This code is to turn an LED on and off with the same button. Notice this line in the code.

if (results.value == 0)  // change zero to your IR remote button number

You will change 0 to whatever number your IR remote button makes. For instance, my power button's number is 16753245, so I will change the code to this:

if (results.value == 16753245)

results.value is just what you see in the serial monitor. So if I say, if results.value is equal to 16753245, then do such and such. Make sense?! So the rest of the code if for making the same button turn an LED on and off. When the LED if off and you hit the button it turns on and if the LED is on and if you hit the same button again it turns off.

Step 8: More Code!

So what if we want each button on the remote to do a different function? Making a lot of 'if' statements would be way too much typing! So lets simplify this with a switch/case statement.

switch(results.value)

We are going to put this after the void loop and after the first if statement. Here's the whole thing-

void loop() 
   
  if (irrecv.decode(&results)) 
    {
      irrecv.resume();   // Receive the next value
    }
  
  switch(results.value)
   {

So now we need finish the code. If you don't know what the switch/case are see http://arduino.cc/en/Reference/SwitchCase
Here is the final code. You can keep on adding cases. Now where it says 'case 03' you change the '03' to whatever button number you wish. For instance, the first case could say:

case 16753245: 
// do this
break;
And we just keep on adding different button numbers for to do different things.

Here is the link to the code below.
/*
 Some Sample code of how to use your IR remote
 
 * Lets get started:
 
 The IR sensor's pins are attached to Arduino as so:
 Pin 1 to Vout (pin 11 on Arduino)
 Pin 2 to GND
 Pin 3 to Vcc (+5v from Arduino)

*/

#include <IRremote.h>

int IRpin = 11;  // pin for the IR sensor
int LED = 13;    // LED pin  
IRrecv irrecv(IRpin);
decode_results results;

boolean LEDon = true; // initializing LEDon as true

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED, OUTPUT);
}

void loop() 
{
   
  if (irrecv.decode(&results)) 
    {
      
      irrecv.resume();   // Receive the next value
    }
  
  switch(results.value)
 {

  case 01:
  // do this
  break;
  
  case 02:
  // do another thing
  break;
  
  case 03:
  // feed my dog for me
  break;
  
  default:
  digitalWrite(LED, HIGH);
  
  }
  
}

Step 9: Conclusion:

I tried to simplify this as much as I can so that you can be controlling your projects with your TV remotes tomorrow evening.
If you don't understand anything please ask me!

Don't forget to give me a vote!

Step 10: Troubleshooting

Sometimes things don't go as planned so I've added a this step to help those that can't get their project to work. I want all reader to be able to experience the awesomeness of this!!! :D

If the value you receive for results.value is letters and numbers like this 'FF01XE' then you must put a 0x in front of that code to get it work work, like this: 0xFF01XE. So you project will read:

if (results.value == 0xFF01XE)
{
  feed the dog();
}

"My project isn't working!"

  1. READ THE IMPORTANT NOTICE IN FIRST STEP OF THIS I'BLE
  2. Rewire the sensor. Make sure it is wired properly. You may need to look online for your sensor correct pinout. Every sensor will not be the same as mine. Google the datasheet for your specific sensor.
  3. Make sure your remote is working properly. You can test this by pointing the remote at a camera and you should be able to see a dim blue/purple light flashing. Also try a second remote.
  4. Make sure the code works is the way I uploaded it. If you changed it you may not have the correct code.
  5. Retest all connections.

Well I hope these steps help if you still have problems after going through this list PM me or comment.

15 People Made This Project!

Recommendations

  • Optics Contest

    Optics Contest
  • Plastics Contest

    Plastics Contest
  • Make it Glow Contest 2018

    Make it Glow Contest 2018

392 Discussions

1
None
boolf

3 years ago

JUST PERFECT!!!

Best Arduino related tutorial and "instructable" that I read so far, (and I assure you I read a lot).

Well organized, simple, straight forward, contains all what is required and with a light touch of humor.

2 replies
0
None
HavocRCboolf

Reply 3 years ago

Thanks! I'm glad to help where I can!

0
None
Z1745566HavocRC

Reply 3 years ago

You are amazing, made my work so easy. Thanks alot :)

0
None
SteveR4

3 years ago on Step 9

This was SOO much better than Adafruits tutorial that has massive arrays you have to capture and then use for each button. Normally they have the BEST stuff for easy understanding. HOWEVER this was by FAR the easiest for my remotes!! The library is about twice the size of the ada one BUT this is so much better I will take the loss on Flash for this ease!! :)
I only have one small glitch and it may just have to do with the remote I'm using BUT, if you click the remote too fast the read code loops forever. So the led just flashes all on it's own, until you do a long press on the remote. Again not sure if this is all remotes or just the one I chose. But I plan on adding this to my daughters Tiara project seen below. I want to cram a small tiny remote into a cheap wand, so the wand will control the Crown / Tiara.. Thanks AGAIN!! ~Steve~

5 replies
0
None
HavocRCSteveR4

Reply 3 years ago on Step 9

Hey thanks so much! I'm really glad that I was able to help you out!

Yeah actually it does the same thing for everyone. That was the main complaint I got because of the looping- it's not my library!

The fix is really simple, after the if statement {
if (results.value == X) }
add a small delay, like { delay(25); }.

This worked for me to keep it out of the loop, but don't put the delay too high. I forgot the actual milliseconds I used to fix it. If you go too high then sometimes when you press the button at the same time the arduino is frozen because of the delay. If you go too low then it doesn't fix the loop problem. Experiment, you'll get it.

And very nice project BTW!

Instead of cramming a remote into a wand you should just send a signal using an ATtiny85/45/44/24/ and the send feature of IR library.

I'm still here if you need any help with the IR part. . . :D

0
None
giladgbergHavocRC

Reply 3 years ago on Step 9

hi i used:

results.value=0;

on the beginning of the void loop... fixed it for me :)

0
None
SteveR4HavocRC

Reply 3 years ago on Step 9

After beating my head against the wall for 20 mins trying to solve the flashing with the delay (Which I could not get to work) I found the PERFECT solution that works 100% everytime Change this line to:

if (results.value == 16236607) {results.value = 0; //Do something here}
By resetting the "results" back to 0 it cures the looping, because it can't "see" the correct value again until it gets the real code again from the sensor.
Thanks again you may want to update the code you shared with this correction and the problem is gone for good :)

0
None
HavocRCSteveR4

Reply 3 years ago on Step 9

Duuude of course! I can't believe I didn't think of that! I never actually used the code (like in a project) other than testing it briefly for the i'ble. I'll go back and change it. Thanks! You could get really fancy and go with an ATtiny13-20SU. I just got a shipment of 20 in the mail! :D

0
None
SteveR4HavocRC

Reply 3 years ago on Step 9

Awesome thanks for the hints!!
Yeah no clue why I didn't think of just using the ATTiny!?!?!?! Perfect Idea!!!

0
None
tnowroz

3 years ago on Introduction

Really Good Tutorial here bro. Finally made the IR sensor work! :D

But for some reasons, many of the links are not working properly. If would be awesome if you could fix those.

Anyway, Thanks for this! Really appreciate it as a beginner with Arduino! :)

1 reply
0
None
HavocRCtnowroz

Reply 3 years ago on Introduction

Thanks! I'm not sure what you are talking about the links though. I just went through and checked all of them and they are all working. Maybe you might just have to copy the address and paste it in your browser's address bar.

0
None
gbwith

Question 14 days ago on Introduction

I need help. I'm having trouble combining 2 sketches. I'd like to remote control a strobe effect on a lighthouse. I can turn on and off an LED with my remote but I can't get the strobe effect into the remote sketch. The strobe sketch uses a for statement and analog write yo control the LED. I can send both sketches if you'd like thank you

5 more answers
0
None
HavocRCgbwith

Reply 14 days ago

Go ahead and post them here I'll take a look

0
None
gbwithHavocRC

Reply 11 hours ago

Thanks for all your work. I should have been a little clearer on my objective. I just want a remote to turn on and off a strobe (or fade) effect in the for loop area either with the same button or by using 2 separate remote buttons. The following is what I've been able to get me close. I can get it turn on and do as I wanted however I can't get it to shut off by remote.

in the else statement the value > 1 was changed from = 0 because it would on go through 1 cycle and end with it > 1 it would continue the cycling. Hope this makes sense. Any ideas. Here's what I have.

#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int ledPin = 5; // Led connected to digital pin 11
int cycle = 75;
int strobe = cycle * 20; // calculate strobe delay
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
#define code1 41565 // code received from Power Button

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup() {
pinMode(ledPin, OUTPUT);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
// variable to remember the last value from the remote
static int value = 0;
// if remote keypress received
if (irrecv.decode(&results))
{
// if currently not fading
if (value == 0)
{
value = results.value;
}
else
{
value > 0; // this is what was = 0 but didn't work
}
irrecv.resume();
}
// take action
switch (value)
{
case code1:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
analogWrite(ledPin, 255);
delay(strobe);
analogWrite(ledPin, maxFade);
for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
}
}

0
None
gbwithHavocRC

Reply 13 days ago

I've combined as much as my limited knowledge allowed me without getting an error. Thanks for looking.

sketch 1. The remote sketch
/*
source: www.electroschematics.com
You'll need to change the led pins and the codes
accordingly to your configuration and IR remote
*/

#include
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int cycle=75;
int strobe=cycle*20; // calculate strobe delay
int ledPin=11;
int maxFade=75; // maximum brightness before strobe
int fadeValue;


int itsONled[] = {0,0,0,0,0};

/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 41565 // code received from Power Button

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{

irrecv.enableIRIn(); // Start the receiver
pinMode(ledPin, OUTPUT);

}

void loop() {

if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(ledPin, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(ledPin, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}

break;
}

irrecv.resume(); // Receive the next value
}
}




STROBE. Sketch. 2. The strobe and or fade
int cycle=75;
int strobe=cycle*20; // calculate strobe delay
int maxFade=75; // maximum brightness before strobe
int ledPin = 11; // Led connected to digital pin 11
int fadeValue;
void setup() {
// nothing happens in set
}
void loop() {
// fade in from min to max in increments of 2 points:
for (int fadeValue = 0 ; fadeValue <= maxFade; fadeValue += 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
analogWrite(ledPin, 255); // simulate a rotating beacon catching your eye
delay(strobe); // hold full brightness for strobe delay
analogWrite(ledPin, maxFade);
// fade out from maxFade to min in increments of 2 points:
for (int fadeValue = maxFade ; fadeValue >= 0; fadeValue -= 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
}

0
None
HavocRCgbwith

Reply 19 hours ago

Sorry bud I've been on vacation. I edited some code for you, it should get you started and on the right track. I was not able to upload and see if it works, as I don't have an Arduino on hand to test with. Hint, since the I'ble's text editor messes up text, once in arduino, select all the code and hit control+T (cmd+T on Mac I believe) to auto format everything. It's also under one of the drop down menus.

//#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of IR Receiver
int cycle = 75;
int strobe = (cycle * 20); // calculate strobe delay
int ledPin = 11;
int maxFade = 75; // maximum brightness before strobe
int fadeValue;
int IRvalue;
// I deleted your array, as you only had 1 led and it was doing anything
#define code1 41565 // code received from Power Button
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
irrecv.enableIRIn(); // Start the receiver
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results)) // keep this the same, don't put stuff in this loop.
{
irrecv.resume(); // Receive the next value
}
IRvalue = results.value;
switch (IRvalue)
{
case XXXXXXXX:
fade();
break;
case XXXXXXXX:
strobe();
break;
case XXXXXXX:
//strobe_fade(); add another loop to both fade and strobe LED.
// note: you cannot call both fade() and strobe() to get it to fade and strobe,
// it can only do one or the other. You must right code to make it do both.
break;
default: // if no values have been recieved, turn the LED off.
digitalWrite(ledPin, LOW);
break;
}
}
void fade() {
// fade in from min to max in increments of 2 points:
for (int fadeValue = 0; fadeValue <= maxFade; fadeValue += 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
for (int fadeValue = 255; fadeValue >= 0; fadeValue -= 2)
{
analogWrite(ledPin, fadeValue);
delay(cycle);
}
}
void strobe() {
analogWrite(ledPin, 255); // simulate a rotating beacon catching your eye
delay(strobe); // hold full brightness for strobe delay
analogWrite(ledPin, maxFade);
// fade out from maxFade to min in increments of 2 points:
for (int fadeValue = maxFade; fadeValue >= 0; fadeValue -= 2) {
// sets the value (range from 0 to maxFade):
analogWrite(ledPin, fadeValue);
// wait for "cycle" milliseconds to see the dimming effect
delay(cycle);
}
}

0
None
gbwithHavocRC

Reply 6 days ago

did you happen to look at those sketches? I looked at what I had sent you and it looked like a mess, however where you originally posted the IR Remote I did see where I (gbwith) commented and the sketches look a little more readable. Thank you

0
None
observerms

3 days ago

(1) Is it possible to read the arduino code in iRremote.h ?
(2) Is " irrecv." has black printout NOT red so it doesn't seem to be part of the IRremote.h does that make it a USER optional name ? or is it essential part of
< irrecv.enableIRIn(); " and other similar structures.

1 reply
0
None
yckelvin

Question 15 days ago on Step 10

Nice to read this post and I can record the IR data using this code but sometimes I will get FFFFFFFF, what does it mean?

I record all my remote button code and modify the code to turn on a motor fan, I can turn it on but cannot turn off. Do you have another project with the transistor?

Best regards,

Kelvin