Universal IR Remote Control

Description

Description

Contributor:

Deepesh Verma

With technological advancements in consumer electronics, nowadays almost every appliance in our home is remote controlled.

Managing a different Remote (average figure) for TV, AC, Fan, DVD, DTH, etc., is quite difficult. What if we could have only one remote capable of working as a dedicated remote for any appliance any time?

Fig. 1: Prototype of Arduino and TSOP1738 based Universal IR Remote Control Receiver

Presented here is such a device based on Arduino that can act as Remote control for any appliance when needed.

Fig. 2: Overview of Arduino based Universal IR Remote Control

The remote controls for electronic appliances use IR (Infra-Red) communication technique. A specific code is assigned to each key of the remote , based on a standard protocol.

There are various protocols designed by manufacturers, such as NEC, Sony SIRC, Philips RC5, Philips RC6; each carrying a different coding scheme. For example, in NEC protocol based remote, 32-bit code is transmitted, whereas in SONY, 12-bit code is transmitted.

Basically, these codes are nothing but pulses with two levels viz. High and Low. The duration of each pulse is in milliseconds, which is decided by the manufacturer.

The frequency at which the signal is modulated is standardized to 38 kHz. To prevent interference from IR sources such as sunlight or lights, the LED is not turned on steadily, but is turned on and off at a modulation frequency (typically 36 or 38KHz). Thus every receiver must be capable of receiving a 38 kHz signal effectively. A very simple and reliable device for IR signal reception is TSOP17XX, where XX denotes the frequency to which it responds. TSOP1738 is commonly used in IR receiver applications.

TSOP 1738

The TSOP is a miniaturized receiver for infrared remote control systems. PIN diode and preamplifier are assembled on lead frame, while the epoxy package is designed as IR filter. The demodulated output signal can be directly decoded by a microprocessor. TSOP is the standard IR remote control receiver series, supporting all major transmission codes. Its output is Active Low.

Fig. 3: Pin Diagram of TSOP1738 IR Receiver

How to Decode a Remote?

To know what signal is transmitted when a key on remote is pressed, we need to decode it. This can be done with the help of Oscilloscope. Although it seems quite difficult, finding an oscilloscope at home is not possible. Well, an affordable solution to this problem lies in Arduino Library. “IRremote” library provides a ready code to decode the IR remote and display it on the serial monitor.

In Arduino IDE 1.0.5 and later, this comes pre-loaded, whereas for older version you can get the library from Arduino’s official website.

‘IRrecvDemo’ is a program to decode the IR signals. Get an Arduino and make connections with TSOP1738 as shown below:

Fig. 4: Circuit Diagram of Arduino based IR Remote Receiver

Now upload the code ‘IRrecvDemo’ to the board.

After being done with uploading, open serial monitor window and point the remote you want to decode towards the TSOP sensor.

Press any key and a code (Hexadecimal) will appear on window. Note down the code for further reference.

If you do not know about the type of protocol of your remote, use the program ‘IRrecord’ in the Examples. Upload the program and repeat the above procedure. On the serial monitor, you’ll find Protocol type along with the HEX code.

Similarly, record the codes for all keys and remotes that you want to replicate into one. These HEX codes will be inserted in the program of universal remote.

Making Up the Universal Remote

In the remote we have made 6 keys viz., Channel UP; Channel DOWN; Volume UP; Volume DOWN; Power ON/OFF and MUTE.

These 6 keys are connected in a 2x3 Matrix, which is easy to interface with Arduino.

Also, to select which appliance is to be operated, (since appliances have different protocols) we provide Select buttons. The number of select buttons depends on the number of remotes to be replicated.

For the sake of simplicity we have taken 3 select buttons, 1 each for TV, DVD and DTH (You can choose any appliance depending on your requirement).

Thus the Remote control will have a total of 9 Keys, (3 select and 6 operational keys)

The IR LED is connected between Digital pin 3 of Arduino and Ground through a 330E resistor.

