Arduino IR Remote: A beginners tutorial on Receiving and transmitting IR Remote Control Signals using an IR LED, and IR receiver connected to an Arduino Uno.


Introduction

How to create an Arduino IR remote control using an IR LED and IR recevier. You have many IR (Infra Red) Remotes in your home which allow you to control the TV, air conditioners, garage door openers, etc. This tutorial will show you how to read the code that your IR remote makes, and replicate it using an Arduino.

How do IR remotes work?

IR remote controls work by sending out unique bursts of infrared light for each button pressed on the remote (using an Infrared light emitting diode). The receiving device, whether a TV, AC unit, or other appliance, contains an IR sensor that can recognize these coded signals. It then carries out the associated function like changing channels or adjusting temperature settings.

How to detect IR remote light

Arduino IR remote keys-22 The tutorial uses an IR sensor module which contains an infrared photodiode and internal conditioning circuit. You get the decoded digital output from one pin - all very convenient - and all you need to do is get the Arduino to interpret the protocol (the sequence of formatted digital data) from the IR sensor - just use the suggested library.

There are many different IR receiver modules, but most will work just fine with this tutorial. However make sure they are tuned to operate at 38~39kHz  - this is the diode modulation frequency. To transmit a digital "1", the diode is not just turned on for a while, it is turned on and off at 38kHz for a set period of time (this is part of sunlight rejection - the receiver can be tuned to reject anything but 38kHz oscillation).

The one used in this tutorial is on the Keys-22 breakout board and is the TL1838

Why not use 'just an IR photodiode' to detect IR?

These modules have been developed over years and include frequency sensitive amplifiers and ambient sunlight rejection circuits - it means they can work up to 18m. In short don't try to make one of these using an IR receiver diode - all the problems in creating a reliable detector are solved in the IR Sensor module.

Note: There is one good use of an IR LED and IR receiver diode - and that is detecting blood flow i.e. creating a heart rate plot.

What this tutorial does

Arduino IR LEDWith an Arduino controlling such an IR sensor module and also controlling an IR LED, you have the ability to both receive from and transmit to other IR devices. We can input codes detected from a physical remote. Or our code can output codes to control appliances without a physical remote of our own.

In this guide, we'll show you how to wire an IR sensor module to an Arduino and code it for bidirectional IR communication. You'll learn to receive signals and also emit your own codes to remotely trigger devices. This opens up plenty of possibilities to control additional systems through the infrared protocols that are included within Arduino libraries.

Getting Started with an Arduino IR Remote

In this beginner-friendly tutorial, we'll show you how to use an Arduino IR remote sensor with your Arduino Uno board to receive signals from a standard TV or air conditioner remote control. By decoding the IR pulses, you'll be able to control Arduino projects and more with a simple remote.

Required Components

  • Arduino Uno or Nano board
  • IR Sensor Module
  • Breadboard
  • Jumper wires
  • Example remote control (from TV, AC, etc)
  • An IR LED
  • A push button
  • A 100 Ohm resistor.

Arduino IR remote: Circuit Diagram

Ground (0V) and supply voltage (5V) are connected from pins in the lower pin header of the Arduino Uno, while the IR sensor is connected to pin 10 of the upper rail and the IR diode is connected to pin 9 of the upper rail via the 100 Ohm resistor - make sure the flat side of the IR diode (denoting the cathode) goes to ground. The push button is connected to pin 2 on one side, and ground on the other.

arduino ir sensor circuit layout
Diagram using fritzing
arduino ir sensor schematic

Diagram using fritzing

Libraries needed

The library you need to install is "IRremote".

Installing an Arduino Library with IDE

Install the library as follows:

1st method:

    Click the menu bar and follow the menu items:

Sketch-->Include Library-->Manage Libraries...

2nd method:

    Click the ICON on the left that looks like a set of books.
The library manager appears on the left of the screen.

In the search box type "IRremote". Now click the install button.

Arduino IR remote: Example Sketch

What it does

The following Arduino IR remote sensor example code will receive a code from your remote - press any button and the serial monitor will display the data for that button.

Warning: Not all protocols are supported - if it fails try a different remote control unit (or look at the github source for IRremote - it has some ideas for other protocol solutions).

When you press your own button on the breadboard, it will spit out the same code with the same protocol, address and code as the remote (these values were are stored in variables: irprotocol, iraddress, ircode on IR recevied action).

It is a simple repeater, but demonstrates the fundamental use of the library so you can use it for more complex projects. For example:

  • IR Remote Controlled LEDs/Motors
  • IR Remote Home Automation
  • IR Repeater
  • Universal Remote
  • IR Message Decoding
  • TV Remote Controlled Car
  • IR Remote Power Relay

You can copy and paste the code below into the Arduino IDE (in a new sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).

#include <IRremote.h>

const int irPin = 9; // IR sensor pin
const int ledPin = 13; // Built-in LED pin
const int irLedPin = 10; // IR LED pin
const int buttonPin = 2; // Button pin

int buttonState = 0;
int previousButtonState = 0;
int irprotocol; // The received protocol.
int iraddress;  // The received address.
int ircommand;  // The received command.

void sendIRCode(int code) {

  digitalWrite(ledPin, HIGH); // Flash built-in LED

  // Send IR code  
  IrSender.write(irprotocol,iraddress,ircommand,0); // 0 == repeat  

  digitalWrite(ledPin, LOW);
}

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(irLedPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);

  Serial.begin(9600);
  Serial.println("Arduino IR remote beginners tutorial - TroncisBench.com");
  IrReceiver.begin(irPin);
  IrSender.begin(irLedPin);

  Serial.print(F("Protocols Allowed: "));
  printActiveIRProtocols(&Serial);
}

