IRRemote transmit and receiver.

    This page is to Setup and Config IRRemote transmit and receiver.
 

Hardware and Software

Hardware 
    
IRRemote transmit and receiver.

Software 
    Arduino IDE


Sanki Notes

  • IR Remote transmit and receiver.
  • Receiver PIN 11
  • Transmit : PIN 3 with 100 Om Transiter
  • Switch : PIN 12 with 1K Transiter
  • And PIN 13 for Signal LEN

IRremote Library

IRremote, by Ken Shirriff, allows you to receive or transmit Infrared Remote Control codes. You can make your projects controlled by a remote, or make them control other devices like televisions and stereo components.

DownloadIRremote.zip (modified to work on Teensy and other boards)

Hardware Requirements

For transmitting, a single Infrared LED and resistor are needed. For receiving, an IR receiver module with internal bandpass filter is needed.

TODO: Add part numbers of known-good infrared LEDs and receivers. The LED in this photo is Lumex OED-EL-8L (Digikey 67-1000-ND) and the receiver is probably Sharp GP1UD281YK0F (now discontinued, Digikey 425-1987-ND).

TODO: Test Vishay TSOP39338 receiver (Digikey 751-1390-5-ND). It's very likely to work. Update this photo. Maybe PJRC should sell a known-good LED & receiver pair?

 

For transmitting, you must connect the LED to a specific pin. The receiver output may be connected to any pin.

BoardReceive
Pin
Transmit
Pin
Timer
Used
PWM Pins
Disabled
Teensy 3.1Any5CMTNone
Teensy 3.0Any5CMTNone
Teensy 2.0Any10412
Teensy 1.0Any17115, 18
Teensy++ 2.0Any120
Teensy++ 1.0Any120

Basic Usage

IRremote acts like 2 libraries, one for sending and one for receiving. Usually it's easiest to find the codes to transmit by first using the receiver.

Receiving

IRrecv irrecv(receivePin)

Create the receiver object, using a name of your choice.

irrecv.enableIRIn()

Begin the receiving process. This will enable the timer interrupt which consumes a small amount of CPU every 50 µs.

irrecv.decode(&results)

Attempt to receive a IR code. Returns true if a code was received, or false if nothing received yet. When a code is received, information is stored into "results".

results.decode_type: Will be one of the following: NECSONYRC5RC6, or UNKNOWN
results.value: The actual IR code (0 if type is UNKNOWN) 
results.bits: The number of bits used by this code 
results.rawbuf: An array of IR pulse times 
results.rawlen: The number of items stored in the array

irrecv.resume()

After receiving, this must be called to reset the receiver and prepare it to receive another code.

irrecv.blink13(true)

Enable blinking the LED when during reception. Because you can't see infrared light, blinking the LED can be useful while troubleshooting, or just to give visual feedback.

Transmitting

IRsend irsend;

Create the transmit object. A fixed pin number is always used, depending on which timer the library is utilizing.

irsend.sendNEC(IRcode, numBits);

Send a code in NEC format.

irsend.sendSony(IRcode, numBits);

Send a code in Sony format.

irsend.sendRC5(IRcode, numBits);

Send a code in RC5 format.

irsend.sendRC6(IRcode, numBits);

Send a code in RC6

irsend.sendRaw(rawbuf, rawlen, frequency);

Send a raw code. Normally you would obtain the contents of rawbuf and rawlen by using the receiver many times and averaging the results. Some adjustments may be necessary for best performance. The frequency is the expected bandpass filter frequency at the receiver, where 38 is the most commonly used.

