Introduction: Using an IR Receiver With Arduino

About: The RobotGeek team is a 6-man operation that wants to make it even easier to use Arduino to make electronics and robots. Check out our instructables and for all of our awesome kits.

Want a low cost solution to wirelessly control your next project? You can use an IR Receiver and an old remote you have lying around the house! In this tutorial, we will walk you through figuring out your remote's protocol, getting the hex codes for button presses, and using those button presses to control some LEDs and a servo! It looks a bit scary at first, but it's really simple!

Step 1: Project Parts List

We will be using a RobotGeek IR Receiver with a Geekduino and sensor shield. You can use any parts you like, but the diagrams will assume you have these parts. The LEDs and Servo are to show just a few ways you can use IR input. They are not fully necessary to complete this demonstration, but serve to illustrate the functionality well.

Step 2: Find Your Remote's Protocol

The first step to using any infrared controller is to find its protocol. The most common protocols are NEC and SONY. Virtually every remote made by Sony uses the same protocol, and most unbranded or generic controllers use NEC, but it's always a good idea to check the protocol. We've put everything together to make it as simple as possible to get started.

Hook up your Arduino according to the diagram

Grab the RobotGeek Libraries and Tools, and drop it in your Arduino folder.

Start up the Arduino IDE and navigate to:

File → Examples → IRLib → IRfreqScout

Load the sketch onto your Arduino.

Open the serial monitor and get your remote ready.

The Serial Monitor should say Send a signal repeatedly. We will report averages and statistics.

Press a button on the remote. You may want to press the same button a couple times. You will get a readout like the following:

Decoded Sony(2): Value:A8B92 (20 bits)
Raw samples(42): Gap:300
  Head: m2300  s650
0:m1100 s650	1:m550 s650		 2:m1100 s700	3:m500 s650		 
4:m1100 s700	5:m500 s650		 6:m600 s600	7:m600 s600		 
8:m1100 s650	9:m500 s700		 10:m1100 s650	11:m1100 s700		 
12:m1100 s650	13:m500 s700		 14:m500 s650	15:m1100 s700		 

16:m550 s600	17:m600 s600		 18:m1100 s650	19:m550
Mark  min:500	 max:1100
Space min:600	 max:700

	 #	Low	High	Avg.
Mark	20	500	1100	792
Space	19	600	700	655

Marks		 #	Low	High	Avg.
Above Avg	9	1100	1100	1100
Belows Avg.	11	500	600	540

Spaces		 #	Low	High	Avg.
Above Avg.	6	700	700	700
Below Avg.	13	600	650	634

Ratios		Below	Above
Head Mark	4.26	2.09
Head Space	1.03	0.93

Mark Above/Below=	2.04
Space Above/Below=	1.10

The main thing to pay attention to at this step is where it says: Decoded Sony(2): Value:A8B92 (20 bits). This tells you the Protocol and the Hex value of the button you pressed. Try pressing different buttons, and you'll see the Value change. If you try a remote with a different protocol, you might get a result such as Decoded NEC(1): Value:FF02FD (32 bits). This one tells you that you're using a remote that utilizes the NEC protocol. You'll need to know which protocol you're using for any project that uses Infrared, so keep track of this.

Step 3: Check the Hex Codes!

You could get all the button presses from the last sketch we loaded, but that would take a long time, as it loads much more information than we need and searches for the protocol every cycle. To check for ONLY the Hex values being transmitted to your receiver, open this sketch in the Arduino IDE:

File → Examples → IRLib → IRhexSerial

You'll need to put the protocol in for your remote in line 11:


If you're using a remote that was defined as something other than NEC, replace NEC with the matching protocol. The protocols are as follows:


Once you have your protocol set, simply load the sketch onto your board and open the Serial Monitor.

Press some buttons, and you will receive hex values! Note that if you are using a NEC remote, holding the button down will give you the value FFFFFFFF. This is normal for NEC remotes, and is handled in code. You can either systematically press the buttons on the remote, keeping track in a spreadsheet which buttons give which hex values, or you can use the wizard utility in the next step to grab the hex values for your remote.


