Using IR remote with AVR MCUs – Part II

Hello Friends,

Welcome back. In previous tutorial I introduced my IR remote decoding library. In this tutorial I will continue our discussion and show you how to add IR remote controls support to your AVR projects.

Step I

Download the library files and unzip them in a folder.

Step II

In AVR studio create a new AVR-GCC project. Then copy the following files to the project folder.

IR remote related.

Source Files


Header Files



LCD Related

Source Files


Header Files



Note: Include file from the

\lib\ATmega8 if you are using ATmega8

\lib\ATmega16 if you are using ATmega16 or ATmega32

After The files have been copied add them to your project by right clicking project view and selecting “Add Existing Source File(s)…” and then select the “lcd.c”. Similarly add other source files. In the same way add the header files by selecting "Add Existing Header File(s)…"

avr studio

Adding files to projects.


If instead of AVR Studio you use Makefiles then add all the source files names in the source section of the makefile along with your main c file (which has same name as your project and has the main function).

Step III.

In your main C source file include the following files

#include "remote.h"

in addition to standard files

in your main() function initialize Remote Subsystem by calling


And your done with initialization.

Step IV

Once initialized the remote subsystem automatically receives command sent by remote and store them in a circular FIFO(first in first out queue). You can call the function GetRemoteCmd() to read from the buffer. The return type is a unsigned char (8bit) so the value can be from 0-255. This is the keycode of the key pressed on the remote . To know the key code of various keys simply run this example on MCU and press keys the key code will be displayed on the LCD. The GetRemoteCmd takes one argument which can be either 0 or 1. When you pass 1 the function checks the queue for command and if there none then it wait till a key is pressed on the remote control. It returns only if a key is pressed and return its keycode. In contrast to this if you pass 0 the function returns immediately if no key is pressed it returns a special constant RC_NONE which is defined as 255 in the header file. Otherwise it returns the key code of pressed key.

To make every thing more clear I am giving the complete sample program which will just wait for keypress and then display its code.

Sample Code


Demo program for testing the IR Remote control libraries.


Copyright Avinash Gupta 2008


Please give credit or reference to
if you use this in your projects and find it useful.


#include <avr/io.h>

#include "remote.h"
#include "lcd.h"

void main()
   uint8_t cmd=0;




   LCDWriteString("IR RemoteDecoder");
   LCDWriteStringXY(0,1,"Key Code:");


See also


If you are an AVR MCU fan, subscribe to my feed powered by Feedburner® and get all tutorials, codes and more delivered to you Inbox !!!


Don’t forget to post a comment about how you liked this article or if you have any doubt and need help.

JLCPCB Prototype: $2 for 10 pcs PCBs, 48 Hours Quick Turn

China’s Largest PCB Prototype Enterprise, 300,000+ Customers & 10,000+ Online Orders Per Day

Get quote online easily: quote

Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.


Avinash Gupta is solely focused on free and high quality tutorial to make learning embedded system fun !

More Posts - Website

Follow Me:
FacebookLinkedInGoogle Plus

Hobby Robotics Wheels

