Timer 2 on a Leonardo

I've been using Ken Shirriff's IRremote Library for a variety of projects with great success on an Arduino Uno. For some reason I can't get the library to work on an Arduino Leonardo.

I tried downloading the datasheet for the ATMega32u4 chip and it's way over my head. The one thing I did notice is that it talks about Timer 0, Timer 1, and Timer 3. No mention of Timer 2 which the library uses for both your PWM output to the IR LED and for timing for interrupts when sampling when receiving.

I've also got the additional problem that I need to use I2C/TWI to drive an LCD display. But on the Leonardo it requires that you use digital pins 2 and 3 instead of analog pins 4 and 5. That means I cannot use digital pin 3 for the IR receiver.

I'm wondering if perhaps it uses Timer 2 for controlling the USB port or serial communications through the USB. One of the problems I'm having is every time I try to run a sketch using your library (for example IRrecvDump) it not only doesn't do anything, it seems to lock up the board because I cannot upload another sketch without doing a hard reset. I'm thinking that when you try to configure Timer 2 in the library it somehow screwing up USB communication.

Can anyone advise is there a Timer 2 on the Leonardo/32u4? Is this the reason it's not working?

Assuming I wade through the datasheet and try to figure all this out… With someone recommend I would have more success using Timer 1 or Timer 3?

I'm not asking for anyone to rewrite the code for me. Just point me in the right direction or give me whatever advice you can.

I don't think the 32u4 has a timer2.

Make sure you download the latest version.

For the Leonardo, it will use Timer4 and pin 13.

Since I posted this original message several months ago I discovered that the latest version of the IRemote library has been updated to include support for other types of Arduino and the various timers which they do or do not support. They do not specifically support Leonardo but they do support Teensy 2.0 which uses the same chip. I added the following code in place of the teensy code and it works just fine with Leonardo.

// Teensy 2.0 versus Leonardo
// These boards use the same chip but the pinouts are different.
#elif defined(__AVR_ATmega32U4__)
  // it's Teensy 2.0
  //#define IR_USE_TIMER1   // tx = pin 14
  //#define IR_USE_TIMER3   // tx = pin 9
  #define IR_USE_TIMER4_HS  // tx = pin 10
  // it's probably Leonardo
  #define IR_USE_TIMER1   // tx = pin 9
  //#define IR_USE_TIMER3   // tx = pin 5
  //#define IR_USE_TIMER4_HS  // tx = pin 13

Thanks cyborg5! That might be valuable for a lot of people...

I have added support for denon devices to the library and was thinking to contact Ken to put it in the official code. If you don't mind, I will send him your changes as well...

The lines…

//it's a teensy 
//is probably Leonardo

was a structure I found on an online forum somewhere. I don't remember where. I do not have a teensy so I can't verify that it really works. All I can say is that it works properly on my Leonardo. As I mentioned in my original message it's really just a case of pin numbering differences. Both boards have the same chip in the same timers so all you're really doing is changing the comments telling the user which pin to hook up.

With that disclaimer, feel free to submit this patch.

I've done a complete rewrite of Ken's code to make it easier to add new protocols and to make it more object-oriented that I intend to release to the public at some point. I really want to get it better documented before I put it out there. It's not going to be what one would call code compatible with his version so I can't just release it as a set of patches to his code. I fully acknowledge his contribution as the basis of it. But it's pretty much a whole new package that I will put on github as a separate package someday soon.

One problem is I bought a raspberry pi and that's keeping me busy for a while. What we really need is a similar library for Raspberry. I'll put that on my "things to do list" right behind cure cancer, create world peace, and invent cold fusion :slight_smile: