Arduino projects: IR receiver

One of the most used types of wireless communication for TV remote control is IR (Infrared). This Arduino project is going to be about using IR.

One of the places you will also always find an IR controlled device is in AC (Air Conditioning system) and projectors.

Infrared is the perfect low energy technology, it works on short distances. It’s easy to build and cheap to add to your project. You can use it to control robots, smart home devices or relays.

In this session, we will learn how to read hex codes of some remote buttons and use them to control LEDs.

Component needed

  • 1 x Arduino Uno Board
  • 1 x IR receiver
  • Jumper wires
  • 4 x LEDs
  • 1 x Breadboard
  • 4 x Resistors of 220 ohms

We also need an additional library for our IR receiver to work properly. You can simply type “IR remote” in the library manager and select IR remote by Shirriff. If you don’t know how to add a library to your sketch, I will suggest you read this article.

There will be 2 codes for this project but one schematic.

We will connect LEDs now but they wouldn’t be used in the first code. We will use them in the second one. If you use a different IR receiver, it is possible that you will need to add resistors, you should check the datasheet of the IR receiver.

When everything is ready, we can upload the first code. It will display the data that have been received by IR receiver on the serial port. You need to copy those code and save them in a text file, we will use them later.  The code is in HEX format, don’t worry if they do not make sense to you at first sight.

#include <IRremote.h>
int IR_PIN = 9;

IRrecv irrecv(IR_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); 
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); 
  }
  delay(10);
}

Now we can upload the second code that will control the LEDs that we’ve connected previously. To do so, we have to copy the codes from the remote buttons we’ve saved in the text file. Paste them into the command1, command2, command3 variables. After doing so, we can upload the code to the Arduino board.

The LEDs should turn ON and OFF when you press the button we’ve mapped earlier on the remote control.

#include <IRremote.h>
int IR_PIN = 9;

IRrecv irrecv(IR_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); 
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
}

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

    //in each case instead of those hex number you have to put yours remote keys codes.
    switch(results.value){

      case 0x49641316:
      digitalWrite(2, !digitalRead(2));
      break;

      case 0x1CF35919:
      digitalWrite(3, !digitalRead(3));    
      break;
      
      case 0xF14FF65A:
      digitalWrite(4, !digitalRead(4));
      break;
      
      
      case 0x3E81CB71:
      digitalWrite(5, !digitalRead(5));   
      break;
    }
    irrecv.resume(); 
  }
  delay(10);
}

You can check the video above to have a glimpse of how our sample worked.

tchouken2

Related Posts