Friday, October 31, 2014

How to Interface IR Remotes with Arduino

                                How to Interface IR Remotes with Arduino

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.

Step 1: Ingredients:

Components :
For Interfacing IR Remotes with Arduino
  • Arduino
  • Any IR remote
  • IR receiver
  • Breadboard
  • Jumper Cables
  • LED

And here is the Make-To-Learn contest questions! - don't forget to vote!


What did you make?
Well I didn't make anything specific in this instructable but it is more of a guide to how to make your other projects 'wireless'.
My projects works by taking TV remotes and other remotes, converting their signals to numbers, and using them.
How did you make it?
I've been working on trying to use IR remotes. All the tutorials I found didn't really put the all the bits and pieces together for me. My main goal was to make an easy tutorial for others to follow.
Where did you make it?
At my computer. I am now able to control my robots and other stuff, like lights and lighting. For instance, I could make it so that whenever I hit the play button on my DVD player remote, the lights in the room dim, or go out.
What did you learn?
My biggest challenge was finding a IR decoder that worked, and then finding installing the proper library

Step 2: 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.

Step 3: Installing the IR Library

The very first thing that we need to do associating with Arduino is to download the IR library. Now just about every tutorial directed you to Github, but it took me forever to find out how to even download it. Then even after is was downloaded, it wasn't named properly. So, 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>Import Library>Add Library and select the 'IRremote' folder. If you are on a PC you may need to delete the mac content within the IRremote folder.
For resources here is the Github


https://github.com/shirriff/Arduino-IRremote

IF YOU HAVE ANY TROUBLE WITH THE .ZIP PLEASE PM ME OR COMMENT!

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

Step 4: Recognizing IR Signals

You now need to download the IR decoder sketch.



Picture of Recognizing IR Signalshttp://www.mediafire.com/view/6qnsndqp9a838xe/Decode_IR.ino













or copy and past this code








/*
 * Sketch modified by Enjoying Electronics: http://electronicsirfan.blogspot.com/2014/10/how-to-interface-ir-remotes-with-arduino.html:
 * IRremote
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html

 * Special thanks to dablondeemu http://www.instructables.com/member/dablondeemu/
 * and his instructable listed below, IR Remote Controlled Color Changing Cloud (Arduino)
 * http://www.instructables.com/id/IR-Remote-Controlled-Color-Changing-Cloud-Arduino/ 


 * 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)

*/

/*******************CODE BEGINS HERE********************/

#include <IRremote.h>

int IRpin = 11;
IRrecv irrecv(IRpin);
decode_results results;

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

void loop()
{
  if (irrecv.decode(&results))
    {
      Serial.println(results.value, DEC); // Print the Serial 'results.value'
      irrecv.resume();   // Receive the next value
    }
 
 
}








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 5: 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 6: 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.

like control Four relays .... By using this code





/*
 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 = 2;  // pin for the IR sensor
int RelayOne = 3;
int RelayTwo = 4;
int RelayThree = 5;
int RelayFour = 6;
IRrecv irrecv(IRpin);
decode_results results;
boolean RelayOneOn = true; // initializing RelayOneOn as true
boolean RelayTwoOn = true;
boolean RelayThreeOn = true;
boolean RelayFourOn = true;

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

void loop()
{
  
  if (irrecv.decode(&results))
    {
      Serial.println(results.value, DEC); // Print the Serial 'results.value'
      irrecv.resume();   // Receive the next value
    }
 
  switch(results.value)
 {

  case 16724175:     //Recorded Value of One
 {
       if (RelayOneOn == true)   // is LEDon equal to true?
         {
           RelayOneOn = false;  
           digitalWrite(RelayOne, HIGH);
           delay(100);      // keeps the transistion smooth
           
         }
        
        else
          {
            RelayOneOn = true;
            digitalWrite(RelayOne, LOW);
            delay(100);
           
          }
         
     }
  break;
 
  case 16718055:
  {
       if (RelayTwoOn == true)   // is LEDon equal to true?
         {
           RelayTwoOn = false;  
           digitalWrite(RelayTwo, HIGH);
           delay(100);      // keeps the transistion smooth
           
         }
        
        else
          {
            RelayTwoOn = true;
            digitalWrite(RelayTwo, LOW);
            delay(100);
           
          }
         
     }
  break;
 
  case 16743045:
  {
       if (RelayThreeOn == true)   // is LEDon equal to true?
         {
           RelayThreeOn = false;  
           digitalWrite(RelayThree, HIGH);
           delay(100);      // keeps the transistion smooth
           
         }
        
        else
          {
            RelayThreeOn = true;
            digitalWrite(RelayThree, LOW);
            delay(100);
           
          }
         
     }
  break;
  case 16716015:
  {
       if (RelayFourOn == true)   // is LEDon equal to true?
         {
           RelayFourOn = false;  
           digitalWrite(RelayFour, HIGH);
           delay(100);      // keeps the transistion smooth
           
         }
        
        else
          {
            RelayFourOn = true;
            digitalWrite(RelayFour, LOW);
            delay(100);
           
          }
         
     }
 
 
 // default:
 // digitalWrite(LED, LOW);
 
  }
 
}
Electronics Lab   is Created by Muhammad Irfan 

 

 


No comments:

Post a Comment