Controlling Devices With IR Remote!

Controlling Devices With IR Remote!

In the previous article we learned about IR remotes, how they work and how to decode signals of any IR remote. If you haven't checked it already, here is a link. I recommend you read it first to better understand this tutorial. 

In this tutorial we will see how to use the decoded values to turn devices on/off, for example LEDs. Here I will demonstrate it using two LEDs. So without wasting any more time, lets get started.

Requirements: 

Now that everything is gathered, we can start with making the circuit on a breadboard.

Circuit:

IR automation, home automation, arduino project

Make the connections as in the image above. Similar to the precious tutorial the IR LED is connected to pin 2. The LEDs are connected to Pin 8 and 9. After making the connections we can upload the code and start controlling the LEDs.

Coding:

Before you upload the code, First you need to install the IRremote library. To know how to do that, check out this tutorial. After the Library is installed you can refer the code given bellow:
#include<IRremote.h>
int IRPIN = 2;  //Receiver Pin  
int Green = 8;  //Green LED
int Red = 9;  //Red LED

int flag1 = 0; //Indicates state of Green LED
int flag2 =0; //Indicates state of Red LED

IRrecv irrecv(IRPIN); //Reads the signals from receiver
decode_results results; //saves decoded results 

void setup()
{
    Serial.begin(9600);
    Serial.println("Enabling IRin");
    irrecv.enableIRIn(); //Starts receiving
    Serial.println("Enabled IRin");

    pinMode(Green, OUTPUT);
    pinMode(Red, OUTPUT);
    pinMode(Green, LOW); //Turns LEDs off by default
    pinMode(Red, LOW);
 }

void loop()
{
    if(irrecv.decode(&results))
   {
      Serial.println(results.value, HEX);
      irrecv.resume();
      if(results.value==0xFD08F7) //If this code is received
     {
        if(flag1 == 0) //If flag state is 0 (LED is off)
       {
          digitalWrite(Green, HIGH); //turns LED on
          flag1 = 1; //Sets flag as 1
       }
       else if(flag1 == 1) //if flag is 1 (LED is on)
      {
         digitalWrite(Green,LOW); //Turns LED off
         flag1 = 0; //sets flag 0
       }
     }
     if(results.value==0xFD8877)
    {
       if(flag2 == 0)
       {
         digitalWrite(Red, HIGH);
         flag2 = 1;
       }
       else if(flag2 == 1)
       {
          digitalWrite(Red,LOW);
          flag2 = 0;
        }
     }
     irrecv.resume(); //Ready to receive next data
 }
}

In the code above we have used the codes generated by IR remote. The algorithm is fairly simple I recommend you check this tutorial to learn about the flags and how to use it.
Copy and upload this code using Arduino IDE.

Testing:

After uploading the code you can start turning on off the LEDs using IR remote. 


I hope this tutorial was helpful and you can implement this in your projects like home automation. Just replace LED with Relays and you can turn on/off AC appliances.
Thats all for this tutorial, if you like it and want more feel free to follow/subscribe. 

In next tutorial we will make IR controlled Robot until then, keep experimenting ;)