Tuesday, April 15, 2014

Send IR commands with Arduino

Sending IR commands from the Arduino is quite simple. It is necessary to connect the IR LED on a specific Arduino pin and write a program to send IR commands. In this example, I connected the IR LED on the Arduino digital pin 2 and wrote a simple sketch to send IR commands by NEC protocol.
Sending commands is very simple, all you need to do is call the function "sendCode (94, 248)". The first number is the address of the device in this case YAMAHA AV receiver and the second number is the command in this case POWER.

More information about NEC protocol can be found at: SB-Projects: IR Remote Control NEC protocol.

Sketch for sending NEC IR commands
// *********************************************************
// Program: NEC PROTOCOL INFRARED REMOTE SENDER
// Version: 1.0
// Author: Elvis Baketa
// Description: 
// *********************************************************

// definitions of constants
#define pulseTime 560         // duration of carrier pulse
#define irLed 2               // ir led connected to digital pin 2
#define YAMAHA 94             // device address byte
#define STANDBY 248           // device command byte

// standard Arduino setup routine
void setup()
{
  pinMode(irLed, OUTPUT);     // set irled pin as output
  digitalWrite(irLed, LOW);   // turn of ir led
  
  // send test command to turn on/off yamaha av receiver
  sendCode(YAMAHA, STANDBY);
}

// standard Arduino loop routine
void loop()
{  
}

// routines to create a carrier pulse
void carrierPulse(unsigned int duration)
{
  for(int i=0; i < (duration / 35); i++)
    {
      digitalWrite(irLed, HIGH);   // set irled to high
      delayMicroseconds(13);       // duration of high pulse
      digitalWrite(irLed, LOW);    // set irled to low
      delayMicroseconds(13);       // duration of low pulse
    }
}

// routines for sending code
void sendCode(byte addressByte, byte commandByte)
{
  // preparing the code for sending
  unsigned long code = 0;
  
  code = addressByte;
  code = code << 8;
  code = code | addressByte ^ 0xFF;
  code = code << 8;
  code = code | commandByte;
  code = code << 8;
  code = code | commandByte ^ 0xFF;
  
  // start sending code
  // send AGC pulse approximate to 9ms
  carrierPulse(16 * pulseTime);
  
  // space pulse approximate to 4.5ms
  delayMicroseconds(8 * pulseTime);
  
  // send bits one by one, MSB first
  for (int i=31; i>=0; i--)
    {
      if (bitRead(code, i))
      {
        carrierPulse(pulseTime);
        delayMicroseconds(3 * pulseTime);
      }else{
        carrierPulse(pulseTime);
        delayMicroseconds(pulseTime);
      }
    }
    
    // send stop bit
    carrierPulse(pulseTime);
}

No comments:

Post a Comment

Programing AT89C2051 using SDCC compiler on Debian Linux

My Homemade AT89C2051 Development Board First and foremost we need to install a compiler and we need a programmer to load the code into the ...