Reading an infrared remote control with Arduino
FunduinoPublished on
Tastencodes einer Fernbedienung einlesen und damit etwas schalten
How an infrared remote control works
Connect the receiver
1 / 2
Read key codes
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
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