The next sketch we'll have you load is the IR Remote Wizard. You can find it under:

File → Examples → IRLib → IRremoteWizard

Load the sketch on to your board, open the serial monitor, set it to No line ending in the new line / carriage return dropdown, and follow the prompts.

The first prompt will ask you to press buttons on your remote. This step automatically finds the protocol of your remote on receipt of a signal. Type 1 in the bar at the top and press Enter on your keyboard or click Send in the top right corner.

The next lines that will show up say:

Protocol Section Finished. Start Button reading.
Press the button for RIGHT_BUTTON

Follow the instruction and press the corresponding button on your remote. Upon receipt of the button press, the serial monitor should now read:

Button Found:3EB92 Send any character on the serial monitor to continue

Follow the instruction, repeating the process of typing a character into the top bar and sending it, followed by pressing the corresponding button on your remote. This will continue through asking you for directional buttons, a select button, numerals 0-9, and two buttons of your choosing.

Once you have completed this process, it will spit out a block of code that looks like this:

IR Code Block: 
const unsigned long MY_PROTOCOL = SONY;
const unsigned long RIGHT_BUTTON = 0x9EB92;
const unsigned long LEFT_BUTTON = 0xDEB92;
const unsigned long UP_BUTTON = 0x9EB92;
const unsigned long DOWN_BUTTON = 0x5EB92;
const unsigned long SELECT_BUTTON = 0xD0B92;
const unsigned long ONE_BUTTON = 0xB92;
const unsigned long TWO_BUTTON = 0x80B92;
const unsigned long THREE_BUTTON = 0x40B92;
const unsigned long FOUR_BUTTON = 0xC0B92;
const unsigned long FIVE_BUTTON = 0x20B92;
const unsigned long SIX_BUTTON = 0xA0B92;
const unsigned long SEVEN_BUTTON = 0x60B92;
const unsigned long EIGHT_BUTTON = 0xE0B92;
const unsigned long NINE_BUTTON = 0x10B92;
const unsigned long ZERO_BUTTON = 0x90B92;
const unsigned long SPECIAL_1_BUTTON = 0x481;
const unsigned long SPECIAL_2_BUTTON = 0xC81;

Copy this code from your serial monitor, you will need it.

Step 5: Insert Your Hex Codes in the Demo Sketch

Now we're getting to the good stuff. So you've got your code for the remotes ready to go, and you need a place to put it. Open:

File → Sketchbook → RobotGeekSketches → Demos → IR → IRcontrolBasics

You'll see at the top two tabs, one marked IRcontrolBasics, and one marked remotes.h. Click on remotes.h and you should see a list of remote definitions, followed by code that looks very similar to the code you just made. You can replace the similar code of one of the remotes or add a definition and #elif statement. The main things to consider are:

1.) The definition of the remote you're using. On line 1, you will see:


Change MINI_REMOTE_1 to the name of the remote you would like to use.

2.) The #elif statement. Before the #endif statement, you can add your remote with a line like the following:


followed by the list of const unsigned longs you got from the wizard.

3.) The const unsigned long integers. You can add any number of buttons that exist on your remote by adding definitions to this list with names corresponding to the hex values you received in the earlier steps preceded by 0x, to indicate that it is a hex value.

Step 6: Hook It Up!

Hook up your parts according to the diagram.

Device Sensor Shield Port
Trigger Servo 9G Servo Digital 6
IR Receiver RobotGeek Infrared Receiver Digital 11
LED 1 RobotGeek LED Driver Digital 2
LED 2 RobotGeek LED Driver Digital 5

Now upload the sketch! You can open the Serial Monitor if you'd like to take a look at the input from the remote. Controlling the sketch works as follows:

  • Pressing Left or Right turns the servo
  • Pressing Up or Down increases or decreases the brightness of LED 2
  • Pressing 1 or 2 turns on or off LED 1
  • Pressing Special Button 1 or Special Button 2 decreases or increases the speed of the servo

This should give you a pretty good foundation to work off of for a wide variety of projects!

Step 7: You're Done!

Now you have a jumping off point for integrating Infrared Control into your next project! What are you going to make? We'd love to see what you come up with!