EEVblog Electronics Community Forum

EEVblog => EEVblog Specific => Topic started by: EEVblog on August 11, 2013, 09:44:17 pm

Title: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: EEVblog on August 11, 2013, 09:44:17 pm
How to capture and reverse engineer an infrared IR code and use an Arduino or other microcontroller to replay the command.
Oscilloscope and logic analyser capture, coding, troubleshooting, tounge angle, it's all here.
In this instance Dave capture the NEC (Japanese) code from his Canon video camera remote control on the digital oscilloscope, figures out all the bits and encoding, and writes an Arduino library to replay the code back, and verifies it with his Saleae Logic logic analyser.

CODE: http://gist.github.com/EEVblog/6206934 (http://gist.github.com/EEVblog/6206934)

! Private video (https://www.youtube.com/watch?v=BUvFGTxZBG8#)
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: HardBoot on August 11, 2013, 11:15:43 pm
Hmm it's simpler than I thought it'd be, I'll have to try that.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: JoannaK on August 11, 2013, 11:20:43 pm
IR communication (Irda and remote control) protocols are indeed quite easy. One of the messy detail is the lack of standards among manufacturers so one may need send 10's of different codes just to turn various televisions off.

Dave: For timing, IMHO it would be more reliable and elegant to use In-chip PWM for making the 38Khz carrier frequency. I'm not sure how many lines of code it's needed for setting it up, but it should not be too hard.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: EEVblog on August 11, 2013, 11:25:42 pm
Dave: For timing, IMHO it would be more reliable and elegant to use In-chip PWM for making the 38Khz carrier frequency. I'm not sure how many lines of code it's needed for setting it up, but it should not be too hard.

Then the code isn't as portable, nor usable in chips that don't have PWM, nor as easily understandable for beginners.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: notsob on August 12, 2013, 12:43:10 am
Just to add a bit of info for those interested, here's a quick pointer  on RC5 and NEC data formats

http://www.vishay.com/docs/80071/dataform.pdf (http://www.vishay.com/docs/80071/dataform.pdf)
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: crisr on August 12, 2013, 12:42:00 pm
Now, that's a better approach!  :-+

Just one note: I know the code is an example, but for time-sensitive stuff, such as 38KHz carrier, and even the bits themselves, I always try to use timer interrupts, since in C it's a little bit hard to know exactly how long an instruction / function call will take, and you generally have to account for loop overhead as well. Another advantage (or the main one) of using interrupts is that your main code can continue doing other things while the ISR takes care of IR transmission.

Here's the best resource on IR protocol info: http://www.sbprojects.com/knowledge/ir/ (http://www.sbprojects.com/knowledge/ir/) -- check the specific protocols on the left-hand menu.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: EEVblog on August 12, 2013, 12:54:48 pm
Just one note: I know the code is an example, but for time-sensitive stuff, such as 38KHz carrier, and even the bits themselves, I always try to use timer interrupts

Hardware PWM would be better.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: casper.bang on August 12, 2013, 01:32:20 pm
Nice practical demonstration of DS20XX in action. I wonder how much data you are actually capable of capturing with this 14Mpts beast?! This is the sort of thing that has me looking beyond the entry level scopes.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: firewalker on August 12, 2013, 02:22:22 pm
Two nice Ir AVR projects.

IRMP (http://www.mikrocontroller.net/articles/IRMP).
IRSND (http://www.mikrocontroller.net/articles/IRMP#IRSND_-_Infrarot-Multiprotokoll-Encoder).

Alexander.
Title: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: ddavidebor on August 12, 2013, 02:55:35 pm
There is also a tweet library but it need wifi shield
Title: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: lgbeno on August 12, 2013, 06:02:35 pm
Dave, why didn't you just open up the remote and hack the button to simulate a real button press...  Maybe use an opto :)

Sorry, couldn't resist.  Please do not take this post seriously...

Great video!




Sent from my iPhone using Tapatalk 2
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: crisr on August 12, 2013, 07:18:48 pm
Hardware PWM would be better.

Yes, but as you said yourself, not every chip has hardware PWM, such as PIC 12F series which I normally use for such applications. But AFAIK, most common MCUs these days do have interrupts (PIC10F series being an exception; don't know about AVRs as I am not familiar with them). Code portability might still be an issue, but I think the concept would be fairly similar even between different architectures.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: casper.bang on August 12, 2013, 07:59:44 pm
Quote
PIC10F series being an exception; don't know about AVRs as I am not familiar with them

Even the tiniest AVR's have hardware PWM's as well as interupts.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: jnissen on August 12, 2013, 09:08:56 pm
Bravo! You did it right this time!  ;)
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: squeakbat on August 12, 2013, 10:54:52 pm
For the scoped challenged (like me), a PC microphone jack works great for reverse engineering IR protocol.  It even has power (5V through a 200-ohm resister, I think) to drive the IR transistor.  To see the signal, use any audio recorder that lets you zoom in on the waveform (like Audacity).

When I did my Arduino-driven IR remote, I used one timer to generate the carrier, which is fed to another timer to generate interrupts at the pulse period.  The Arduino library doesn't support any of this, of course, and the code was super messy in the end.  (Timer 0 is used for timing by the library internally, and repurposing it messes up calls like delay().)  So, Dave's way is great as long as it works.  (My code had to do other things in the mean time so couldn't block sending the IR sequence.)
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: JoannaK on August 13, 2013, 12:32:37 am
Dave: For timing, IMHO it would be more reliable and elegant to use In-chip PWM for making the 38Khz carrier frequency. I'm not sure how many lines of code it's needed for setting it up, but it should not be too hard.

Then the code isn't as portable, nor usable in chips that don't have PWM, nor as easily understandable for beginners.

And apparently it would not be possible to implement with basic Arduino without some 3rd party libhraries. Apparently there are no easy way to set PWM speed for analog outputs, and the alternative (tone command) does not have options for setting the the frequency (38KHz) high enough for carrier.  :palm:

Ah well.. we allays have 555.s ..  >:D
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: IanJ on August 13, 2013, 06:10:10 am
Hi all,

Related.........For anyone wanting to Rx/Tx IR on the Arduino and using multiple protocols (NEC, Sony, RC5, RC6), I used Ken Shirrif's IR code library on one of my own projects.

http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html (http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html)

Contains detailed explanation & usage.

Ian.

Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: westfw on August 13, 2013, 06:26:44 am
Quote
it would not be possible to implement with basic Arduino without some 3rd party libhraries.
Well, you could read the avr datasheet and set the timers up yourself; you don't always have to have a library do something for you...

There are plenty of things that could be changed in the software, and it's tempting to "improve" it.  But instead, I think I'll comment that I thought this was a great example of how capable the arduino environment IS.  An application that is pretty much "on the edge" of the capabilities; you can't assume that library functions take "zero time", but there is plenty of speed available...
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: squeakbat on August 13, 2013, 07:25:09 am
Well, you could read the avr datasheet and set the timers up yourself; you don't always have to have a library do something for you...

There are plenty of things that could be changed in the software, and it's tempting to "improve" it.  But instead, I think I'll comment that I thought this was a great example of how capable the arduino environment IS.  An application that is pretty much "on the edge" of the capabilities; you can't assume that library functions take "zero time", but there is plenty of speed available...

I totally agree that the tutorial is at just the right level.

About programming the hardware directly, that's at little harder than just reading the datasheet because certain things interact badly with the library.  For example, timer 0 is used for internal time keeping, so it can't be reprogrammed to generate the IR carrier (or not easily anyway).  And you wouldn't know this without reading the source.

I'm not at all criticizing the Arduino.  It's not meant for programming the AVR from scratch.  At the same time, the source is freely available and not too hard to read and modify.  So it's a reasonable compromise: fairly easy to use without locking out low-level features.

The same goes for the hardware.  It works out of the box, and is at the same time easily hackable:  schematics are available, and the two-layer board is easy to modify.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: JoannaK on August 13, 2013, 08:27:49 am
Quote
it would not be possible to implement with basic Arduino without some 3rd party libhraries.
Well, you could read the avr datasheet and set the timers up yourself; you don't always have to have a library do something for you...

I have never used Arduinos myself (started coding on C64 era, with HC11 were first embedded), so it's not (yet) 100% clear to me how much the user is allowed to mess outside of the official sandbox.

Quote
There are plenty of things that could be changed in the software, and it's tempting to "improve" it.  But instead, I think I'll comment that I thought this was a great example of how capable the arduino environment IS.  An application that is pretty much "on the edge" of the capabilities; you can't assume that library functions take "zero time", but there is plenty of speed available...

Ok, so it's hackable for those who can and want to. And it's really difficult to blame a platform that 10€ price point.

Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: sagdahl on August 13, 2013, 03:38:13 pm
Brilliant video again.
You are the champ!
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: mjkuwp on August 14, 2013, 02:30:25 am
I like the video!

 I made an IR repeater using Arduino except that I set up and used the timers my own way.  The Arduino environment will not stop you doing things your own way; one just has to understand that some functions will break.  I looked at Ken Shirriff's stuff closely but did my own thing mainly for the sake of learning.

I used Timer0 for timing slow functions - such as holding a pin low for 4 seconds to reboot my PC.
I used Timer 2 in PWM mode to create the 38khz carrier frequency

totally disabled Timer1

so delay() probably doesn't work and millis() doesn't work.  I don't like those Arduino functions anyway so I don't mind those breaking.


Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: Stonent on August 14, 2013, 07:39:39 am
Quote
it would not be possible to implement with basic Arduino without some 3rd party libhraries.
Well, you could read the avr datasheet and set the timers up yourself; you don't always have to have a library do something for you...

I have never used Arduinos myself (started coding on C64 era, with HC11 were first embedded), so it's not (yet) 100% clear to me how much the user is allowed to mess outside of the official sandbox.

Quote
There are plenty of things that could be changed in the software, and it's tempting to "improve" it.  But instead, I think I'll comment that I thought this was a great example of how capable the arduino environment IS.  An application that is pretty much "on the edge" of the capabilities; you can't assume that library functions take "zero time", but there is plenty of speed available...

Ok, so it's hackable for those who can and want to. And it's really difficult to blame a platform that 10€ price point.

It's still basically C++ but there are standard things that the Arduino software includes that shield you from having to do a lot of the setup routines. It also assumes you're running the chip at 16MHz so there's no issues there.

Also you can still program the AVR chip using Atmel Studio but you have to make sure your code matches your clock speed otherwise things will not run at the right speed.

I haven't written anything too complex yet but I did successfully convert one Arduino sketch to standard C in Atmel Studio. The first time it ran too slow but I realized I had the clock set wrong, just another 10 seconds and a recompile and everything was great.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: jnissen on August 14, 2013, 06:29:50 pm

I have never used Arduinos myself (started coding on C64 era, with HC11 were first embedded), so it's not (yet) 100% clear to me how much the user is allowed to mess outside of the official sandbox.


I was in the same boat a few years ago and just picked one up. Amazed at how easy the platform was. I was used to old school assembler and getting your hands dirty with code and hardware. I still do that from time to time as needed. The tools support a huge amount of shared knowledge with the various libraries and code examples. My son actually turned me onto the Arduino after I gave him about 1% chance of being successful designing an LCD enabled tachometer. He got his proto working in two days! Made a believer of me!
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: David_AVD on August 14, 2013, 10:28:27 pm
I've used the Arduino boards as a project proof of concept.  I'd probably not use one in a sold product, but they are perfectly fine for R&D.
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: open loop on August 15, 2013, 06:30:43 pm
Excellent video and came at the pefect time.

I was messing about with an IR based project where I wanted to time 100 IR beam breaks. The LEDs arrived in the post but not the photo transistor. So I thought, "I know... I tore down an old sky HD box a while ago for the HDD I'll use that IR receiver". Worked out the connections but it didn't work with anything except the stero remote checked the LED with my phone camera and it worked!

What was happening? - now I know my LED did not generate the required 38kHz carrier! .... As David says "I hope your project doesn't work"!. I have noticed that newer phone cameras now have an IR filter so you can't see the IR LEDs working anymore.




Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: Legit-Design on August 15, 2013, 06:56:27 pm
I have noticed that newer phone cameras now have an IR filter so you can't see the IR LEDs working anymore.
This smells like bullshit.
All digital cameras still have IR cut filter, it just doesn't block IR remote signals too well. I find it more plausible that your project isn't sending IR signals, than they have included ir cut filters that completely cut all IR, even remote signals. What phone are you using?
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: open loop on August 15, 2013, 09:32:45 pm
I do agree with you that digital cameras have an IR and that the average remote would be seen due to it not blocking these particularly well.

I often used my iPhone 3GS to test remotes to see if the problem was with the remote or the appliance. When our cable box played up I tried this trick on my wife's iPhone 4S and my iPad 3 and I could not see the tell tale flashing you normally see so there must be something going on with the newer cameras, why I am not sure...

The reason my project did not work was because I was trying to use a constantly lit IR LED with a IR receiver not a photo transistor. Which Dave's video explained perfectly.



Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: Legit-Design on August 15, 2013, 09:42:35 pm
I do agree with you that digital cameras have an IR and that the average remote would be seen due to it not blocking these particularly well.

I often used my iPhone 3GS to test remotes to see if the problem was with the remote or the appliance. When our cable box played up I tried this trick on my wife's iPhone 4S and my iPad 3 and I could not see the tell tale flashing you normally see so there must be something going on with the newer cameras, why I am not sure...

The reason my project did not work was because I was trying to use a constantly lit IR LED with a IR receiver not a photo transistor. Which Dave's video explained perfectly.

http://www.instructables.com/id/See-Infrared-LED-Light-with-an-iPhone-4/?ALLSTEPS (http://www.instructables.com/id/See-Infrared-LED-Light-with-an-iPhone-4/?ALLSTEPS)
Use the front facepalm camera on your iDevices. I understand you like to use the simple iDevices, but occasionally you have to press a button.  :-/O
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: HeyTom on August 16, 2013, 02:03:44 pm
Hardware PWM would be better.

For hardware PWM, I have a Pic PWM Calculator you might find handy at http://micro.alleypress.org/ (http://micro.alleypress.org/)  It also works in a browser on a smart phone.

It let's you find stuff like


I'm open to any feedback to make it better
-Tom
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: GalvanicResonse on August 18, 2013, 02:15:19 am
Re: IR receiver and phototransistor examination of carrier and stripped carrier :
"Traps For Young Players" file.

Hi - I am new to the blog and forum.
The video was very informative. I don't have the fancy pants equipment but I wanted to view the output of some of the remotes around the house on an analog scope. I scrounged up a photo tranny and an IR receiver from the junk box. This note is just to add to the "TFYP" file on this aspect of the subject.

There are two things that I bumped into while trying this out.

1) IR receivers - I was unaware of what these things do, so the video encouraged me to try one out. The trap here is that the pin-outs are not uniform across manufacturers. I had a few that had been removed from consumer gear but they have no part number to look up. My solution was to remove another one that was still in place on some junk board, but paying attention to the circuit traces as to which ones are the likely suspects ( example, an electro cap across two pins was good evidence that I had Vcc and ground). So my advice to a beginner on this is to pay attention when you remove it and store it away with a bit of documentation.

2) Detecting the carrier. My first circuit for this was a simple grounded emitter ( Vcc - 2k2 - photo-t collector , photo-t emitter grounded). This detected IR from a remote but I didn't see a carrier. I then tried 4 other remotes - same results. The output looked about the same as that from the IR recevier. I thought that I was unlucky and none of my remotes used modulation. But unlucky 5 times ?? I cranked up the volts/div on the scope and could see tiny sawtooth waves on the low going parts of the signal. I started to wonder if the lone photo-t was not fast enough to follow 38 khz. I googled 'phototransistor circuits' and got a few hits that were real app notes. When I built the circuit in the Sharp app note ( figure 11a ) the response was fast enough that I could see ( and measure the period of ) the  carrier (36 k in 5 out of 5) .   That made for a fun evening. Thanks. 

If the URLs don't work, google
 
'Photodiode/Phototransistor Application Circuit' for the Sharp app note and
'application note an-3005 design fundamentals' for the Fairchild one

Fairchild app note

http://www.fairchildsemi.com/an/AN/AN-3005.pdf (http://www.fairchildsemi.com/an/AN/AN-3005.pdf)?

Sharp app note ( this had the solution that I needed )

http://physlab.lums.edu.pk/images/1/10/Photodiode_circuit.pdf (http://physlab.lums.edu.pk/images/1/10/Photodiode_circuit.pdf)

GR
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: westfw on August 18, 2013, 07:45:51 am
Quote
it's not (yet) 100% clear to me how much the user is allowed to mess outside of the official sandbox.
It *is* a microcontroller with no "protection."  "Arduino" is little more than an IDE, some initialization code, and some libraries.  Anything is "allowed"; you can reinitialize all the peripherals, ignore the libraries, and write code in assembler if you want.  (of course, once you start re-programming the peripherals, all bets are off WRT the official libraries continuing to work...)
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: mrwildbob on June 28, 2015, 09:37:42 pm
Hello Everyone,  I'm slowly getting back into programming after watching Dave/EEVblog over the years.  Very inspiring.  I need a little help.  I would like to use this code for a different project but with out the 38KHz carrier frequency.  I've tried editing the code to work with my application but can not seem to get the desired results.  Can someone help me with this?  I just need to send out a 550 byte code out pin 2.  thanks
Title: Re: EEVblog #506 - IR Remote Control Arduino Protocol Tutorial
Post by: ftonello on February 28, 2016, 08:05:55 pm
Doing some tests here....Now i understand what Dave said about capturing these waveforms in a 2.5K oscilloscope (a TBS 1072B-EDU).
Do the job,but with small resollution and losing some carrier freq.

(https://dl.dropboxusercontent.com/u/12504158/ELECTRONICS/TEK0000.JPG)

(https://dl.dropboxusercontent.com/u/12504158/ELECTRONICS/TEK0001.JPG)

Any ideas to improve the signal capturing?

Capturing the data from a Samsung DVD remote controler, with a ZILOG Crimzon  ZLR2802GR54WL controller on board.

(https://dl.dropboxusercontent.com/u/12504158/ELECTRONICS/IMG_0352.JPG)

(https://dl.dropboxusercontent.com/u/12504158/ELECTRONICS/IMG_0353.JPG)

Att