As the pin does not appear to be able to be defined when creating an object from the IRsend
class, it seems as if you will have to modify the IRremote library itself.
I recently had the same problem with the USB Host library, see Change select pin of USB Host library. It is annoying when you can't define the pin(s), that a particular library uses, yourself... However, the authors of the library probably have their reasons for doing so.
Looking through the various headers of the library, I found in boarddefs.h
on lines 134-138:
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
// ATmega48, ATmega88, ATmega168, ATmega328
#else
//#define IR_USE_TIMER1 // tx = pin 9
#define IR_USE_TIMER2 // tx = pin 3
So, the line to change, for the Uno, would seem to be line 138 in boarddefs.h
#define IR_USE_TIMER2 // tx = pin 3
However, it is not, as that merely states that for the Arduino Uno IR_USE_TIMER2
is used...
Looking further down to where the Timers are defined, you will see the section for Timer 2, on line 146. In that section the transmit pin, for a number of versions of the Arduino, is defined as TIMER_PWM_PIN
on lines 189-197:
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) \
|| defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) \
|| defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324A__) \
|| defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega164A__) \
|| defined(__AVR_ATmega164P__)
# define TIMER_PWM_PIN 14 // MightyCore
#else
# define TIMER_PWM_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc
#endif // ATmega48, ATmega88, ATmega168, ATmega328
You will have to change this line in the library, Line 196 in boarddefs.h:
# define TIMER_PWM_PIN 3 // Arduino Duemilanove, Diecimila, LilyPad, etc
You could try changing the 3
so that you can use another pin. It might work. For example to use pin 5, en lieu of pin 3, change the 3
to a 5
, like so:
# define TIMER_PWM_PIN 5 // Arduino Duemilanove, Diecimila, LilyPad, etc
Be careful not to choose a pin that is used by something else, AND I think that the pin will need to be PWM capable.
Note that the definitions that are in boarddefs.h
used to be in RemoteInt.h
, in earlier versions of the library. See this comment at the very top:
// This file contains all board specific information. It was previously contained within
// IRremoteInt.h
So, make sure that you have the latest version of the IRremote library.
The above hack should also work for the Mega2560 as is uses the same timer, Timer 2, see lines 68-71:
// Arduino Mega
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
//#define IR_USE_TIMER1 // tx = pin 11
#define IR_USE_TIMER2 // tx = pin 9
Other boards are also defined in this file. If you have another type of board, the timer may be different, but the process to find which line to change would be very similar...
IR_USE_TIMER2
is defined...