Reading an infrared remote control with Arduino

FunduinoPublished on

Tastencodes einer Fernbedienung einlesen und damit etwas schalten

Eine Fernbedienung sendet ihre Befehle als unsichtbares Infrarotlicht. Mit einem kleinen Empfängerbaustein kann der Arduino diese Befehle mitlesen – und du kannst damit alles steuern, was an deinem Board hängt.

In dieser Anleitung findest du zuerst heraus, welchen Zahlencode jede Taste deiner Fernbedienung sendet. Danach schaltest du damit eine LED ein und aus.

Small infrared remote control with number buttons and arrow buttons
The infrared remote control from the learning kit

How an infrared remote control works

An infrared receiver allows the Arduino to receive and process commands from an infrared remote control. The data travels as infrared light from the remote control to the receiver. Our eyes cannot see this light.

You can still check whether a remote control is transmitting with a simple trick: Point a digital camera at it - a smartphone camera will do - and look at the infrared LED on the screen. When you press a button, you’ll see the LED light up. Camera sensors can detect infrared light. The light flickers slightly because the LED switches on and off very quickly. It follows a specific pattern that allows the receiver to recognize which button was pressed.

You encounter infrared remote controls all the time:

  • Adjusting the volume and changing channels on the TV
  • Setting the temperature on radiator thermostats
  • Opening and closing garage doors
  • Turning power outlets on and off
Small infrared remote control with number buttons and arrow buttons
The infrared remote control from the learning kit

Connect the receiver

The VS1838B receiver contains a photodiode. It detects the infrared light, and the module’s electronics convert it into a numeric code. You read this code in the sketch and use it to trigger an action.

Build the circuit according to the schematic. In this example, the receiver’s signal pin is connected to pin 11. Check the pinout - it isn’t the same for all infrared receivers.

Circuit diagram with VS1838B infrared receiver on the Arduino UNO
Schematic: the infrared receiver on the Arduino
Pinout of the VS1838B infrared receiver
Pinout of the VS1838B receiver
1 / 2

Read key codes

Each button sends its own numeric code. The sketch reads this code and displays it in the Serial Monitor, along with the corresponding button on our remote control.

When you press the “A” button, the Serial Monitor displays: Decimal value: 69 -> A

If your remote control returns different values, that’s normal: The mapping applies to the model in our learning set. Simply enter the decimal values from the Serial Monitor into the sketch.

The sketches use the IRremote library syntax from version 3 onward (IrReceiver.begin, IrReceiver.decodedIRData). Older guides online often still use the version 2 syntax with irrecv and results.value - the two cannot be mixed. Install IRremote through the Library Manager to get the latest version.

Read the remote control key codes
// The program uses a library here. This saves a lot of work: The
// infrared light is sent encoded, and reading this code yourself and
// converting it into suitable values would require many lines of code.
#include <IRremote.h>

int EmpfaengerPin = 11;  // the pin to which the infrared receiver is connected

void setup()
{
  Serial.begin(9600);               // Start the Serial Monitor
  IrReceiver.begin(EmpfaengerPin);  // Start the infrared receiver
}

void loop()
{
  if (IrReceiver.decode())  // decode() checks whether data is available
  {
    delay(200);           // brief pause so that only one button press is read
    IrReceiver.resume();  // resume() reads the next value

    /*
      The receiver occasionally receives signals that cannot be evaluated.
      Therefore, only correctly recognized buttons should be evaluated. Their
      decimal values range from 0 to 95. The following line checks whether the
      received command IrReceiver.decodedIRData.command is greater than 0 AND (&&) less than 95.
    */
    if (IrReceiver.decodedIRData.command > 0 && IrReceiver.decodedIRData.command < 95)
    {
      // The text "Decimal value: " appears in the Serial Monitor, followed by the value itself.
      Serial.print("Decimal value: ");
      // decodedIRData.command is the value of the pressed button.
      Serial.print(IrReceiver.decodedIRData.command);
      // The arrow separates the value from the corresponding button on the remote control.
      Serial.print(" -> ");

      // On our remote control, the decimal number 69 corresponds to the "A" button, and so on.
      if (IrReceiver.decodedIRData.command == 69) Serial.println("A");
      if (IrReceiver.decodedIRData.command == 70) Serial.println("Up arrow");
      if (IrReceiver.decodedIRData.command == 71) Serial.println("B");
      if (IrReceiver.decodedIRData.command == 68) Serial.println("Left arrow");
      if (IrReceiver.decodedIRData.command == 64) Serial.println("X");
      if (IrReceiver.decodedIRData.command == 67) Serial.println("Right arrow");
      if (IrReceiver.decodedIRData.command ==  7) Serial.println("0");
      if (IrReceiver.decodedIRData.command == 21) Serial.println("Down arrow");
      if (IrReceiver.decodedIRData.command ==  9) Serial.println("C");
      if (IrReceiver.decodedIRData.command == 22) Serial.println("1");
      if (IrReceiver.decodedIRData.command == 25) Serial.println("2");
      if (IrReceiver.decodedIRData.command == 13) Serial.println("3");
      if (IrReceiver.decodedIRData.command == 12) Serial.println("4");
      if (IrReceiver.decodedIRData.command == 24) Serial.println("5");
      if (IrReceiver.decodedIRData.command == 94) Serial.println("6");
      if (IrReceiver.decodedIRData.command ==  8) Serial.println("7");
      if (IrReceiver.decodedIRData.command == 28) Serial.println("8");
      if (IrReceiver.decodedIRData.command == 90) Serial.println("9");
    }
  }
}

Extension: Control an LED with the remote control

Now, pressing the "1" key should turn on an LED, and pressing the "2" key should turn it off again. To avoid having to rewire anything, use the onboard LED on pin 13.

Expansion: Switch the LED with the remote control
#include <IRremote.h>
int EmpfaengerPin = 11;
int LED = 13;  // LED pin

void setup()
{
  IrReceiver.begin(EmpfaengerPin);
  pinMode(LED, OUTPUT);  // Pin 13 is defined as an output.
}

void loop()
{
  if (IrReceiver.decode())  // Check whether data is available
  {
    IrReceiver.resume();  // read the next value

    if (IrReceiver.decodedIRData.address == 0)
    {
      // Button 1 -> read decimal value 22 -> turn on the LED
      if (IrReceiver.decodedIRData.command == 22) digitalWrite(LED, HIGH);
      // Button 2 -> read decimal value 25 -> turn off the LED
      if (IrReceiver.decodedIRData.command == 25) digitalWrite(LED, LOW);
    }
  }
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.