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
remote.c
Header Files
remote.h
rckeys.h
LCD Related
Source Files
lcd.c
Header Files
lcd.h
myutils.h
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)..."
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
RemoteInit();
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 avinash@eXtremeElectronics.co.in
Web: www.eXtremeElectronics.co.in
Please give credit or reference to www.eXtremeElectronics.co.in
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;
RemoteInit();
LCDInit(LS_BLINK);
LCDClear();
LCDWriteString("IR RemoteDecoder");
LCDWriteStringXY(0,1,"Key Code:");
while(1)
{
cmd=GetRemoteCmd(1);
LCDWriteIntXY(10,1,cmd,3);
}
}
See also
- LCD Interfacing with AVRs
- Making a "Hello World" Project.
- Home Made AVR Devboard.
- Making Programmer and Target.
- More ...
Subscribe
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 !!!
Comment
Don't forget to post a comment about how you liked this article or if you have any doubt and need help.

hi/please guide me that can i program a atmega32 with tow or more channel of adc in bascom compiler.thanks alot/best regard
November 16th, 2008 at 10:23 pmHi Hamid,
November 17th, 2008 at 7:58 amI am not a BASCOM user but I prefer C for programming. But I will help you. I will contact you withing few days.
Hi There,
Thankyou for the library! It has been very helpfull in my learning curve! Could you maybe do tutorial for SPI Interface?
December 13th, 2008 at 7:28 pmHi Rhys,
December 14th, 2008 at 6:48 amThanks. Nice to know you liked my tutorials. I will surly write a tutorial on SPI.
Hi avanish,
Thankyou for offering to do a SPI tutorial. First, can i ask, will this IR code run on the ATTINY44?
Thankyou
December 14th, 2008 at 4:25 pmHello 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.
December 14th, 2008 at 6:59 pmDo 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.
May 29th, 2009 at 3:57 pmGreat Job!
July 9th, 2009 at 5:29 pmThanx for the AVR Tutorials.Your tutorials are helping me a lot.Keep writing the Tutorials in future.
Hi Avinash
July 14th, 2009 at 6:47 pmThank 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…?
@Anshul
July 15th, 2009 at 8:42 amUse a DVD Played remote. They are easily available and cheap (Rs 40) and have maximum number of buttons. With nice navigation keys!
Hi Avinash ..Thank u for reply
July 15th, 2009 at 11:15 ami hav Philips DVD player i tried this decoder on its remote. but m not getting the output…
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
July 19th, 2009 at 5:24 pmi have use bpl remote n lg remote also but thr is no decoded info on da lcd ……help pls
July 20th, 2009 at 11:51 amHi! 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 !
Regards,
July 31st, 2009 at 4:24 pmHi Avinash
August 26th, 2009 at 10:04 amThank 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.
hi Avinash.
August 26th, 2009 at 8:06 pmin 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
@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.
August 27th, 2009 at 6:11 amhi Avinash
August 27th, 2009 at 8:01 amthank 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
ISR(INT1_vect)
{
GICR&=(~(1<<INT1)); //Disable INT1
sei();
if(stop) return;
unsigned int TempTime=Time;
Time=0;
TCNT0=0;???}
I used internal 8Mhz & in main function i #define F_CPU 8000000UL LCD working very nicely,plese guide me bcz i realey need it
Hi,
I need to know which remote control should I used with NEC format protocol. If Philips or LG remote control works with this library.
Thnaks
October 25th, 2009 at 11:16 pmcan i have the header file of remote.h
December 19th, 2009 at 4:21 pm@Prasanta
The link is in the above article!
December 19th, 2009 at 5:14 pmDear 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.
February 8th, 2010 at 3:55 pmHi 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
February 20th, 2010 at 4:39 ammurx
Great tutorials. Extremely useful.
March 20th, 2010 at 3:54 amIt’s amazing that you share your knowledge and experience with others.
Good Luck
Thanks Avinash,
March 25th, 2010 at 1:39 pmit 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.
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
March 26th, 2010 at 8:48 amMost 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!
Thanks a lot avinash. Your website is just too good for freshers like me. I am getting a lot of help from ur tutorials.
June 13th, 2010 at 12:42 pm[...] remotely control the speed of an AC fan and to switch it on or off. The remote control is a cheap NEC Format remote, usually supplied with small DVD players. Three buttons are used to command the circuit. The UP key [...]
July 4th, 2010 at 12:41 pmhi 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!
July 11th, 2010 at 12:31 pmI can send you all the necessary info regarding with this. Again Thanks!