will be read-only starting December 31st, 2018. For more info please look at this Forum Post

Multitasked IR transmission


This sketch lets you transmit a Sony remote code, and its main feature is that the sending function works in background, and that's why i decided to call it "multitasked transmission".

How to use it


All you have to do to send the Sony code is specify the code itself of course, and the number of bits you wish to transmit. The maximum code lenght is 32 bits, since the datatx variable is unsigned long (of course you can modify it if you need a longer code). In case you want this code to talk to a Sony device remember that for it to "understand" the code, it must be sent 3 times, and there must be a delay of 45 milliseconds between the start of each transmission (the coding for this is already included below). You can also use the code to transmit data to another arduino. (for reception you can use this library by Ken Shirriff:


You must connect an IR led on pin3. Remember to put a resistor in series.

More info

The library uses timer2 both to work in background (by calling an ISR with a frequency of 40KHZ, or once every 25us) and to generate PWM at 40KHz. It can be easily modified so that it uses timer1 instead.


This example code uses the SonyIR() function to send a Sony remote code in background. It sends a code every 2500ms.


 Multitasked IR transmission

 For more infos, check out
 or mail me at

 created 13 July 2010
 by Riccardo Argiolas
 aka CaptainTuna


#define TOPBIT 0x80000000
int timertx=0; int resettx=0; int bitstx=0; float elapsed=0; unsigned long datatx=0; boolean spacetx = false;

void setup(){

  //set PWM: FastPWM, OC2A as top, OC2B sets at bottom, clears on compare
  //COM2B1=1 sets PWM active on pin3, COM2B1=0 disables it
  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2A = _BV(WGM21) | _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS21);
  OCR2A = 49;
  OCR2B = 24;


void loop(){

  SonyIR(0xa90, 12);
  SonyIR(0xa90, 12);
  SonyIR(0xa90, 12);


boolean SonyIR(unsigned long data, int nbits) {

  if(bitstx == 0) { //if IDLE then transmit 
    timertx=0; //reset timer
    TIMSK2 |= _BV(TOIE2); //activate interrupt on overflow (TOV flag, triggers at OCR2A)
    resettx=96; //initial header pulse is 2400us long. 2400/25us ticks = 96
    spacetx = false;  
    datatx=data << (32 - (nbits + 1)); //unsigned long is 32 bits. data gets   shifted so that the leftmost (MSB) will be the first of the 32.
    TCCR2A |= _BV(COM2B1); // Enable pin 3 PWM output for transmission of header
    bitstx=nbits + 1; //bits left to transmit, including header
    return true;
  else {
    return false;





  if(bitstx != 0) {  //if we have got something to transmit

    if(timertx >= resettx) {  //if reset value is reached

      if(spacetx){  //it was a space that has just been sent thus a total "set" (bit + space) so..  
        spacetx = false; //we are not going to send a space now  
        bitstx = bitstx - 1;  //we got 1 less bit to send
        datatx <<= 1;  //shift it so MSB becomes 1st digit
        TCCR2A |= _BV(COM2B1);  //activate pin 3 PWM (ONE)
          if((datatx & TOPBIT)&&(bitstx != 0)) {  
            resettx = 48;
          else if(!(datatx & TOPBIT)&&(bitstx != 0)){
            resettx = 24;
            TCCR2A &= ~(_BV(COM2B1));  //deactivate pin 3 PWM (end of transmission, no more bits to send)
            TIMSK2 &= ~(_BV(TOIE2));   //deactivate interrupts on overflow
      else {  //we sent the bit, now we have to "send" the space
        spacetx = true;  //we are sending a space
        resettx = 24; //600us/25us = 24
        TCCR2A &= ~(_BV(COM2B1));



Many thanks go to Ken Shirriff for his great blog. It helped me a lot to understand how timers and background tasking work. Do check it out:
Many thanks to the guys at the arduino forums who helped me solve my doubts.


Feel free to mail me at frsmnk (at) gmail (dot) com