Connect with us

Clone IR Remote for Arduino Projects (IRlib2)

Discussion in 'Microcontrollers, Programming and IoT' started by joshzstuff, Jul 1, 2017.

Scroll to continue with content
  1. joshzstuff

    joshzstuff

    49
    1
    Jul 4, 2010
    Hello Gang!

    I am trying to clone the IR signal of consumer electronics to give IR control to my Arduino.
    (I wish to give the Arduino the ability to turn on and off a TV)
    This seems simple enough, and there are several tutorials online, so, easy, right?

    I've found several examples of how to copy the IR signal into the Serial Monitor my problem is re-transmitting them with my IR Led from my arduino.

    Demo Hardware:


    My breadboard example:
    I'm using an detector to collect the IR signals, and a 940nm IR LED to transmit.

    [​IMG]

    Most of the tutorials I find on the internet are based on outdated IR Libraries.
    (I don't mind this, as long as I can get them to work)

    After failing to transmit the captured codes from my IR Led I tired the examples from the new IRlib2 library.

    I've tried the "rawRecv" example sketch and successfully captured the timing data.

    Code:
    /* rawR&cv.ino Example sketch for IRLib2
     *  Illustrate how to capture raw timing values for an unknow protocol.
     *  You will capture a signal using this sketch. It will output data the
     *  serial monitor that you can cut and paste into the "rawSend.ino"
     *  sketch.
     */
    // Recommend only use IRLibRecvPCI or IRLibRecvLoop for best results
    #include <IRLibRecvPCI.h>
    
    IRrecvPCI myReceiver(2);//pin number for the receiver
    
    void setup() {
      Serial.begin(9600);
      delay(2000); while (!Serial); //delay for Leonardo
      myReceiver.enableIRIn(); // Start the receiver
      Serial.println(F("Ready to receive IR signals"));
    }
    
    void loop() {
      //Continue looping until you get a complete signal received
      if (myReceiver.getResults()) {
        Serial.println(F("Do a cut-and-paste of the following lines into the "));
        Serial.println(F("designated location in rawSend.ino"));
        Serial.print(F("\n#define RAW_DATA_LEN "));
        Serial.println(recvGlobal.recvLength,DEC);
        Serial.print(F("uint16_t rawData[RAW_DATA_LEN]={\n\t"));
        for(bufIndex_t i=1;i<recvGlobal.recvLength;i++) {
          Serial.print(recvGlobal.recvBuffer[i],DEC);
          Serial.print(F(", "));
          if( (i % 8)==0) Serial.print(F("\n\t"));
        }
        Serial.println(F("1000};"));//Add arbitrary trailing space
        myReceiver.enableIRIn();      //Restart receiver
    I get this code from my samsung remote:
    Code:
    Do a cut-and-paste of the following lines into the
    designated location in rawSend.ino
    
    #define RAW_DATA_LEN 68
    uint16_t rawData[RAW_DATA_LEN]={
        4530, 4514, 562, 1686, 562, 1682, 566, 1682,
        562, 590, 534, 590, 534, 590, 534, 590,
        534, 590, 534, 1686, 558, 1686, 562, 1686,
        562, 590, 534, 590, 534, 590, 530, 594,
        530, 594, 530, 590, 534, 1686, 562, 590,
        534, 590, 534, 590, 534, 590, 534, 594,
        526, 594, 530, 1686, 562, 590, 534, 1690,
        558, 1682, 562, 1686, 562, 1686, 562, 1686,
        562, 1682, 562, 1000};
    Here is the 'rawSend' example sketch:


    Code:
    /* rawSend.ino Example sketch for IRLib2
     *  Illustrates how to send a code Using raw timings which were captured
     *  from the "rawRecv.ino" sample sketch.  Load that sketch and
     *  capture the values. They will print in the serial monitor. Then you
     *  cut and paste that output into the appropriate section below.
     */
    #include <IRLibSendBase.h>    //We need the base code
    #include <IRLib_HashRaw.h>    //Only use raw sender
    
    IRsendRaw mySender;
    
    void setup() {
      Serial.begin(9600);
      delay(2000); while (!Serial); //delay for Leonardo
      Serial.println(F("Every time you press a key is a serial monitor we will send."));
    }
    /* Cut and paste the output from "rawRecv.ino" below here. It will
     * consist of a #define RAW_DATA_LEN statement and an array definition
     * beginning with "uint16_t rawData[RAW_DATA_LEN]= {…" and concludes
     * with "…,1000};"
     */
    
    
    
    
    
    /*
     * Cut-and-paste into the area above.
     */
      
    void loop() {
      if (Serial.read() != -1) {
        //send a code every time a character is received from the
        // serial port. You could modify this sketch to send when you
        // push a button connected to an digital input pin.
        mySender.send(rawData,RAW_DATA_LEN,36);//Pass the buffer,length, optionally frequency
        Serial.println(F("Sent signal."));
      }
    }
    Hardware


    I'm using a 940nm IR LED driven by a 2222a BJT.
    I used the diagram from a tutorial that used a 4.6K resistor on the base.
    (should I have a current limiter for the IR LED?)

    Here is my question:


    After I paste the timing code into "rawSend" How do I code the LED to turn on and off with the appropriate timing?

    I've tried many tutorials I found online, but perhaps because they use a different library, I'm not having success.

    I've tried examples like these:


    Code: Clone IR Signals with Arduino

    Adafruit IR Library example

    http://www.instructables.com/id/Arduino-Infrared-Remote-tutorial/

    Using IR commands to control consumer electronics is something I'd like to use a lot once I figure it out.
    Any tips would be greatly appreciated!

    Thanks!
     
  2. Bluejets

    Bluejets

    4,268
    907
    Oct 5, 2014
    I don't see why you need to re-transmit a signal from the arduino when you already have a control that transmits the required signal.
     
  3. joshzstuff

    joshzstuff

    49
    1
    Jul 4, 2010
    A reasonable question
    (if it was a question, instead of an allusion that I'm meddling with frivolity as it came across ;)
    (which, by the way, forums are full of, but not this time! I actually do have a purpose! Just keep reading!)

    Sure, I'll provide the background info if you like.

    You can find my project details here:
    https://www.electronicspoint.com/th...actuators-for-tv-control.280249/#post-1704670

    My project includes swinging open a set of TVs on hinges automatically (this is done with linear actuators)
    The actuators move too fast, so I'm using an Arduino to PWM pulse them at the right times to let them come to a gradual stop instead of the abrupt SLAM! that they are doing now.

    Since I'm already using a micro-controller for each TV, it is a trivial matter to send an IR pulse to turn them ON and OFF while they open and close.

    (a 'trivial' process for the uController, that is, once I figure out how to tell it to do it!)

    Sure, I could just grab a remote or 2, then walk up to a stage, (where the TVs are mounted) and open and turn on the TV's every time they are needed (and then close them and turn them off when finished)

    But this is 2017, and we have this thing called "automation" which uses our tech to do stuff quicker/ easier/ and more efficient than manually.

    We also have these wonderful resources called "forums" which let people share their knowledge and build on what they know.
    They are generally very helpful, so long as potential contributors deem your project "useful" and "worthy of their time" to extol you with their great stores of wisdom!"

    I humbly await your wisdom, oh grand forum, please extol me with your virtues!
     
  4. Bluejets

    Bluejets

    4,268
    907
    Oct 5, 2014
    As a pointer, it is not uncommon for a basic idea to quickly become a complex mess unless a set of rules is followed. Try planning and implementing in Arduino.
     
    Last edited: Jul 2, 2017
  5. joshzstuff

    joshzstuff

    49
    1
    Jul 4, 2010
    Thanks for the fortune cookie.
    I bet my 'lucky numbers' are in Hexadecimal eh?

    But seriously, I found a video that describes all of the steps, only he is using a computer to trigger the code.


    Here is the code that seems to work well.
    I'm going to try it exactly as he has, then after I've proven the concept I'll try to adapt it to trigger inside the Arduino.

    Code:
    #include <IRremote.h>
    
    IRrecv receiver(2); // receiver is connected to pin2
    IRsend sender;
    decode_results results;
    
    long repetitions;
    long count;
    unsigned int durations[100];
    void (*reset)(void) = 0;
    
    void setup() {
      Serial.begin(9600);
      receiver.enableIRIn(); // start receiving signals
    }
    
    void loop() {
    
      // check for text from the PC
      // the PC sends a string containing "r,n,a,b,c,d,e,..." where r is how many times to repeat the command,
      // n is the number of durations, and a/b/c/d/e/... are the durations.
      // the durations are how long each mark and space period of an infrared command should last, in microseconds.
      if(Serial.available()) {
    
        // parse the text
        repetitions = Serial.parseInt();
        count = Serial.parseInt();
        for(int i = 0; i < count; i++)
          durations[i] = Serial.parseInt();
    
        // send the command using 40kHz PWM
        for(int i = 0; i < repetitions; i++) {
          sender.sendRaw(durations, count, 40);
          delay(50);
        }
    
        // for a bit of fault tolerance, reset the arduino after receiving any command
        reset();
        
      }
    
      // check if a decoded infrared signal is available
      if(receiver.decode(&results)) {
        Serial.println(results.value, HEX);
        Serial.print(results.rawlen - 1);
        for(int i = 1; i < results.rawlen; i++) {
          unsigned int number = results.rawbuf[i] * USECPERTICK;
          Serial.print(",");
          Serial.print(number);
        }
        Serial.println("");
        receiver.resume();
      }
     
    }
     
  6. jagan4

    jagan4

    1
    0
    Jul 3, 2017
    Hi @joshzstuff,
    Did you try this Arduino IR remote project, this is exactly what you want.
    Here this guy created a Android app to send IR codes via bluetooth and then Arduino at the other end captures and decode them via HC-05 and TSOP. Then Arduino again send those decoded signals to TV/DVD player via IR led by illuminating the LED in particular pattern for each button.
    Basically he is sending IR signals via Arduino and IR LED, which is perfectly received by the TV. Check the video too.
    He has used this IR library on GIT hub for IR signals.
    Let me know if this works for you
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-