After completing the first LED project, we can now use an Infrared Sensor to control our lights.

We need to connect our Infrared receiver to our breadboard, each Infrared receiver has 3 pins, one for signal, one for ground, and one for VCC(power), the Arduino board has multiple ground pins, use one of them and connect it to your ground pin on the receiver, connect one of the Arduino number pins into the signal, and plug in the 5v power to the Infrared VCC pin.

Now, In order to program the Infrared Receiver, we need to find the button codes for the remote control that will send information to the Infrared Receiver. To do this, we need to use the IRremote Library, you can view it here.

Once you get the Library, you want to open up your Arduino IDE and include the library. To do this go to Sketch -> Include Library -> Add .ZIP Library, then find the IRremote file.

Now we need to use this library to view key codes for our Infrared Remote controller.

#include <IRremote.h>

const int RECV_PIN = 7; //create receiver object
IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop(){
   if (irrecv.decode(&results)){
   Serial.println(results.value, HEX);
   irrecv.resume(); //resume and receive next value
}
}

Press each key on the remote to find the values, Check the values using your IDE’s serial monitor. I will be using button “7” to control my LED which is 0xFF42BD

Once you have what you need, lets start connect our LED to our breadboard. connect the LED to ground and a pin. Create a variable for your LED pin, I will use pin 8 for the Led. Create another variable for the receiver, I will use pin 7.

#include <IRremote.h>

const int LED = 8;
const int RECV_PIN = 7;

void setup(){

}

void loop(){

}

Next we will use IRrecv, which will have been define in the IRemote Library and create a variable called irrecv and when we create this, we need to define what pin it is connected to, which will be pin 7 in this case.

#include <IRremote.h>

const int LED = 8;
const int RECV_PIN = 7;
decode_results results;
IRrecv irrecv(RECV_PIN);

void setup(){

}

void loop(){

}

Next we will go to our setup and set the LED(which we have already defined) to output. We will use pinMode to do this. We will also start the IR receiver.

#include <IRremote.h>

const int LED = 8;
const int RECV_PIN = 7;
decode_results results;
IRrecv irrecv(RECV_PIN);

void setup(){
   pinMode(LED, OUTPUT);
   irrecv.enableIRIn(); //start the IR receiver
}

void loop(){

}

Next we will go to our loop, we need to make an if statement that will respond to when the function “decode” with the variable results as defined earlier. We will also use switch to be followed by a case statement, which will be the code for the button 0xFF42BD, use digitalWrite and a delay to set up the LED to turn on when the button is pressed.

#include <IRremote.h>

const int LED = 8;
const int RECV_PIN = 7;
decode_results results;
IRrecv irrecv(RECV_PIN);

void setup(){
   pinMode(LED, OUTPUT);
   irrecv.enableIRIn(); //start the IR receiver
}

void loop() {
   if (irrecv.decode(&results)){ //call to function decode
      switch(results.value){
         case  0xFF42BD: //Keypad button "7"
         digitalWrite(LED, HIGH);
         delay(2000);
         digitalWrite(LED, LOW);
       }
   irrecv.resume(); //continue receiving
    }
}

Categories: Arduino