Skip to content

Receiving with the IRremote library

shirriff edited this page May 6, 2011 · 2 revisions

This page gives some simple examples of how to receive IR codes with the IRremote Arduino library.

Receiving and printing a code

The following sketch will receive codes and print them to the serial port. This sketch is very useful for testing IR receiving, and for determining what code values to use in your code. A slightly more complex version is in the examples directory as IRrecvDump.

This sketch also illustrates how to perform an action while a button is pressed. In this example, the action is writing to the serial port.

#include <IRremote.h>
IRrecv irrecv(11); // Receive on pin 11
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Continue receiving
  }
}

Performing one action when a button is pressed an another when the button is released.

This sketch illustrates how to perform an action when a button is pressed, and another action when a button is released. Note that an IR remote repeatedly sends a code while a button is pressed, so the code uses a timer to determine if the button has been released (i.e. no code has been received for a little while).

In this example, the LED is turned on when the button is pressed, and off when the button is released.

Toggling a state when a button is pressed.

This sketch illustrates how to use a button on the remote as a toggle. Press a button once to turn the LED on, and press again to turn the LED off. As in the previous sketch, a timer is used to determine if the button is held down, or has been released.

Clone this wiki locally