106 thoughts on “Using IR remote with AVR MCUs – Part II

  • By hamid - Reply

    hi/please guide me that can i program a atmega32 with tow or more channel of adc in bascom compiler.thanks alot/best regard

  • By Avinash - Reply

    Hi Hamid,
    I am not a BASCOM user but I prefer C for programming. But I will help you. I will contact you withing few days.

  • By rhys - Reply

    Hi There,

    Thankyou for the library! It has been very helpfull in my learning curve! Could you maybe do tutorial for SPI Interface?

  • By Avinash - Reply

    Hi Rhys,
    Thanks. Nice to know you liked my tutorials. I will surly write a tutorial on SPI.

  • By Rhys - Reply

    Hi avanish,

    Thankyou for offering to do a SPI tutorial. First, can i ask, will this IR code run on the ATTINY44?


  • By Avinash - Reply

    Hello Rhys,

    First test it on ATmega8/16/32 because I am not sute what remote control protocol are more popular in your country. As I said there are two common format, Sony RC5 and NEC format. My Lib works for NEC as these are popular in India.

    If you get it working then you can port it to ATTINY44 but check
    1) Does the TIMER0 has compare unit ?

    May be you have to change the NAME of some registers and ISRs to get it running.

    I have not worked it ATTINY44 so pls Consult its database.

    When you are about to port it do contact me and I will help.

  • By Mike - Reply

    Do you know of any problems if i used a RPM7138 IR sensor instead of a TSOP1738 ? I read that they are the same but it doesn’t seem to work for me.

  • By Akshay - Reply

    Great Job!
    Thanx for the AVR Tutorials.Your tutorials are helping me a lot.Keep writing the Tutorials in future.

  • By Anshul - Reply

    Hi Avinash
    Thank you very much for this great tutorial.
    But i have a problem .. which TV ir remote(sony,samsung etc) should i use for this decoder…?

    • By Avinash - Reply

      Use a DVD Played remote. They are easily available and cheap (Rs 40) and have maximum number of buttons. With nice navigation keys!

      • By jayakrishna -

        please tell the remote model,i have receiver but not remote

  • By Anshul - Reply

    Hi Avinash ..Thank u for reply
    i hav Philips DVD player i tried this decoder on its remote. but m not getting the output…

  • By raghu - Reply

    thnks for da lib but whn i use dem in my project i am geting an some errors could u please check dem out once more…..da errors ar occurring only whn i change da project setings from atmega32 to atmega16

  • By raghu - Reply

    i have use bpl remote n lg remote also but thr is no decoded info on da lcd ……help pls

  • By Zagg - Reply

    Hi! Many thanks for your great website !
    Its possible to use a TSOP1136 (36KHz) in replacement of the TSOP1738 (38KHz).
    I think the only diff is 36Khz/38KHz carrier.
    What I need to modify in the library for that works please ?
    Thanks in advance !


    • By Dheeraj - Reply

      Someone please answer the question

  • By sagar - Reply

    Hi Avinash
    Thank you very much for this great tutorial.but i have tested ur libarary on Atmega8 with NEC format remote,it doesent work plese plese check it again bz i realey need it
    thank you.

  • By sagar - Reply

    hi Avinash.
    in your responce no 48 ur say Crystal used is 12Mhz or 16Mhz Only but in your tutorial on IR remote interfacing with atmega8 lib it is compatable with 8/12/16Mhz,plez guide me which crystal frq.did i prefer

    • By Avinash - Reply

      @Sagar use any crystal (12MhZ OR 16 mhZ)

      V E R Y I M P O RT A N T
      The chip must be configured to use the external crystal, other wise it is useless to attach external crystal.
      Fuse must be set as follows to enable that
      LOW FUSE = FF (Hex) HIGH FUSE =C9 (hex)

      Also in AVR Studio or Makefile correct frequency of crystal used must be set.

  • By sagar - Reply

    hi Avinash
    thank you for very much for ur replay; But in ur “IR remote decoding library”the Atmega8 is compatable with 8Mhz and it uses 16-Bit Timer Timer1 only(if im correct).
    but in
    GICR&=(~(1<<INT1)); //Disable INT1
    if(stop) return;
    unsigned int TempTime=Time;
    I used internal 8Mhz & in main function i #define F_CPU 8000000UL LCD working very nicely,plese guide me bcz i realey need it

  • By S.M.Abdullah - Reply


    I need to know which remote control should I used with NEC format protocol. If Philips or LG remote control works with this library.


  • By prasanta - Reply

    can i have the header file of remote.h

    • By Avinash - Reply


      The link is in the above article!

  • By kunal - Reply

    Dear avinash,
    I’ve made hardware as per ckt dia. but its notworking.
    I’ve one doubt, carrier for Philips RC5 is 36Khz.
    can above ckt will work on TOSP 1738 or it will work on TOSP 1736. Pls guide.

    Thanks in advance.

  • By murx - Reply

    Hi Avinash

    Thanks for your effort on this stuff!

    I tried to use your lib on a ATmega16 with 16MHz XTal, but the content of GetRemoteCmd(0) is always “06” no matter if i press any button or not.

    -The “hardware” works fine (i checked it with a simple code)
    -The Fusebits are set how you recommend it (FF/C9)

    Do you have some hint what could be wrong?
    I use WinAVR GCC

    Thanks in advance and Greets from Switzerland

  • By midOSmid - Reply

    Great tutorials. Extremely useful.
    It’s amazing that you share your knowledge and experience with others.
    Good Luck

  • By kunal - Reply

    Thanks Avinash,
    it works.
    i used TOSP 1730 and TOSP 1738 it works on both.
    i used remote control BPL RC604 (it is available in any remote shop for Rs. 50).
    Thanks again.

    • By vishnu - Reply

      kunal as you said we bought a BPL RC604 remote .do we need to change the key commands in rickeys.h file?if so can you please tell what all changes do we need to make

      • By shubham jain -

        thanx for sharing
        bpl rc604 is working fine

    • By Suhasini - Reply

      Hello, I’m using TSOP1738 and RC604 only. I have connected the pins of TSOP to ground, Vcc and output. The program gives output as “Key code:220” straightaway as soon as the program starts to run. I’ve set fuse bits as C9 FF….Please help me about what might have gone wrong??

      • By Avinash -

        Please post the image of your setup in the forum, along with the MCU being used, Crystal frequency and connection of the LCD with MCU.

  • By KK - Reply

    Hi, Avinash,

    I am using ATMEGA16 as my 1st atmega MCU.
    I am trying to write prg to control DVD, TV, AVPC all together in a single remote unit. Before that I should decode the IR first, but I fail as I searched the web which 2 methods are good for that. One is ICP and the other is just a timer.

    My fosc is 11.0592MHz without clock divde and fclk for ICP is 11.0592M/256=43.2KHz(23us). The capture mode is falling edge in ICP1 of timer1. I worked on CodevisionAVR, I think it is not related to platform. My timer0 is worked in 11.0592MHz/64=172.8KHz. Will interrupt priority caused problem????

    I just think the time is fast enough to dectect :

    NEC format
    Most remotes here are NEC format. I tried two days. The result is disappointed. Therefore, can u help me a bit with sending me your successful code for further evaluation. Pls help…..Thank you!

  • By rajkumar - Reply

    Thanks a lot avinash. Your website is just too good for freshers like me. I am getting a lot of help from ur tutorials.

  • Pingback: AVR Project - Remote Controlled Fan Regulator. | eXtreme Electronics

  • By Michelle - Reply

    hi Avinash,

    I am using a c program in the microcontroller, but i have a problem regarding about how to add an interrupts to my program. It is about this that when i will send a signal from a remote contol it will bypass all the running programs and make priority to the signal fom the remote control.. Please help me with this one… thanks!
    I can send you all the necessary info regarding with this. Again Thanks!

  • By Nirmalya - Reply

    Hi Avinash,

    Thanks a ton for the tutorial. For all of them actually. Works like a charm!
    Really appreciate you effort in supporting novices like me.

    • By Avinash - Reply


      My pleasure! 😉

    • By Sony - Reply

      which sensor did you use ?? TSOP1738 only ??

  • By sandeep - Reply

    THANKS a ton….for such an awsome tutorial…it works well…
    bestest site to learn AVR STUFFS.. 🙂

    • By vishnu - Reply

      hai sandeep can you please tell the remote you just started with the projects.what all problems did you did you solved it.can you tell me what to do once the library functions worked well?what is the next step.this is 1st project with a at ground zero.can you share your knowledge please

  • By Abraham - Reply

    Thanq so much….the code works superbly….thanx once again….:)

  • By Abraham - Reply

    Just for the information of everyone, I used a BPL remote version RC1088N….and Car mp3 remote….

    @Avinash: I forgot to mention I had to change the code at 3 places in the code remote.c. All were changing “char” to “int”. I made change for Qfront,Qend and i (occurs in ResetIR() and RemoteInit() )….thanx once again for your help…:)

    • By vishnu - Reply

      hai abhraham can you mail me the modified code please..can you mail me at

    • By hemghosh04 - Reply


      I tried this library but dont no why its not working. I have followed all the steps and instructions Stated here but still its not working 🙁
      I burned the corresponding .hex it works fine but when i create a new project using this lib they are not showing any values for any pressed button. But it is always print a garbage value like 203,213,169… etc.

      IR RemoteDecoder
      Key Code: 203 or 127 etc..

      The value is not changing with any button presses.

      Can you please help mail id

      • By Aritra Das -

        hello, I am having a similar problem. Did you find a solution?

      • By Grzegorz -


        Do you have solution for that?

        I have the same problem and no idea what is wrong…
        Best regards

  • By Dinesh - Reply

    Hi Avinash,
    Thank you for this wonderful tutorial. I would like to use the internal 1MHz default oscillator. Is it possible to use this or should I have to use the external 8/12/16 MHz compulsorily?
    Thank you

  • By Halfgaar - Reply


    I’m trying to re-implement your lib to learn and to suit my slightly different needs. Anyway, I found a few things that may be helpful.

    I think there is a bug in the Atmega8 driver, and perhaps others:

    In the INT1 interrupt handler, you set TCNT0=0, but you’re using timer1. You should set TCNT1=0, right? Plus, TCNT[1|0] are 16 bit registers, which need special care to write to (interrupts need to be disabled while you do it).

    Then, about the clockspeeds, why don’t you just do:

    #define us_to_timer_compare_match(time) ((F_CPU/1000000)*time)

    Then set OCR1A=us_to_timer_compare_match(TIMER_INTERRUPT_AFTER_US)

    I do and it works great.

    • By Avinash - Reply


      I wrote the lib many years back when I was new in this field. So their may be many faults (but NOT bugs).

      I still use the lib in many projects but never required any modification to the actual lib.

  • By Kasamiko - Reply


    Where can I find the schematic for the code above?


  • By Kasamiko - Reply

    Hi Avinash,

    I finally got it working…thank you so much for posting such a great tutorial…

    BTW I want to change the display code to HEX, any idea to convert it to HEX before being displayed on LCD?


    • By vishnu - Reply

      hai kasamiko can you please tell the remote you just started with the projects.what all problems did you did you solved it.

  • By Avinash - Reply


    “BTW I want to change the display code to HEX, any idea to convert it to HEX before being displayed on LCD?”

    Pal those things are taught in 6th standard!

  • By Kasamiko - Reply

    I’m sorry but what is “6th standard”?

    • By Avinash - Reply

      6th year of schooling.

  • By Kasamiko - Reply

    Sorry but it was never taught in medicine school…

  • By korytnacka7 - Reply

    Thank you for the library! It works.

    • By Avinash - Reply


      Thank you !

  • By korytnacka7 - Reply
    With another remote control.

  • By jitu - Reply

    i want to show u my error
    ../rm.c:4:20: error: remote.h: No such file or directory
    ../rm.c:7: warning: return type of ‘main’ is not ‘int’
    ../rm.c: In function ‘main’:
    ../rm.c:11: warning: implicit declaration of function ‘RemoteInit’
    ../rm.c:17: warning: implicit declaration of function ‘GetRemoteCmd’
    ../rm.c: At top level:

    • By shubham jain - Reply

      just mail it to me i will make it error free for u

      • By anoop -

        Please provide me the code for atmega 8.

        thanks in advance

  • By Suman - Reply


    I tried this library but dont no why its not working. I have followed all the steps and instructions you gave, but still its not working 🙁
    I burned the corresponding .hex files within the library in both Atmega32 and Atmega8, but they are not showing any values for any pressed button(even not showing 000). But when I made a new project using the library files it is always giving a garbage value like 203,213,169… etc.
    Please Help…

  • By Suman - Reply


    I tried this library, but dont know why its not working. I burned the corresponding .hex files on both Atmega8 and Atmega32 with correct fusebits and used a nec protocol remote, but it is just showing this

    IR RemoteDecoder
    Key Code:

    Nothing is happening when any button is pressed.
    So, I created a new project and included all the necessary files as instructed. Now its giving up strange values like 203,213,169…etc and showing this

    IR RemoteDecoder
    Key Code: 203

    The value is not changing with any button presses.
    Please Help…

    • By shubham jain - Reply

      suman ji
      which remote did u use?

  • By Suman - Reply


    At last it worked…!!!
    The project file within the demo folder worked, but the files within the lib folder not working in AVR Studio 4.


  • By harshit - Reply

    plz tell me how to decode NEC remote stepwise plz help

  • By korytnacka7 - Reply

    Hello from Czech Republic. Now I have a big remote control from a PC TV tuner. It works fine.

  • By Avinash - Reply


    Thank you very much 🙂

  • By smriti - Reply

    Sir i set fuse bits
    Lower 9f
    Higher c0

    But after setting fuse bits i tried to flash a program nd nw winavr is showing error microcontroller is not responding

    can you tell me which fuse bit i have
    set wrong?
    your site is really a superb learning source for begineers as well as experts

    great work
    lots of wishes

  • By Vladimir - Reply

    How it works ?

    If I call GetRemoteCmd(1) then program will stop here until key is pressed ?


    GetRemoteCmd(1) returns value every time it is called ?

    • By shubham - Reply

      bro getremotecmd is in while loop
      so it will be re executed again and agaim

  • By Darex - Reply

    Hi .What is TOL in the program code ?

    • By ronak - Reply

      Yeah…even i cannot figure out what “TOL” is in the code !! guide please..

      • By ronak -

        oh…sorry…missed the header file

        @Darex..please check out the remote.h file..u’ll connect the missing links !

  • By summerNight - Reply

    Hi Avinash,

    I am using ATMEGA 8515-16PU. The INT0 port is not PIN16 here. Its on PD2 (pin12). I followed your article and the LCD seems to work but I get 255 as the pressed button value all the time. Are my INT0 ports ok ?

  • By hari - Reply

    do you have this library for codevision? i would love to try it.

    thanks and regards

  • By Hari Hendaryanto - Reply

    i’ve finally end up compiled it using avr studio, with some modification, add another interupt and timer for other task, compiled cleanly and tested ok.

    • By Avinash - Reply


      Good !

  • By Gangster - Reply

    Complied code and tested the libraries are working perfectly.!
    Thanks Avinash great work 🙂

    Well i am getting the command thats fine…
    can i get the address using the same lib ?

  • By Ani - Reply

    Hi Avinash,

    First and foremost, thanks so much for putting this up.

    When I connect the circuit, I get 186 as the code number, and it stays like that only.

    I am yet to try with BPL remote mentioned above, but I tried with around 8 different remotes. The value did not change.

    I have used my own LCD Libraries, and am using only remote related libraries from your project.

    Please let me know if I am missing something.

    Best Regards,

  • By Amey - Reply

    Very good tutorial, thanks for such valuable information.

    I have two queries though:
    1) Unable to locate the remote.h library for Atmega16 , Please provide exact link to the Atmega 16 library.

    2) Is there any change in INT0 pin where TSOP data pin is connected ? I had found INT0 in atmega8 of yours.


  • By surendra - Reply

    sir this is nec or rc5…….

    • By Avinash - Reply

      @Surendra, Please read the article bit more carefully !

  • By Chirodip - Reply

    cn i get some lil-more help on dis project??
    i used atmega8 with 12MHz Xtal, 16×2 lcd connected as ur tutorial – 1.
    TSOP – 1738 used for receiving the IR signal, connected to pin5 with 10uF,63v (cap) proper closed. Transmitting the IR signal with InTEX DVD remote & also used car Audio-remote (should be NEC protocol).
    but still i cnt get the output.
    help me on the internal oscillator settings & Fuse-bit settings and also with ur valuable suggestion..
    Thank you,

  • By Asma - Reply

    which version are you using? avr studio4?

  • By satish - Reply

    Hey my previous problem of lcd initialisation was solved.
    Now the code works fine but everytime i start the circuit a constant value 113 is printed on the lcd.
    Pressing any remote key does not have any effect.
    Pls suggest something

  • By kartik - Reply

    heloo sir
    it give some error like “unsupported cpu frequency” ,” TIMER_COMP_VAL’ undeclared (first use in this function)” and ” IR remote Lib : Pls use F_CPU = 8MHz,12Mhz or 16MHz”
    please help

    • By Avinash - Reply

      @Kartik Sorry dear I have no time to help you!

  • By Manoj - Reply

    I want to use this infrared library with ATMEGA328 chip and ATMEGA32U4 chip. What changes are needed in the library? Can you make this library universal?

  • By D.GHOSH - Reply

    can you help me by providing a simple fan regulator code & cct using 12F675 PIC for both TX & RX without 38 KHz modulation because I want to couple it with 433 MHz TX RX module.
    For TX part only ON/OFF and SPEED UP & DOWN in steeps, 7/8 steeps are ok, that means only 3/4 input buttons are sufficient.


  • By Shankar - Reply

    Hi Avinash!
    Thanks for the great article!

    Can you tell me, if there is any way to note the values from the remote, instead of using the lcd module

    Thanks in advance 🙂

    • By Darius - Reply

      Of course there are way. You only need to find your own way 😀 In your situation i would send tada to PC with UART. Anyway, you can connect lot of LED’s and write whole sequence to it 😀

  • By sumit - Reply

    hey i m using usart to debug the code and get the values, i can get the values if i transmit every time the 0’s and 1’s of the command , but if i try to transmit whole command at once i get garbage value..1!@# some ascii characters. i have also converted the value in digits as per the LCDWriteXY(). kindly help to decode the correct command.

    • By Darius - Reply

      I have written code, with atmega, IR sensor, IR LED, LCD and bluetooth module. When you send command “1” via bluetooth terminal from phone to atmega, it shows on LCD “waiting for IR signal” , then you press remote control button, and LCD write that code into the memory and saves it. When you send “2”, then IR sensor do not catch IR signals anymore. And if you send command “3” , atmega repeats the IR signal from remote control.

  • By sumit - Reply

    @avinash: ur library is not working for 8mhz or 12 mhz…i think it operates only on 16mhz..coz u have kept a 10us interrupt..and 8 or 12 mhz clocks are unable to run the code outside timer interrupts.. i have simulated on proteus

  • By SOM NATH ROY - Reply

    First time I seen your Project for Remote controlled Fan Regulator Using ATmega8 & should say great job.

    I want to make similar one but want to add some tactile switches to control manually fan speed and OFF/ON control in addition to remote control.

    Can you suggest where to connect those tactile switches and what improvement required in the coding ?

    Looking for your help.


  • By Pipaliya Kevin - Reply

    Thanks so much Avinash
    your work is awesome…

    Keep it up..
    It is the very much helpful for me as I’m beginner..

  • By Drawin Kumar - Reply

    Hi Avinash,
    Thanks for the excellent tutorials and friendly website.
    I am trying to get the IR decoder work with ATMEGA16L@12MHz. I do not have an LCD module so debugging via USART. I have tried many remotes from cheap Rs 30 to LG and Airtel but none of them works. I can get INT0 interrupts so the frequency is definitely 38KHz but not sure about the protocol without an oscilloscope.

    BTW in the remote.c you have mentioned connection as follows:
    | _ |
    | | | |
    | | | |
    | | |
    | | |
    | | |
    (5V) (To PIN16)

    *TSOP 1738 Front View*

    But in datasheet 5V and GND are reversed.
    Can you please check.


  • By - Reply

    Dear Avinash,
    I’m tryimg to port you IR-lib to ATMEGA328 !
    It’s realy actual as it is arduino nano board – most popular avr-develop-board. More then 2 days I can’t make it work. Please help whith setup registers of timer and interrupts.

    The same:
    By Manoj – September 22, 2014 8:04

  • By pinkey - Reply

    hii sir…
    i’m using ATMEGA 8A CONTROLLER so i need a sample code for ir remote controller plz help me sir

  • By disha - Reply

    When we download hex file in Atmega8 given on site for remote testing LCD display nothing only green light we moves preset both direction also please help

  • By Shahid Burki - Reply

    hi Avinash
    I was very intrested in making remote control and I used your liberary. It is working very nicely and un expectedly it worked at very first time. Mostly remote control teases alot but as I said that unexpectedly it worked at first attempt, I am using it with Atmega 32, internal RC oscilator with 8 Mhz in AVR studio. it is working fine but some time I have to press a key multiple times.
    Any way thanks for the excellent work.

  • By alaa mansour - Reply

    please i need your help in something like that
    im working on a project on AVR atmega 16 i need to connect ir receiver to the AVR so i can control over output pins to do more than one function
    can i get some help please

Leave a Reply to satish Cancel reply

Your email address will not be published. Required fields are marked *

two + = 5

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>