How to Use Infrared (IR) receiver module with Arduino Microcontroller

Today, we will be using the IR receiver sensor module to detect infrared signals from an old TV remote and use the HEX file generated when we send signals to the IR receiver sensor module to control simple displays like LEDs and perhaps later use it to control an electric fan. In this how to use an Infrared (IR) receiver module with Arduino Development Board wiki, we will cover understanding the IR receiver module and how to interface with the Arduino Uno. So let’s jump right into it.

How to Use IR Receiver Module with Arduino: Materials for this tutorials:

The Circuit Diagram

The IR receiver module has three terminals shown and named in the figure below:

How to Use Infrared (IR) receiver module with Arduino
IR Receiver Module

From top-left to bottom-right, the first pin S is the pin we connect to any of the digital pins or analog pins of our choice on the Arduino development board. The second pin is the Vcc pin that is hooked up to the +5V  of the Arduino development board. The third and last pin is the ground, which is connected to the header port labeled GND on the Arduino board.  The connection on breadboard is thus:

How to Use Infrared (IR) receiver module with Arduino
the circuit diagram

From the breadboard, the 5V output of the Arduino board is connected to pin 2 of the IR receiver module, both the IR receiver module and Arduino Uno board have a common ground.

How to Use Infrared (IR) receiver module with Arduino
plug in the Arduino Uno

Next, plug in your Arduino communication cable into the USB port of your PC, open your Arduino IDE, click  File, create a new file, and you can name it whatever you want, and save your sketch.

We saved ours with IR_Receiver_Test. You might want to do the same.

To use the Infrared remote on the Arduino, we need to import a special library, known as the IRremote. Once downloaded from the github page, click on the  sketch tab, include library, and Add ZIP library.

How to Use Infrared (IR) receiver module with Arduino
adding a zip file

In the list of libraries, you will find your new added library, IRremote, quickly add then and copy the code below to get your HEX codes.

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

//state the IR input to the MCU
#define RECV_PIN A1
//make it recognised to IR Lib
IRrecv irrecv(RECV_PIN);
//ask it it get results and save it
decode_results results;

void setup() {
  //enable the IR
  irrecv.enableIRIn();
  //begin the serial monitor comm
  Serial.begin(9600);
}

