Skip to content
Arduino contents

Lesson 1.7: Infrared receiver

A remote control is a cheap wireless controller. With an infrared receiver, any project can take orders from it.

Parts

How it works

Infrared light is invisible to us, but there's plenty of it around: sunlight, light bulbs. To stand out, the remote switches it on and off 38,000 times a second, and the receiver only responds to that frequency.

Each key sends a sequence of pulses following a protocol; the most common in simple remotes is NEC. The IRremote library measures those pulses and turns them into an address and a command: the command is what tells one key from another.

When you hold a key down, many remotes don't resend it: they send a special repeat code. The library marks it with a flag so you can ignore it or use it.

Wiring

ComponentConnects toNote
IR receiver, SPin 11
IR receiver, +5V
IR receiver, −GND

Code

// Prints the code of each key on the remote.
#include <IRremote.hpp>

const int IR_PIN = 11;

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK);
  Serial.println("Press a key on the remote");
}

void loop() {
  if (!IrReceiver.decode()) return; // nothing received

  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;

  if (!repeat) {
    Serial.print("Command: 0x");
    Serial.println(IrReceiver.decodedIRData.command, HEX);
  }

  IrReceiver.resume(); // ready for the next signal
}

What the code does

  • #include <IRremote.hpp> loads version 4 of the library. Older tutorials use IRrecv and decode(&results): that style no longer compiles with current versions.
  • IrReceiver.begin() starts the receiver; ENABLE_LED_FEEDBACK blinks the built-in LED on every signal.
  • decodedIRData.command is the key's code. Write them down: the stepper motor lesson needs them.
  • resume() gets the receiver ready for the next signal. Without that line, only the first one is received.

Step by step

  1. Install the «IRremote» library from the library manager.
  2. Wire the module following the table.
  3. Upload the code and open the serial monitor at 9600 baud.
  4. Point the remote at the receiver and press a few keys.

Result

Each key prints its code in hexadecimal. The same key always gives the same code, and different keys give different codes.

If it doesn't work

  • If nothing appears, look at the remote's LED through your phone camera: on many of them it shows up purple when you press a key. If it doesn't light, replace the battery.
  • Sunlight or some fluorescent lights can interfere: try somewhere else.
  • If the compiler can't find IrReceiver, you have an old version of IRremote installed: update it.