Infrared receiver modules

Submitted by james on
Infrared receiver module

Infrared receiver modules are commonly used in remote control systems, and sometimes for line-of-sight wireless data transmission.

For the IR receiver module pictured here, which is based on a VS1838B sensor receiving a 38 kHz carrier, we will use a library called IRremote, which includes support for several different modules and protocols.

The module has three pins, labeled `G`, `R`, and `Y`. These letters may (or may not) stand for green, red, and yellow, which happen to be sensible colors to use for the wires connecting ground, voltage, and signal pins, respectively.

When the IR receiver detects a signal, it emits a number that can be used in the Arduino sketch. Pressing different buttons on the remote controller causes the IR receiver to emit different numbers. If you don't have a reference or data sheet, these numbers must be found by trial and error for your particular device.

#include <IRremote.h>

// Arduino pin 5 connected to the module's signal pin
const byte IR_PIN = 5;

// This object talks to the module through pin IR_PIN
IRrecv irrecv(IR_PIN);

// This structure store results from reading the module
decode_results results;

void setup()
{
	Serial.begin(9600);

	// Enable the receiver
	irrecv.enableIRIn();
}

void loop()
{
	// Check for a signal and store in the results variable
	if (irrecv.decode(&results)) {
		handleResults(results);

		// receive the next value
		irrecv.resume();
	}
}

// Look at what's in the results and act upon them
void handleResults(decode_results& results)
{
	Serial.print("Decode type: ");
	Serial.println(results.decode_type, HEX);

	Serial.print("Address: ");
	Serial.println(results.address, HEX);

	Serial.print("Bit count: ");
	Serial.println(results.bits);

	Serial.print("Magnitude: ");
	Serial.println(results.magnitude, HEX);

	Serial.print("Is a repeat: ");
	Serial.println(results.isRepeat);

	Serial.print("Decode type: ");
	Serial.println(results.decode_type, HEX);

	Serial.print("Raw value: ");
	Serial.println(results.value, HEX);

	Serial.print("Overflowed: ");
	Serial.println(results.overflow);

	Serial.println("--------------------");

	Serial.println("Interpreted value:");
	Serial.print("    "); // indent

	// Here we interpret the result "value" code and do something (in this case, just print to Serial).
	// These values were found by experiment and will probably not match your device.
	switch (results.value) {
		case 0xE318261B:
			Serial.println("CH-");
			break;
		case 0x511DBB:
			Serial.println("CH");
			break;
		case 0xEE886D7F:
			Serial.println("CH+");
			break;
		case 0x52A3D41F:
			Serial.println("PREV");
			break;
		case 0xD7E84B1B:
			Serial.println("NEXT");
			break;
		case 0x20FE4DBB:
			Serial.println("PLAY/PAUSE");
			break;
		case 0xF076C13B:
			Serial.println("VOL-");
			break;
		case 0xA3C8EDDB:
			Serial.println("VOL+");
			break;
		case 0xE5CFBD7F:
			Serial.println("EQ");
			break;
		case 0x97483BFB:
			Serial.println("100+");
			break;
		case 0xF0C41643:
			Serial.println("200+");
			break;
		case 0xC101E57B:
			Serial.println("0");
			break;
		case 0x9716BE3F:
			Serial.println("1");
			break;
		case 0x3D9AE3F7:
			Serial.println("2");
			break;
		case 0x6182021B:
			Serial.println("3");
			break;
		case 0x8C22657B:
			Serial.println("4");
			break;
		case 0x488F3CBB:
			Serial.println("5");
			break;
		case 0x449E79F:
			Serial.println("6");
			break;
		case 0x32C6FDF7:
			Serial.println("7");
			break;
		case 0x1BC0157B:
			Serial.println("8");
			break;
		case 0x3EC3FC1B:
			Serial.println("9");
			break;

		default:
			Serial.println("[UNKNOWN]");
	}

	Serial.println("--------------------");

	delay(500);
}