void loop() {
 if(irrecv.decode(&results)) 
  {
     irrecv.resume();
    //print the remote results in HEX codes
     Serial.println(results.value, HEX);
  }

Source Code Explanation

Basically, the code is used to generate your HEX codes from pointing and depressing any key on the home remote guide at the IR receiver sensor module. The brief explanation to it is thus: From line 1 to line 4, we included the IR remote libs, Only the IRremote.h header file is enough but it felt good leaving all the others there. Next, we defined the pin on the Arduino where we are connecting the signal pin of the IR of receiver, this we called Recv_Pin and we connected to analog pin 1, A1. Then we make it recognizable to the library. In the setup function, we enabled the IR receiver and begin the serial monitor and set our communication speed between the computer and the Arduino board with Serial.begin(9600) code line. In the loop function, we ask the Arduino to get results and display it on the serial monitor.

After this, we verify the code and upload it and wait while it finishes uploading.

Uploading a code on Arduino
use control U or click upload button

Open your serial monitor, by clicking on tools, serial monitor. Alternatively, you could click on the white search box.

How to Use Infrared (IR) receiver module with Arduino
where to open serial monitor

We then start getting the HEX code for each keypad on the TV remote, each key we press when we point at the IR receiver would display its own HEX code.

How to Use Infrared (IR) receiver module with Arduino
The car remote HEX code displayed along the decimal numbers

Now we can use this HEX codes to play around with the displays of our LEDs. We connected four LEDs as shown in the breadboard connection below:

four LEDs
four LEDs on a breadboard

Before we go back to our sourcecode to make some changes, we want to pick three different buttons on the old TV remote we want to use to cause changes to the way the LEDs display. after making this choice, we should save their HEX codes somewhere because we are going to be using it to in our next step.

How to Use Infrared (IR) receiver module with Arduino: If statements, and for loops

The if statements we would use here would help us to make comparisons and change the current state of the LEDs to another state when we want them. whereas the for loops would iterate our output LEDs as long as the conditions used in it is true. Otherwise it would stop executing the line of code in it.

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

//state the IR input to the MCU
#define RECV_PIN A1
//make it recognised to IR Lib
IRrecv irrecv(RECV_PIN);
//ask it it get results and save it
decode_results results;

void setup() {
  //enable the IR
  irrecv.enableIRIn();
  //begin the serial monitor comm
  Serial.begin(9600);
  for(int i = 9; i < 14; i++){
  pinMode(i, OUTPUT);
  
  }
}

void loop() {
  
    if(irrecv.decode(&results)) 
  {
     irrecv.resume();
    //print the remote results in HEX codes
     Serial.println(results.value, HEX);
  }
  if(results.value == 0x4C){
  for(int i=9; i <14; i++){
    digitalWrite(i, HIGH);
    delay(250);
      }
      for(int i=9; i <14; i++){
    digitalWrite(i, LOW);
    delay(250);
      }
  }
  delay(1000);
}

Source Code Explanation

What the sketch does from code line 18 is; make the digital pins from digital pin 9 to digital pin 13 outputs using a for loop. Line 28 through 30 ask the Arduino Microcontroller to get the HEX codes sent from the IR transmitter of the home TV remote and print it out on the Serial monitor screen.

Line 32 through 42 uses the same if statement and comparism but with two for loops to check for when a specific button on the home TV remote is press and display the LEDs in a certain effect.

To use more buttons of the TV remote to display different effects, we use this sketch below:

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

//state the IR input to the MCU
#define RECV_PIN A1
//make it recognised to IR Lib
IRrecv irrecv(RECV_PIN);
//ask it it get results and save it
decode_results results;

void setup() {
  //enable the IR
  irrecv.enableIRIn();
  //begin the serial monitor comm
  Serial.begin(9600);
  for(int i = 9; i < 14; i++){
  pinMode(i, OUTPUT);
  
  }
}

void loop() {
  
    if(irrecv.decode(&results)) 
  {
     irrecv.resume();
    //print the remote results in HEX codes
     Serial.println(results.value, HEX);
  }
  if(results.value == 0x4C)
  for(int i=9; i <14; i++){
    digitalWrite(i, HIGH);
    delay(250);
      }
      for(int i=9; i <14; i++){
    digitalWrite(i, LOW);
    delay(250);
      }

      if(results.value == 0x876){
        digitalWrite(11, HIGH);
        delay(200);
        digitalWrite(10, HIGH);
        digitalWrite(12, HIGH);
        delay(200);
        digitalWrite(13, HIGH);
        digitalWrite(9, HIGH);
        delay(500);
        digitalWrite(13, LOW);
        digitalWrite(9, LOW);
        delay(200);
        digitalWrite(10, LOW);
        digitalWrite(12, LOW);
        delay(200);
        digitalWrite(11, LOW);
             }
     if(results.value == 0x877){
      digitalWrite(13, HIGH);
      delay(200);
      digitalWrite(13, LOW);
      delay(200);
      digitalWrite(12, HIGH);
      delay(200);
      digitalWrite(12, LOW);
      delay(200);
      digitalWrite(11, HIGH);
      delay(200);
      digitalWrite(11, LOW);
      delay(200);
      digitalWrite(10, HIGH);
      delay(200);
      digitalWrite(10, LOW);
      delay(200);
      digitalWrite(9, HIGH);
      delay(200);
      digitalWrite(9, LOW);
      delay(200);
     }
  
  delay(1000);
}

The HEX codes should be prefixed with:- 0x for the MCU to recognize your if statements as shown in the source codes above. You can watch the YouTube video of it here; or simply click on the clip below.

IR receiver module with any TV remote

Conclusion

We have successfully designed and constructed the project, How to Use Infrared (IR) receiver module with Arduino. Do you think you can replicate the same thing? Or better make it smarter than our own. Let us know if you did this project. We will like to see a photograph or video.

Connect with us on Telegram, Instagram, Facebook page or WhatsApp.

Leave a Reply

Your email address will not be published. Required fields are marked *