Saturday, September 7, 2013

NEC Protocol IR (Infrared) Remote Control With a Microcontroller

Introduction          

        This little project will demonstrate how you can build NEC protocol based Infrared Remote Control to use with 
various NEC Protocol IR receivers.

        actually there are lots of projects out there to accomplish this task but i have to write my own code because of too many requests on this IR(infrared) Remote Control Relay Board with PIC 12F675 Microcontroller   people keep asking "Where is the Transmitter for this" although you can use any NEC protocol based remote ,but i just wanted to build one by my self. so here it is.





         you can use this NEC IR Remote Control with my IR(infrared) Remote Control Relay Board .

       12 Channel Relay + 2 Channel Dimmer control by IR Remote and keyboard

Consumer IR protocols    
    There are a number of consumer Infrared protocols out there and they have been used for every single purpose possible i guess, like PDA laptops and other consumer appliances. RC-5 & RC-6 by Phillips , RCA are few examples of consumer IR protocols.


       In this demonstration we will stick the to NEC protocol by NEC corporation,



NEC Infrared Protocol 


  • A 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
  • A 4.5ms space
  • The 8-bit address for the receiving device
  • The 8-bit logical inverse of the address
  • The 8-bit command
  • The 8-bit logical inverse of the command
  • Final 562.5µs pulse burst to show end of message transmission.
  • Logical '0' – a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
  • Logical '1' – a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25    

The transmission of 0 and 1 is shown in the image blow 


  
           There are  four bytes of data bits are being sent in  least significant bit first order the figure blow shows the format of an NEC IR transmission frame, for a 
command of 0xB1 (10110001b) and an address of 0x8D (10001101b) .  






16 bits for the address (address + inverse) require 
27ms to transmit time .and the 16 bits for the command (command + inverse) also require 27ms to transmit time.  

because (address + address inverse) or (command+command inverse) will always contain 8 '0's  and 8 '1's    so (8 * 1.125ms) + (8 * 2.25ms) == 27 ms . 
according to this total time required to transmit the frame is (9ms +4.5ms +27ms+27ms) = 67.5 ms. 


Verify with Oscilloscope and logic analyser 

 the image  give blow is take by Rigol DS1052E Oscilloscope 


Logic Analyser shows the timing details
T1 leading pulse at  84.115ms 
T2  space on 93.28ms 
T3 Address starts at 97.580ms
T4 Address ends , address inverse starts 107.670ms
T5 address inverse ends , command starts at 124.486ms
T6 Command ends, command inverse starts 135.696ms
T7 Command inverse ends and last 562.5µs pulse to show end of transmission 




Extended NEC protocol (not used in this demonstration)       
        
         The NEC protocol is so widely used that soon all possible addresses were used up. By sacrificing the address redundancy the address range was extended from 256 possible values to approximately 65000 different values. This way the address range was extended from 8 bits to 16 bits without changing any other property of the protocol. The command redundancy is still preserved. Therefore each address can still handle 256 different commands.in extended protocol instead of sending address and address inverse we send address low and address high as shown in the image blow.






Repeat Codes

          If the key on the remote controller is kept depressed, a repeat code will be issued, typically around 40ms after the pulse burst that signified the end of the message. A repeat code will continue to be sent out at 108ms intervals, until the key is finally released. The repeat code consists of the following, in order:

A 9ms leading pulse burst
A 2.25ms space
A 562.5µs pulse burst to mark the end of the space (and hence end of the transmitted repeat code).
the figures give blow show the timing of repeat codes



if user keeps the key depressed the repeat codes keep coming

T8 shows the timing of repeat code 



Encoding NEC Protocol with microcontroller 

Schematic 

As shown in the schematic and the pcb images this circuit use PIC12F615 as infrared encoder , the IR LED is driven by a separate npn transistor for longer range and the Coin cell Battery CR2032 is used to power the whole infrared remote. battery nominal voltage is 3.0V volts but as the datasheet says the PIC12F615 can work down to  2.0V.

 for GP4,GP5,GP1,GP0 internal pullups are being used but the GP3 pullup can't be used for GIPO as its is internally connected to MCLRE .

The current Consumption of the Circuit in sleep mode is around 35nA which is less then what datasheet claims. the CR2032 coin cell battery will last quite some time.

Hardware 


Source Code 
the Software utilize the internal PWM capability of the PIC12F615 to generate quite stable and accurate carrier frequency of 38Khz. most of the time Micro stay in the sleep mode ,even Brown out is enabled only when the wake mode to save battery. the current consumption is around 35nA when sleeping.
GitHub Repo.  

