Tutorial: Arduino IR sender and receiver

On this tutorial, we will show how to implement an IR sender and receiver with Arduino. To do this, you will need 2x Arduinos, 1x IR emitter, 1x IR receiver, 1x 200 ohm resistor, 1x push button, and 1x LED. You can use your TV remote as an IR transmitter.

First, you have to check the receiver pinout. Look at the datasheet, you should find something like that:

Connect your transmitter like the following picture:

Then, connect your receiver like that:

Before we get to the programming, download and install the IR library.

The sample program was extracted from the Arduino Cookbook book. Open your Arduino IDE and upload the following code:

irSend sketch
this code needs an IR LED connected to pin 3
and 5 switches connected to pins 4 - 8
#include <IRremote.h>
// IR remote control library
const int numberOfKeys = 1;
const int firstKey = 4;
// the first pin of the 5 sequential pins connected to buttons
boolean buttonState[numberOfKeys];
boolean lastButtonState[numberOfKeys];
long irKeyCodes[numberOfKeys] = {
    0x18E758A7, //0 key

IRsend irsend;
void setup()
    for (int i = 0; i < numberOfKeys; i++){
        int physicalPin=i + firstKey;
        pinMode(physicalPin, INPUT);
        digitalWrite(physicalPin, HIGH); // turn on pull-ups
void loop() {
    for (int keyNumber=0; keyNumber<numberOfKeys; keyNumber++)
        int physicalPinToRead=keyNumber+4;
        buttonState[keyNumber] = digitalRead(physicalPinToRead);
        if (buttonState[keyNumber] != lastButtonState[keyNumber])
            if (buttonState[keyNumber] == LOW)
                irsend.sendSony(irKeyCodes[keyNumber], 32);
            lastButtonState[keyNumber] = buttonState[keyNumber];

Do the same with the receiver code:


IR_remote_detector sketch
An IR remote receiver is connected to pin 2.
The LED on pin 13 toggles each time a button on the remote is pressed.
#include <IRremote.h> //adds the library code to the sketch
const int irReceiverPin = 2; //pin the receiver is connected to
const int ledPin = 13; 
IRrecv irrecv(irReceiverPin); //create an IRrecv object
decode_results decodedSignal; //stores results from IR detector

void setup()
    pinMode(ledPin, OUTPUT);
boolean lightState = false;
unsigned long last = millis();

// Start the receiver object
//keep track of whether the LED is on
//remember when we last received an IR
void loop()
    if (irrecv.decode(&decodedSignal) == true) //this is true if a message has been received
        if (millis() - last > 250) {
            //has it been 1/4 sec since last message
            lightState = !lightState;
            //toggle the LED
            digitalWrite(ledPin, lightState);
        last = millis();
        // watch out for another message

It's time to test! When you press the button, the LED on the other Arduino should switch on. When you press it again, the LED switch off.





Views: 82750

Tags: arduino, infrared, ir

Comment by Mitchell S. Lowther, Jr. on May 6, 2012 at 11:26pm

This is neat. I'm going to do this, but I need to purchase another 'duino first. 

Thanks for posting this!

Optional add-on:  if a speaker was added to the receiving unit as well, then you could have it emit a small tone in addition to lighting up the LED. Then a lower note when shutting off the led.  Just a thought...



Comment by lovakiranvarma on August 3, 2012 at 7:24pm

I think, this circuit range is less to detect the IR signal. To improve the range of the IR circuit, one should generate 38KHz signal as a carrier, I found in 


off course you should need to increase the voltage required to drive the IR circuit

Comment by lunia on March 8, 2013 at 1:24am

hey..how about i just want to transmit data like 123 or abc..and i dont want to use irsend.sendSony because my project do not use remote


Comment by kkukkuk on March 13, 2015 at 9:21am

hi..there are some errors in both irsend and irreceive coding..

ir sender:

sketch_mar14a.ino:17:1: error: 'IRsend' does not name a type
sketch_mar14a.ino: In function 'void loop()':
sketch_mar14a.ino:38:17: error: 'irsend' was not declared in this scope

whereas in ir receiver coding:

'TKD2' was not declared in this scope

int RECV_PIN = TKD2; // the pin the IR receiver is connected to
help me please :)

Comment by AnalysIR on March 13, 2015 at 10:56am


this problem has been discussed receltly on the Arduino forum - why not search there.

You should be able to find the solutiion there.


Visit the AnalysIR Blog


You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

GarageLab posted blog posts
5 hours ago
Profile IconChristy and Shane Watson joined GarageLab (arduino, electronics, robotics, hacking)
7 hours ago
Cris replied to Kringen's discussion MSP430 Launch Pad
"I have and I like to use TI's CCS an eclipse base IDE.. there is a problem, though. long story…"
9 hours ago
Jacqulyn Person commented on GarageLab's blog post Motorcycle Helmet Has Heads-Up Display, Rearview Camera and GPS
"The Skully AR-1 helmet is definitely a revolution is motorcycle safety. It will help in preventing…"
10 hours ago
Shane Watson commented on GarageLab's blog post New Software to Mind Reading
"It could be an improved version for lie detector test..."
11 hours ago
Shane Watson replied to Kringen's discussion MSP430 Launch Pad
"it has great potential for cheap projects."
11 hours ago
GarageLab posted a blog post

Cooling things with an Infrared Laser

Lasers (Light Amplification by Stimulated Emission of Radiation) have a large industrial and…See More
Profile IconDavid Cofer, RITIU TRAIAN and MUNIR SADEQ OMAR OTHMAN joined GarageLab (arduino, electronics, robotics, hacking)
GarageLab posted a blog post

Nanowire Production New Method could Revolutionize Photonics Applications

University Wollongong, Australia, researchers just published, in Nanoscale Journal, a new method…See More
michae coley posted photos
Nov 24
GarageLab posted blog posts
Nov 24
Profile IconRaphael Enciso, Daniel Cerean, Naveed ur Rehman and 2 more joined GarageLab (arduino, electronics, robotics, hacking)
Nov 23
GarageLab posted blog posts
Nov 19
Profile IconTsan-Ming Chou, Jacqulyn Person and Paul joined GarageLab (arduino, electronics, robotics, hacking)
Nov 19
GarageLab posted blog posts
Nov 17
EEeng is now a member of GarageLab (arduino, electronics, robotics, hacking)
Nov 16
GarageLab posted a blog post

Android Robot

Android RobotEach year more and more investments is done to develop android robots that can make…See More
Nov 13
Profile IconNatalia and Andrew joined GarageLab (arduino, electronics, robotics, hacking)
Nov 13
GarageLab posted blog posts
Nov 12
Profile Iconcheck-iss and Christie joined GarageLab (arduino, electronics, robotics, hacking)
Nov 11

© 2015   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service