Examples

          This example is read signal to RAW and Send out
  1. /* IRrecord: record and play back IR signals as a minimal 
     * An IR detector/demodulator must be connected to the input RECV_PIN.
     * An IR LED must be connected to the output PWM pin 3.
     * A button must be connected to the input BUTTON_PIN; this is the
     * send button.
     * A visible LED can be connected to STATUS_PIN to provide status.
     *
     * The logic is:
     * If the button is pressed, send the IR code.
     * If an IR code is received, record it.
     *
     * Version 0.11 September, 2009
     * Copyright 2009 Ken Shirriff
     * http://arcfn.com
     */

    #include <IRremote.h>

    int RECV_PIN = 11;
    int BUTTON_PIN = 12;
    int STATUS_PIN = 13;
    // Pin 3 for Send IR

    IRrecv irrecv(RECV_PIN);
    IRsend irsend;

    decode_results results;

    void setup()
    {
      Serial.begin(9600);
      irrecv.enableIRIn(); // Start the receiver
      pinMode(BUTTON_PIN, INPUT);
      pinMode(STATUS_PIN, OUTPUT);
    }

    // Storage for the recorded code
    int codeType = -1; // The type of code
    unsigned long codeValue; // The code value if not raw
    unsigned int rawCodes[RAWBUF]; // The durations if raw
    int codeLen; // The length of the code
    int toggle = 0; // The RC5/6 toggle state

    // Stores the code for later playback
    // Most of this code is just logging
    void storeCode(decode_results *results) {
      codeType = results->decode_type;
      int count = results->rawlen;
      if (codeType == UNKNOWN) {
        Serial.println("Received unknown code, saving as raw :: ");
        //Serial.println("");
        codeLen = results->rawlen - 1;
        // To store raw codes:
        // Drop first value (gap)
        // Convert from ticks to microseconds
        // Tweak marks shorter, and spaces longer to cancel out IR receiver distortion
        for (int i = 1; i <= codeLen; i++) {
          
          if (i % 2) {
            // Mark
            rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK - MARK_EXCESS;
            Serial.print(" m");
          } 
          else {
            // Space
            rawCodes[i - 1] = results->rawbuf[i]*USECPERTICK + MARK_EXCESS;
            Serial.print(" s");
          }
          
          Serial.print(rawCodes[i - 1], DEC);
          //Serial.println(rawCodes[i - 1], HEX);
        }
        //Serial.println("");
        //Serial.println("-----END----");
      }
      else {
        if (codeType == NEC) {
          Serial.print("Received NEC: ");
          if (results->value == REPEAT) {
            // Don't record a NEC repeat value as that's useless.
            Serial.println("repeat; ignoring.");
            return;
          }
        } 
        else if (codeType == SONY) {
          Serial.print("Received SONY: ");
        } 
        else if (codeType == PANASONIC) {
          Serial.print("Received PANASONIC: ");
        }
        else if (codeType == JVC) {
          Serial.print("Received JVC: ");
        }
        else if (codeType == RC5) {
          Serial.print("Received RC5: ");
        } 
        else if (codeType == RC6) {
          Serial.print("Received RC6: ");
        } 
        else {
          Serial.print("Unexpected codeType ");
          Serial.print(codeType, DEC);
          Serial.println("");
        }
        Serial.println(results->value, HEX);
        codeValue = results->value;
        codeLen = results->bits;
      }


        
    }

    void sendCode(int repeat) {
      
      if (codeType == NEC) {
        if (repeat) {
          irsend.sendNEC(REPEAT, codeLen);
          Serial.println("Sent NEC repeat");
        } 
        else {
          irsend.sendNEC(codeValue, codeLen);
          Serial.print("Sent NEC ");
          Serial.println(codeValue, HEX);
        }
      } 
      else if (codeType == SONY) {
        irsend.sendSony(codeValue, codeLen);
        Serial.print("Sent Sony ");
        Serial.println(codeValue, HEX);
      } 
      else if (codeType == PANASONIC) {
        irsend.sendPanasonic(codeValue, codeLen);
        Serial.print("Sent Panasonic");
        Serial.println(codeValue, HEX);
      }
      else if (codeType == JVC) {
        irsend.sendPanasonic(codeValue, codeLen);
        Serial.print("Sent JVC");
        Serial.println(codeValue, HEX);
      }
      else if (codeType == RC5 || codeType == RC6) {
        if (!repeat) {
          // Flip the toggle bit for a new button press
          toggle = 1 - toggle;
        }
        // Put the toggle bit into the code to send
        codeValue = codeValue & ~(1 << (codeLen - 1));
        codeValue = codeValue | (toggle << (codeLen - 1));
        if (codeType == RC5) {
          Serial.print("Sent RC5 ");
          Serial.println(codeValue, HEX);
          irsend.sendRC5(codeValue, codeLen);
        } 
        else {
          irsend.sendRC6(codeValue, codeLen);
          Serial.print("Sent RC6 ");
          Serial.println(codeValue, HEX);
        }
      } 
      else if (codeType == UNKNOWN /* i.e. raw */) {
        // Assume 38 KHz
        //irsend.sendRaw(rawCodes, codeLen, 38);
        irsend.sendRaw(rawCodes, sizeof(rawCodes) / sizeof(rawCodes[0]), 38);
        Serial.println("Sent raw");

        for (int i = 1; i <= codeLen; i++) {
          Serial.print(rawCodes[i - 1], DEC);
        }
        Serial.println("");
      }
    }

    int lastButtonState;

    void loop() {
      // If button pressed, send the code.
      int buttonState = digitalRead(BUTTON_PIN);
      if (lastButtonState == HIGH && buttonState == LOW) {
        Serial.println("Released");
        irrecv.enableIRIn(); // Re-enable receiver
      }

      if (buttonState) {
        Serial.println("Pressed, sending");
        digitalWrite(STATUS_PIN, HIGH);
        sendCode(lastButtonState == buttonState);
        digitalWrite(STATUS_PIN, LOW);
        delay(50); // Wait a bit between retransmissions
      } 
      else if (irrecv.decode(&results)) {
        digitalWrite(STATUS_PIN, HIGH);
        storeCode(&results);
        irrecv.resume(); // resume receiver
        digitalWrite(STATUS_PIN, LOW);
      }
      lastButtonState = buttonState;
    }