# Compiler error with IRremote library and tone() function

**URL:** <https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549>\
**Category:** Programming\
**Created:** [July 6, 2014, 3:55am UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549 "2014-07-06T03:55:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 6, 2014, 3:55am UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/1 "2014-07-06T03:55:25Z")

</div>

I looked up how to control an arduino with an IR remote and I found this library:

> **[GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for...](https://github.com/Arduino-IRremote/Arduino-IRremote)**
>
> Infrared remote library for Arduino: send and receive infrared signals with multiple protocols - GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive in...

It works, but when I use this library and the tone() function in the same sketch I get an error.  
Here is my code:

```plaintext
#include <IRremote.h>

[const int sound = 8;
const int led = 2;
const int sensor = 3;

int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT);
  pinMode(sound, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

void loop(){
  while(digitalRead(sensor) == LOW);
  while(true){
    delay(500);
    tone(sound, 262);
    digitalWrite(led, HIGH);
    delay(500);
    //noTone(snd);
    digitalWrite(led, LOW);
  }
}

```

[

And here is my error:

```plaintext
core.a(Tone.cpp.o): In function `__vector_7':
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/Tone.cpp:535: multiple definition of `__vector_7'
IRremote\IRremote.cpp.o:C:\Users\Albert\Documents\Arduino\libraries\IRremote/IRremote.cpp:337: first defined here

```

When I comment out either the IR library calls or the tone() and noTone() functions, the code compiles fine.

I suspect that the tone() function and the IR libraries are conflicting, but I'm not sure. Any thoughts?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![rogerClark](https://avatars.discourse-cdn.com/v4/letter/r/9fc29f/32.png) [@rogerClark](https://forum.arduino.cc/u/rogerClark)\
**Post date:** [July 6, 2014, 7:39am UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/2 "2014-07-06T07:39:55Z")

</div>

See

[http://letsmakerobots.com/content/solved-strange-compilation-error](http://letsmakerobots.com/content/solved-strange-compilation-error)

Its because both libraries use timer 2 (interrupts)

---

<div class="post-metadata">

**Author:** ![AnalysIR](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@AnalysIR](https://forum.arduino.cc/u/AnalysIR)\
**Post date:** [July 6, 2014, 1:57pm UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/3 "2014-07-06T13:57:55Z")

</div>

The latest version of the IRLib library has an option which does not use timers. (bitbang mode) \*& allows sending from any pin.

In this mode it is blocking while sending, but does free up the various timers.

The Author says it is experimental at this stage.

If I remember correctly, it may also allow choice of timer to use(?)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 6, 2014, 7:33pm UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/4 "2014-07-06T19:33:29Z")

</div>

Thank you everyone! My sketches are now running smoothly! 🙂

---

<div class="post-metadata">

**Author:** ![AnalysIR](https://avatars.discourse-cdn.com/v4/letter/a/848f3c/32.png) [@AnalysIR](https://forum.arduino.cc/u/AnalysIR)\
**Post date:** [July 6, 2014, 8:27pm UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/5 "2014-07-06T20:27:47Z")

</div>

It might be agood idea to share what you did to fix it, so that others can benefit/learn!

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 6, 2014, 10:30pm UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/6 "2014-07-06T22:30:42Z")

</div>

Here is the modified code in the IRremoteInt.h file:

(Lines 25-68)

```plaintext
// define which timer to use
//
// Uncomment the timer you wish to use on your board.  If you
// are using another library which uses timer2, you have options
// to switch IRremote to use a different timer.

// Arduino Mega
#if defined( __AVR_ATmega1280__ ) || defined( __AVR_ATmega2560__ )
  //#define IR_USE_TIMER1   // tx = pin 11
  #define IR_USE_TIMER2     // tx = pin 9
  //#define IR_USE_TIMER3   // tx = pin 5
  //#define IR_USE_TIMER4   // tx = pin 6
  //#define IR_USE_TIMER5   // tx = pin 46

// Teensy 1.0
#elif defined( __AVR_AT90USB162__ )
  #define IR_USE_TIMER1     // tx = pin 17

// Teensy 2.0
#elif defined( __AVR_ATmega32U4__ )
  //#define IR_USE_TIMER1   // tx = pin 14
  //#define IR_USE_TIMER3   // tx = pin 9
  #define IR_USE_TIMER4_HS  // tx = pin 10

// Teensy++ 1.0 & 2.0
#elif defined( __AVR_AT90USB646__ ) || defined( __AVR_AT90USB1286__ )
  //#define IR_USE_TIMER1   // tx = pin 25
  #define IR_USE_TIMER2     // tx = pin 1
  //#define IR_USE_TIMER3   // tx = pin 16

// Sanguino
#elif defined( __AVR_ATmega644P__ ) || defined( __AVR_ATmega644__ )
  //#define IR_USE_TIMER1   // tx = pin 13
  #define IR_USE_TIMER2     // tx = pin 14

// Atmega8
#elif defined( __AVR_ATmega8P__ ) || defined( __AVR_ATmega8__ )
  #define IR_USE_TIMER1   // tx = pin 9

// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, etc
#else
  #define IR_USE_TIMER1   // tx = pin 9   uncomment out this line
  //#define IR_USE_TIMER2     // tx = pin 3   comment out this line
#endif

```

Note that if the servo library is also used in your sketch, the IRlibrary will interfere with it.

See here for more information on arduino timers:

[http://letsmakerobots.com/content/arduino-101-timers-and-interrupts](http://letsmakerobots.com/content/arduino-101-timers-and-interrupts)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 8:45pm UTC](https://forum.arduino.cc/t/compiler-error-with-irremote-library-and-tone-function/244549/7 "2021-05-05T20:45:52Z")

</div>