Select Switches are connected to Digital pins 9, 10 and 11, whereas the operational keys are interfaced in the form of a 2x3 matrix with digital pins 4,5,6,7 and 8.

The operation of the remote is quite simple and straight forward. The select buttons help to decide, which appliance will be controlled by the remote. Say, if Switch1 is pressed, Applliance1 (here, T.V.) will be controlled. Now, on pressing operational keys, corresponding code will be transmitted.

After pressing operation, all work is done by the heart of the remote i.e. Arduino and finally IR LED plays like cherry on the cake.

unsigned int TIMEOUT = TICKS11ms; // the pulse should occur before this time excede Otherwise it is an error

unsigned int PREPULSE = TICKS8ms;// the interrupt should occur after this time Otherwise it is an error

static unsigned short long timer;// varible to keep track of long timeouts ( it can also be int if you want to save flash memory for some other purpose )

static unsigned char dataready;// varible to use as flag when data is completly received and ready it is 1 else 0

static unsigned char necpoj=0; /* (necpoj=NEC position )this varible is used to keep track of the edges of the input singal

as decoding of the singal is done by a state machine

so this varible acutalley sotores what state we currently are

and total bits 32 and 2 leading pulse */

unsigned char address=0,notaddress=0;// these varible are used to store received address

unsigned char command=0,notcommand=0;// these varible are used to store received address

void interruptOnChangeIsr(void); // interrupt service routine for interrupt on change of input port for IR sensor of mcu

void timerInterruptIsr(void);// interrupt service rouine for timer0

void interrupt t0intr(void)

{

if(INTCONbits.T0IF)// check the timer0 over flow interrupt flag

{

timerInterruptIsr();// timer0 overflow interrupt has been occur call the isr

INTCONbits.T0IF =0;// clear the timer0 interrupt flag

}

}

/* THE main source code Start here*/

void main()

{

TRISAbits.TRISA2=1;

PORTAbits.RA2 = 0;

//INTERRUPT ON CHANGE INPUT only RA2

TRISB=0X00;

PORTB=0X00;

PCON=0X08; //SET INT OSC AT 4 MHZ

CMCON=0x07;// disable the comparator

TMR0 = 0;// clar the timer

OPTION_REG = 0x88;//pullups are disabled

//timer0 clock source is internal

//timer0 perscaller is 1:1 (disabled "assigned to WDT")

INTCONbits.T0IE = 1;// Timer0 overflow interrupt enable

INTCONbits.T0IF = 0; // clar the timer0 intrrupt flags

INTCONbits.INTE = 1; // external interrupt on RB0 pin is enabled

INTCONbits.RBIF = 0;// clear the external interrrupt flag

INTCONbits.PEIE = 1; // peripheral intrrupt enable

INTCONbits.GIE = 1; // GLOBL interrupt enable

while(1) // wait forever for the data received and ready

{

if(dataready=0)// data is received and ready to be procssed

{

// key1 0x21toToggle relay 1// these are command of the IR remote control which i have

// key2 0xA1toToggle relay 2

// key3 0x61toToggle relay 3

// key4 0x80toToggle relay 4

// key5 0x40toToggle relay 4

// key6 0x00toTurn off all the relays

switch(command)// swich on

{

case 0x21: PORTBbits.RB3=0; // LED ON

break;

case 0xA1:PORTBbits.RB7 = 0; //LED ON

break;

case 0x61: PORTBbits.RB4 =0; //LED ON

break;

case 0x80: PORTBbits.RB5=0;//LED ON

break;

case 0x40:PORTBbits.RB1=0; //LED ON

break;

case 0x00:PORTBbits.RB6=0; //LED ON

// break;

default :

break;

}

INTCONbits.GIE = 1; // Enable the interrupts

dataready=0;// data has been processed so clear the dataready flag

}

}

}

void interruptOnChangeIsr(void)

