You are on page 1of 6

Infrared Remote control

Wireless control will open new possibilities in Arduino. We will be able to build thousands
of things and control them in different ways.  We can use infrared, Bluetooth, Wifi, RF and
GSM communication to control a device.

In our Starter kit, we have an infrared sensor and remote. We are going to learn how to use
it.

We need the sensor and the remote. Look for these two components in your box.

The sensor is like


this.

There are 3 wires,


PWR, GND, and
SIG

Connections:  

Look at the image on the left and


connect the sensor this way.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 1
LIBRARIES:
A library is some code which is already written and available on the internet. We can install
and use, instead of writing every line ourselves.

The Arduino environment can be extended using libraries, just like most programming
platforms. Libraries provide extra functionality for use in sketches, e.g. working with
hardware or manipulating data. To use a library in a sketch, select it from Sketch > Import
Library.

Several libraries come installed with the IDE, but you can also download or create your
own. There are a lot of libraries on the web and they can make our work much easier.

In this case, we must download the IR Library. Download it here: IRremote_Library.zip


and extract it to your “Library” folder inside your IDE software, it should be named
“IRremote”.

Once you have installed the Library, just go ahead, and restart your IDE Software.

If you look at the code below, you will see that the first lines are like this:

#include <IRremote.h>

#include <IRremoteInt.h>

If we are writing the code, we have to


import it from the libraries you have
now in the program. You may do it like
this (in the image) or through a
command, but now it is easier this way.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 2
You will get something like this: (In this case it will not work if you don’t follow the
previous steps and include the libraries, so do not just copy the sketch…)

#include <IRremote.h> //We include the library


#include <IRremoteInt.h>

int RECV_PIN = 11; // IR is connected to pin 11

IRrecv irrecv(RECV_PIN);

decode_results results; // create instance of 'decode_results'

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

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

Instead of copying or writing the code, we can learn now an interesting way to study and
make use of written code. We are going to open an example.

Go to “Archivo → Ejemplos” and look for IRremote.  You will find several examples. Open
“IRrecvDemo”.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 3
 Now open the serial monitor and see the result when you press the buttons in the
remote:

If you take a different remote (anyone will work, like TV remotes) you will get different
results but you could use them for any project.

Take note of the results you get when you press the different buttons in the remote.

Notice all the ‘FFFFFFFF’ entries, these are “repeat” commands, and if you hold a button
down, you get a constant stream of ‘FFFFFFFF’.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 4
EXERCISE 1 (IR LED)
Save it as “IR LED control 1”
Connect an LED to pin 9 and the IR sensor to pin 11.

Copy and study the following code: Change the values of the Remote according to your
results in the previous example.

#include <IRremote.h> // We include the library (IRemote.h)


int ReceptorIR = 11; //pin for the IR sensor
int led = 9; //pin for LED
int i; // one variable
IRrecv irrecv(ReceptorIR); //join pin to library
decode_results CodigoTecla;//variable to save Key code received

void setup()
{
Serial.begin(9600); // Communication starts
irrecv.enableIRIn(); //We start taking data
pinMode(led, OUTPUT);
}

void loop()
{
if (irrecv.decode(&CodigoTecla)) // if a key is pressed
{
Serial.print("0x"); // macro to see the key code on screen
Serial.println(CodigoTecla.value, HEX); //
switch (CodigoTecla.value) // “CodigoTecla” variable as switch
{
case 0xFF10EF: //when we press the first key (1):
digitalWrite(led, HIGH);
break;
case 0xFF38C7: //when we press the second key (2):
digitalWrite(led, LOW);
break;
}
delay(250); //delay to avoid pressing the key twice
irrecv.resume(); //to stop getting data
}
}

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 5
EXERCISE 2 (IR LEDs)
Connect three LEDs (pins 8,9, 10) and change the previous code to make each one turn ON
in order when you press 1, 2, and 3 and turn OFF when you press 4, 5 and 6 in the remote.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 6

You might also like