void loop() {
  int code;

  if (IrReceiver.decode()) {
    // Flash built-in LED
    digitalWrite(ledPin, HIGH);  
    delay(100);
    digitalWrite(ledPin, LOW);


        if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
            Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
            // We have an unknown protocol here, print extended info
            IrReceiver.printIRResultRawFormatted(&Serial, true);
            IrReceiver.resume(); // Do it here, to preserve raw data for printing with printIRResultRawFormatted()
        } else {
            IrReceiver.resume(); // Early enable receiving of the next IR frame
            IrReceiver.printIRResultShort(&Serial);
            IrReceiver.printIRSendUsage(&Serial);
            Serial.print("Command ");
            Serial.println(IrReceiver.decodedIRData.command);
          irprotocol = IrReceiver.decodedIRData.protocol;
          iraddress  = IrReceiver.decodedIRData.address;
          ircommand  = IrReceiver.decodedIRData.command;
        }
        Serial.println();    
   
    IrReceiver.resume(); // Receive the next value
  }

  // You could use millis delay code but this works as
  // IRremote library is interrupt driven.
  // In general don't use delay() this is beginners code.
  int reading1 = digitalRead(buttonPin);
  delay(50);
  int reading2 = digitalRead(buttonPin);
 
  if(reading1==reading2) { // Press or release is same as 50ms ago.
    buttonState = reading1;

    if(buttonState == HIGH && // Update on correct transition - button release.
    previousButtonState == LOW) {  

      // Flash built-in LED
      digitalWrite(ledPin, HIGH);  
      delay(100);
      digitalWrite(ledPin, LOW);

      // Send saved IR code
      sendIRCode(code);
       
    } // went high
    previousButtonState = buttonState;

  } // r1 == r2
} // loop

Short Code Explanation

  • The code receives IR codes from a receiver module using the IRRemote library to create an Arduino IR remote project.
  • It defines pins for the receiver, LED, IR LED and button.
  • Global variables store the received protocol, address and command.
  • Setup initializes the pins and starts code receiving/sending.
  • Loop checks for received codes and prints values to serial monitor.
  • It stores the protocol, address and command in global variables.
  • Button press is checked and LED flashes on press.
  • On button press, the stored command value is sent via IR LED.

Arduino IR remote: Detailed explanation

The code first includes the IRremote library. This library provides functions and classes to encode and decode IR signals.

It then declares some global variables that will be used throughout the code:

  • irPin: The pin number connected to the IR receiver module
  • ledPin: The pin for the built-in LED
  • irLedPin: The pin connected to the IR LED
  • buttonPin: The pin for the button

It also declares variables to store the received protocol, address and command:

  • irprotocol
  • iraddress
  • ircommand

The setup() function runs once on startup. It initializes the LED, IR LED and button pins as outputs or inputs. It then begins initialization of the IRreceiver and IRsender objects from the IRremote library. This prepares them to receive and transmit IR signals.

The main loop() is where the logic happens. It first calls IrReceiver.decode() to check if a new code was received. If a code was received, it prints out details like the protocol and address. Importantly, it stores the received values in the global variables.

Next, the button state is checked. If pressed, the stored command value is passed to sendIRCode(). sendIRCode() first flashes the LED for feedback. It then uses the IRsender object to transmit the code, using the stored protocol, address and command.

By receiving codes, storing the important values, and retransmitting on demand, this demonstrates the basic process of IR reception and retransmission with the IRremote library.

Arduino IR remote: Uploading the Code

There are a few steps to uploading the code using the Arduino IDE:

  • Connect the Arduino Uno to the PC with a USB cable.
  • Select the Arduino Uno hardware.
  • Open a new sketch.
  • Paste the code above into the new page (overwrite everything).
  • Press the upload button (right arrow at top).

You can find a more detailed tutorial on the Arduino IDE page.

Testing the Circuit

Connect up as shown, and point at IR receiver on the breadboard e.g. use your TV remote - and point it at the sensor. After that, Press the button on the solderless breadboard to re-transmit that code. If it does not work your remote may have an unsupported protocol. The solution is to use another remote, or use a different IR library.

Look at the Serial monitor output to see what was received to see which protocol your remote is using, and other received data e.g. address and command.

Making it reliable

If the device you are controlling does not react very well, then change the code to increase the repetitions of the output code. So for instance, changing the '0' to a '3' in the statement below, will cause 3 repetitions of the output code. This line is within the function sendIRCode().

    IrSender.write(irprotocol,iraddress,ircommand,0); // 0 == repeat  


The reason this works is that in all IR remote codes there is a bit within the sent data that indicates if a key-press-has-changed. The Receiving device can understand, that if it receives multiple IR codes, that you are holding the button down - not that you want to turn the television on and off twenty times a second!

Arduino IR remote: Conclusion

This concludes the basic tutorial on using an Arduino IR remote sensor to receive and transmit infrared signals. With just a few inexpensive components, you've built a system that can detect remote control codes and retransmit them to trigger associated actions.

The skills learned here provide a foundation for many possible extension projects. Some ideas include building a universal remote to control multiple devices, creating an IR-based home automation system, or adding IR controls to robots or other Arduino projects.

The IRremote library also supports decoding protocols for many common remotes, so with further exploration you can receive codes from a wide range of sources. Combining IR reception with other sensors and actuators opens up creative possibilities.

I hope this tutorial helped demonstrate the basic functionality of an Arduino IR remote sensor and IR LED. Please feel free to experiment further and let me know if you have any other questions.


Written by John Main who has a degree in Electronic Engineering.

Note: Parts of this page were written using claude-instant as a research assistant.



Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use