{

unsigned short long tdiff;

unsigned char pin;

static unsigned long rxbuffer;

tdiff = ((timer<<8)+TMR0) ; // calculate how much time has been passed since last interrupt

// the time shold be less then time out and greater than PREPULSE

pin = IRSENSOR;// store the current status of Sensor

TMR0 = 0;// reset the timer0 to measure the next edge(interrupt) of input

timer = 0;// reset the timer varible to

if ((tdiff>PREPULSE) && (tdiff<TIMEOUT) )// the edge (interrupt) occurrence time should be less then the TIMOUT and greater then PREPULESE else it is an fake singal

{// At the very first edge (necpoj==0) this conditon will always false and the false block of this if will bring the state machine (necpoj) to position 1(position 1 means 9ms leading pulse has started now we have to wait for 4.5ms start pulse to occur)

if(necpoj==1 || necpoj==2)// when we are hear it means 9ms leding pulse has ended and now we are necpoj=1 or necpoj=2

{

if((pin==1) && (necpoj==1))

{

necpoj++;

TIMEOUT = TICKS5o5ms; // timeout for 3rd pulse 5.5ms

PREPULSE = TICKS3ms;// PREPULSE for 3rd pulse 3ms

}

else if((pin==0)&& (necpoj ==2))

{

necpoj++;

TIMEOUT = TICKS2o3ms; // now data starts so timeout is 2.3ms

PREPULSE = TICKS0o2ms;

}

else// this block handle the conditon if any error occur after the completing the pre pulses

{

necpoj = 0;//reset the state machine

TIMEOUT = TICKS11ms;

PREPULSE = TICKS8ms;

}

}

else if(necpoj>2)//now we are picking the data

{

necpoj++;//necpoj sill inrement on every edge

if(necpoj&0x01)// here we check the if necpoj is an odd number because when necpoj goes greater then 3 then

//necpoj will always be and odd value when a single bit tranmission is over

{

rxbuffer=rxbuffer<<1;//shift the buffer

if(tdiff>1250)//we are here means we just recevied the edge of finished tranmission of a bit

// so if last edge was more than 1.24 ms then the bit which is just over is one else it is zero

{

rxbuffer = rxbuffer | 0x1;

}

else

{

rxbuffer = rxbuffer |0x0;

}

}

if(necpoj >66)// we have reached (Leading pulse 2 +address 16+~address16+ command 16+ ~command 16+ last final burst first edge 1)=67th edge of the message frame means the date tranmission is now over

{

address = (rxbuffer>>24)& 0xFF;//extract the data from the buffer

notaddress = (rxbuffer>>16)& 0xFF;

command = (rxbuffer>>8)& 0xFF;

notcommand = (rxbuffer)& 0xFF;

rxbuffer=0;//clear the buffer

if((!(address & notaddress)) && (!(command & notcommand)))// check weather the received data is vaild or not

{

dataready =0;

}

else

{

dataready=1;

}

TIMEOUT = TICKS11ms;// weather we received the vaild data or not we have to reset the state machine

PREPULSE = TICKS8ms;

necpoj=0;

}

}

else

{

TIMEOUT =TICKS11ms;// some error occured reset state machine

PREPULSE =TICKS8ms;

}

}

else

{

if(pin==0)//we are here means that after a longtimeout or PREPULSE we just detect a pulse which may be the start of 9ms pulse

{

necpoj = 1;// yes it could be the start of 9ms pulse

}

else

{

necpoj = 0;// no it's not start of 9ms pulse

}

address = 0xFF;

notaddress = 0xFF;

command = 0xFF;

notcommand = 0xFF;

dataready = 0x000;

TIMEOUT = TICKS11ms;//default timing

PREPULSE = TICKS8ms;

}

}

void timerInterruptIsr(void)

{

if(timer<0xFFFF)// this code is to increment the variable timer's value on every over flow but this if conditon will prevent this variable form rollover when a long timeout occurs