Pages

Tuesday, November 19, 2013

Using an IR remote to Control an Arduino Project

For some projects it is desirable to be able to control the system remotely. One easy way to do this is via an IR remote control.

In order to accomplish this you need to obtain an IR receiver module. These are rather inexpensive components and can be purchased or scavenged from old VCRs. DVD's and other remote control devices. If you wish to purchase you can do so from Sparkfun or Adafruit.

The Adafruit site has a nice tutorial on setting up and using the receiver.

The IR receiver module that I am using is shown below. The diagram notes both the model number and the pin connections. Note that all IR receiver modules will have the same three connections, but the order of the connections may be slightly different. You can see this if you compare the one below with the similar diagram on the Adafruit site.

For the connections to the Arduino: Vcc goes to +5 V, Vout goes to a digital input Pin.



For the examples that I will be using, the IR Receiver is connected to P11 and three LED's are connected to digital pins P2, P3, and P4. The final configuration is shown here:


Download and Install IR Remote Library

The library that I recommend was written by Ken Shirriff. He has a full description of the project on his blog under the post A Multi-Protocol Infrared Remote Library for the Arduino.

Step 1: Download the library from GitHub and install the library using the following directions:


Step 2: Test system

As an initial test of your system your should upload and run one of the example sketches that comes with the IR Remote Library. File -> Examples ->  IRremote -> IRrecvDemo. Running this sketch will allow you to both verify that you have correctly configured your IR Receiver module and obtain the IR codes from whatever IR Remote control that you wish to use.

After loading and uploading the sketch, open the Serial Monitor and then push a button on your remote control. If the system is working, you should see something similar to this...



You should notice that every time you push a button, you will see a different sequence of letters and numbers. If you hold down the key you will see a repeat code of FFFFFFFF. The library is decoding the IR signal and outputting a value in Hexadecimal format. If you want to check out how Hexadecimal (base 16) compares to both decimal (base 10) and binary (base 2) you can look at this online converter.

The Arduino code will recognize a hexadecimal value as a regular integer, however for these values you need to use the variable identifier long instead of int because these 8 digit hexadecimal numbers are larger than the upper limit of the int range. In decimal format FFFFFFFF is 4,294,967,295.

If all is working correctly, you can go ahead and note down the codes for the remote control keys that you want to use. For the case of my remote control, I wanted to use the first four digits on the number pad and the codes for these for keys are as follows:

button 1 - FF728D
button 2 - FF52AD
button3 - FF929D
button4 - FFB24D

In order to use these as hexadecimal numbers in the Arduino code, it is necessary to prepend an identifier so that the code does not mistake these values as a string or some other variable name.  The identifier is 0x so the variables that we will use in the sketch become:

button 1 - 0xFF728D
button 2 - 0xFF52AD
button3 - 0xFF929D
button4 - 0xFFB24D

Step 3: Understand the particular lines that are required to make IRremote function.

Before presenting the entire sketch it may be helpful to look at the specific lines of code that relate to the IRremote library (some of my comments have been take from this page)

#include <IRremote.h>    This line is place at the beginning of the sketch and alerts the Arduino IDE that it should include the IRremote library when the sketch is compiled.

int RECV_PIN = 11;    This sets a variable name for the digital pin that is connected to the Vout pin of the IR receiver module. The example code uses P11, but any digital I/O pin can be used.

long button1 = 0xFF728D;    This is one of the variable declarations that provides a variable name to the hexadecimal number that corresponds to the buttons on the remote. Note that the variable identifier long has been used instead of int.

IRrecv irrecv(RECV_PIN);     IRrecv is a function that is defined by the IRremote library. This creates a IRrecv object that is called irrecv and this object is connected to RECV_PIN which has been defined as P11. Note that irrecv is a user defined name and you could use "bob" or another descriptive name. The use here is analogous to creating a Servo object when using the Servo library.

decode_results results;   This line creates a memory location that is used to store the information
received from the decode function of IRremote

irrecv.enableIRIn()
Begin the receiving process. This will enable the timer interrupt which consumes a small amount of CPU every 50 µs.
irrecv.blink13(true)
Enable blinking the LED when during reception. Because you can't see infrared light, blinking the LED can be useful while troubleshooting, or just to give visual feedback.
irrecv.decode(&results)
Attempt to receive a IR code. Returns true if a code was received, or false if nothing received yet. When a code is received, information is stored into "results".
results.decode_type: Will be one of the following: NECSONYRC5RC6, or UNKNOWN.
results.value: The actual IR code (0 if type is UNKNOWN)
results.bits: The number of bits used by this code
results.rawbuf: An array of IR pulse times
results.rawlen: The number of items stored in the array

if (results.value == button1){digitalWrite(ledBlue, HIGH);}    This if statement compares the value that has been stored in results to the first of the four button codes

irrecv.resume()
After receiving, this must be called to reset the receiver and prepare it to receive another code.

Step 4: Create sketch to respond to specific IR signals from remote

The video and code show a simple application of the IRemote library. Four buttons are defined and buttons 1, 2, and 3 each turn on one LED. button 4 then turns all the LED's off.

(Video)


(sketch)