Click Here to download Source code. 

67 comments:

  1. Great, i will like to build this project, am a beginner in MCUs and i think this realy straightforward pls where is the circuit diagram, can't see it.

    ReplyDelete
    Replies
    1. schematic is there please take a look. thank you

      Delete
    2. congrats for ur amazing project.sir, i m working on remote control based project by using nec protocol(vire remote)...i want to decode data from tsop 1738 which is send by the remote..for decoding purpose can i do serial communication between Real term software(pc software for serial communication)and tsop1738.i have set so many different baud rate (like 2400,1200,900,9600,384008N1)but i couldn't able to receive desire output....shall i use the microcontroller for decoding purpose...plz guide me sir..i will be greatful for ur kind support

      Delete
    3. it is not possible to connect TSOP1738 directly to UART with NEC protocol, either you transmit in UART format or receive over NEC and retransmit over UART .

      Delete
  2. what is the range between receiver n transmitter

    ReplyDelete
  3. it is not very feasible to tell the range in general as it depends depends on few factor,

    1. what specific led and receiver part you are using?
    2. how much current you have set for your IR emitter LED, more current (up to the LED Specs) increase range but shorter battery life.
    3. and this may also affect the range ,how much IR noise you have in operating environment.

    my unit works around 20 meters or so.

    ReplyDelete
  4. Dear Sir.

    Can i use pic12f629 or pic12f675 instead of 12f615 with same HEX code
    R. Murugan
    murugabi07@yahoo.com

    ReplyDelete
    Replies
    1. you can use any mcu you like , re-compile the code for your target device

      Delete
    2. hi,
      can not compile for 12f675 , because some registers or settings not to use for this mcu's. please help for convert code.

      Delete
    3. hi sir can not compile for....12f629 or 12f675

      Delete
  5. have some Q
    1. you used power down mode for low power consumption, then how you used wake up signal from every button pressed.

    ReplyDelete
  6. Dear Gaurav,

    I found a mistake in the schematics for your infrared encoder. The IR LED gets nearly the full supply voltage of 3V when switched on. As a result, there will be a very high current flowing through the LED.
    The current through the LED must be limited to about 20-30mA. Please use a resistor in series with the LED of about 50 Ohm to make the LED happy for the rest of it's life. Thanks for your good work, i will build it! regards, Paul Prinz

    ReplyDelete
    Replies
    1. in general most IR LED is can easily work around 100mA (continuous ) the peak current for a short amount of time around 10 time of that , just like in our application.
      5.6 resistor will limit the base current , so the collector current.
      led get (supply voltage-0.2) with current limited by the base resistor.

      you can add a resistor in to collector if you like , no harm in that but, if you have less current then range will go down very significantly.

      in my test it IR LED goes around 80mA ,which is much less then specs.

      Delete
  7. i need the code for this design in VHDL code please!

    ReplyDelete
  8. What is the purpose of switch at GP3 pin of ic PIC12F615 in IR transmittor?
    Sharda

    ReplyDelete
  9. i want to know which compiler I will use..for compiling

    ReplyDelete
    Replies
    1. HI-TECH C Compiler for PIC10/12/16 MCUs V9.83 Copyright (C) 2011 Microchip Technology Inc.

      Delete
  10. many many thanks to the author "Gaurav Chaudhary", no doubt you're a gifted man with amazing talents , i'm feeling great full to you because i was looking for a such project long time ago. Now i'm requesting you to give me a C source code with hex file so that the receiver can be run by a Sony remote control unit. And obviously please mention your explanation of the code on comment section. Thanks!

    ReplyDelete
    Replies
    1. source code is freely downloadable ,

      a direct link and github link both present at end of the post

      Delete
  11. can i know what function of switch at GP3??

    ReplyDelete
    Replies
    1. function is same as other switch,

      GP3 does not have a built in pull up so i have to use a external

      Delete
  12. hi please help me replace 12f615 by 12f675 i can`t buy 12f615.pic 12f675 not support hardware pwm for generating 38Khz singal.have you code to generating 38Khz singal by software user timer and interrupt

    ReplyDelete
  13. Thank you allot . What is the compiler that you yous

    ReplyDelete
  14. hi,
    can not compile for 12f675 , because some registers or settings not using in 12f675 mcu's. please help for convert code 12f615 to 12f675. thanks

    ReplyDelete
  15. Hello, please, I would like to use pic12f75 on the transmitter but they are not in possession of the compiler. Could you do it and send, or publish only .Hex file? Send to rinosannino@tin.it. Thank you and good job.

    ReplyDelete
  16. HELLO FRIEND THAT YOU HAVE THE CODE FOR INFRARED CONTROL PIC12F75 / 629.
    IF YOU COULD HELP ME.

    JNCONTROLE@GMAIL.COM

    ReplyDelete
  17. I like to buy a remote at Amazon but how I make sure it uses the NEC Protocol?

    ReplyDelete
    Replies
    1. you have search online for the part number which is mentioned on the remote body or something like that , i am not sure

      but one thing you can do is buy a universal remote which have all the protocols so it is going to have nec also

      Delete
  18. The source code is for both the PIC's in the transmitter and receiver,am i right?..pls reply..tnx

    ReplyDelete
  19. And one thing more...Can i use MPLABX for this?..it's also from microchip inc.

    ReplyDelete
  20. please give me code for nec remote interfacing AT89S52 11.0592 mhz , i tried but unable to interface it.

    ReplyDelete
  21. sir,
    I complete the circuit but it is not working can you help me this project.

    ReplyDelete
  22. Sir,
    I complete the NEC Protocol IR Transmitter and Receiver Circuit as per your Circuit diagram but that is not working please help me about this circuit.

    ReplyDelete
    Replies
    1. you made the remote only or both?

      things which can go wrong
      1. incorrect connection
      2. battery
      3. firmware is not burned properly.
      4. any of the semiconductor part is faulty.

      if you have any debuging tool the you can view the waveform of the remote and the receiver both if they are getting properly.

      remote you have check with your cell phone camera also , IR led is a little visible on camera as little violet light when switch is pressed.

      Delete
  23. No Sir,
    I make both are remote and receiver.I check all connection properly, all are correct but the circuit is not working.I use the micro controller PIC12f615 for remote and PIC12f675 for receiver.I have a doubt in this micro controller,This micro controller with programmed or without program.Please clarify this problem.

    ReplyDelete
  24. Sir,
    I check my remote circuit that all connection is correct but when I pressed any switch the IR led is not emitting check in mobile phone camera what is the problem i cant understand.

    ReplyDelete
  25. One of the three programs in source code can i use in pic12f615 or no ? and can i change this pic in another type of pic

    ReplyDelete
  26. sir i m new to this circuit world.... kindly tell me how to program ic for this circuit... plllzzz... can i use ic widout programing???... my id is sannanqadri@gmail.com

    ReplyDelete
  27. sir how can i program this microcontroller?..My id is armangml946@gmail.com

    ReplyDelete
  28. This comment has been removed by a blog administrator.

    ReplyDelete
  29. What is the Range of This in Meters?

    ReplyDelete
  30. Dear Gaurav Chaudhary
    I tried this circuit and I achive my goals, and the range of this remote about 1.5 metre,
    For my project i must be this circuit with serial communication , I make a circuit fro receiption wave (NEC )
    and sent the code for the wave so if anyone helps me how can I programmer this pic with serial communication.
    Sincerely,
    Wassim Khalife
    wassim.khaifeh.91@hotmail.com

    ReplyDelete
  31. Dear Mr. Gaurav,
    Thanks for this nice project and it is absolutely working fine.
    Instead of IR modulation I like to send these NEC codes through RF.
    For this, I understand that plain HEX code,w/o modulation, is required to come out of the
    Tx PIC 12F615 and so, no IR modulation frequency, 38KHz generation is required.
    I do not know whether it is possible that by disabling some instructions in your program, generation of 38KHz will be stopped & plain Hex codes as per the switch, comes out of Tx PIC.
    If possible kindly show me, if not I like to have your advice.
    Kindly note that I am totally new to programs & want to transmit NEC protocol only as I have learn it from your write-up.
    Regrds,

    D. Ghosh

    ReplyDelete
  32. Hello Sir, First of all i would like to say thanks to you for sharing such a great work with us, your all post are amazing, i hope in future we expect some more interesting project here, i have tried many of your project they work fine but facing some issue with remote projects like "NEC Protocol IR (Infrared) Remote Control With a Microcontroller" & 12 Channel Relay + 2 Channel Dimmer control" i think there is some issue with ir signal because "12 Channel Relay + 2 Channel Dimmer" work fine with key kindly suggest me how can i change remote codes in project to make it work with remote, i am using cheap nec remote(well Known carMP 3), please guide me, Thanks

    ReplyDelete
  33. can you please give me the program to dump on pic16f877a for controlling single led using ir remote using nec protocol

    ReplyDelete
    Replies
    1. please look at the post blow for PIC16F877A receiver, it has IR ,many relay output and some other functionality , if you want you can remove those functions in code ,if not then just use only one output you want and ignore reset of them,

      http://www.circuitvalley.com/2015/01/remote-ac-fan-dimmer-microcontroller-relay-control.html

      Delete
    2. Good work by Gaurav Chaudhary...

      Delete
  34. I loaded same code and SEND sendFrame(0x80, 0x3B)(10000000 00111010) and ON Receiver side i got 01DC (00000001 11011100)
    why this was Happend?? Please suggest Me

    ReplyDelete
    Replies
    1. YOU GOT LSB FIRST MSB LAST , THATS WHY YOUR LSB BECAME MSB

      Delete
  35. Sir how to extend 4 channel into 8 channel can u please help me Mr.gaurav

    ReplyDelete
  36. For nec protocol detection which ir receiver i should use? TSOP1738 will do? I am implementing similar proejct using FPGA? Also can you please tell me the algorithm of the program?How did you detect each bit and after detecting how you proceded further for detecting command bits?
    Pls rply ASAP!!
    Thanks in advance!

    ReplyDelete
    Replies
    1. Type of detector you need to use depends on your transmitter modulation frequency, I use TSOP1738 because my transmitter modulate at 38kHz, you can use any one you like, but modulation frequency will change

      I habe already implemented NEC decoder on xilinx fpga using verlilog find more details in this link
      http://www.circuitvalley.com/2015/10/xilinx-FPGA-board-NEC-IR-receiver-decoder-VerilogHDL-VHDL-wireframe-spartan.html?m=1

      Delete
    2. Thanks a lot!
      Can you tell the applications of this project? Where can i use this detection of NEC protocol bits concept in any major project?
      I need some ideas for college major project.Can you pls suggest some new and innovative idea for final year project?
      Kindly help me out!
      thanks in advance!
      (stream-electronics)

      Delete
  37. Thanks a lot!!!! =)
    Can you tell me some major applications of this project? Like after decoding of this nec protocol where can i use this further?
    Also do you have any new ideas of projects based on FPGA which are not yet implemented anywhere.I need unique ideas for my college major project.Kindly help me out.
    Thanks in Advance!!!

    ReplyDelete
  38. Hello sir pls
    any one can give me header file of pic remote control
    E mail-- Harjeet.bhati66@ gmail.com

    ReplyDelete
  39. Sir if i want to connect a relay to GPIO 5 and toggle it , kindly let know how to modify the receiver code.
    i tried removing the lines LED =1 and LED =0 and also instead of LED = GPIO5 i made RELAY5 =GPIO5, but not working. please guide

    ReplyDelete
  40. Can I use TSOP28238 instead of TSOP1738. What changes I have to do in software if I want to use TSOP28238 IR remote

    ReplyDelete
  41. Dear Sir
    I found the project very interesting, it is working very well but can we make it faster (the turn off time) at the "momentary on" mode?
    Thanks in advance

    ReplyDelete
  42. hi dear
    i compile this project but these error show:
    please hlpe
    Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\bin\picc.exe" --pass1 "C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c" -q --chip=12F615 -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
    Error [192] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 44.5 undefined identifier "INTCONbits"
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 44.20 struct/union required
    Error [192] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 48.5 undefined identifier "GPIObits"
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 48.19 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 51.22 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 55.25 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 58.22 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 62.25 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 65.22 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 69.25 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 72.22 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 76.25 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 79.22 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 84.15 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 85.17 struct/union required
    Error [192] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 92.1 undefined identifier "CMCON0bits"
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 92.17 struct/union required
    Error [192] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 95.1 undefined identifier "OPTION_REG"
    Error [192] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 109.1 undefined identifier "INTCONbits"
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 109.17 struct/union required
    Error [196] C:\Program Files (x86)\HI-TECH Software\PICC\9.80a\samples\NEC IR Remote_2\NEC IR Remote\main.c; 110.17 struct/union required
    Advisory[1] too many errors (21)

    ********** Build failed! **********

    ReplyDelete
  43. I use MPLAB IDE V8.92 and HI-TECH V9.8 and V9.83 ,but errors are yet

    ReplyDelete
  44. Sir,
    I make both are remote and receiver.I check all connection properly, all are correct but the circuit is not working.I use the micro controller PIC12f615 for remote and PIC12f675 for receiver.I have a doubt in this micro controller,This micro controller with programmed or without program.Please clarify this problem.

    ReplyDelete