How to use IR remote control with Arduino

How to use IR remote control with Arduino

In this Tutorial we are going to explain how to use an IR remote control module with Arduino!

We are going to make a simple sketch in which we will associate to each transmitter’s button a different action!

First of all we will have a quick look at how IR works

What is IR?

What is IR? IR means infrared and it is a form of light.

The difference between IR light and visible light is the frequency and wavelength. Infrared radiation are outside the range of visible light, so we can’t see it.

But if you want to check that your transmitter works (and if you want to know how it works) you just have to see it through your smartphone’s camera.

In that way you will be able to see it, and it will be like a blinking purple light!

How IR transmitter/receiver works?

The transmitter module looks like a black LED, but as said before, you have to see it through your smartphone’s camera to see how it works. Then the receiver will receive the signal.

Each time you press a button on the transmitter a unique hexadecimal code is generated.

The receiver receives that hexadecimal code. Then you have to associate to each code a different action in Arduino.

Schematic

ir remote control
ir remote control
ir remote control
Second

Sketch

First of all we have to add the IRremote.h library.

To add the library you can follow this tutorial -> Add a library to Arduino IDE.

Then we can write this sketch.

#include <IRremote.h>

int RECV_PIN = 7;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
}

With this sketch we will read on the serial monitor the hexadecimal code associated to the button.

Now upload the code, then press any button and copy the hexadecimal code!

Let’s make a second sketch!

#include <IRremote.h>

const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int redPin = 10;
const int greenPin = 11;


void setup(){
  irrecv.enableIRIn();
  irrecv.blink13(true);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop(){
    if (irrecv.decode(&results)){

        switch(results.value)
        {
          case 0xFF38C7: //Keypad button "5"
          digitalWrite(redPin, HIGH);
          delay(2000);
          digitalWrite(redPin, LOW);
          break;
          
          case 0xFF18E7: //Keypad button "2"
          digitalWrite(greenPin, HIGH);
          delay(2000);
          digitalWrite(greenPin, LOW);
          break;
          }

        irrecv.resume(); 
    }
}

With this code we will set HIGH for 2 seconds the LED on pin 10 when the “5” button is pressed, and we will set HIGH for 2 seconds the LED on pin 11 when the “2″ button is pressed.

I hope that now you kno how to use IR remote control with Arduino!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

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

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: