title: Arduino Uno PWM pins conflict
body: <p>I built <a href="http://www.instructables.com/id/DIY-Arduino-Motor-Shield-L298N-Chip-2-4-Amps-" rel="noreferrer">this motor shield</a> based on the L298N chip to control two motors of a tank. It uses pins 5 and 6 for one motor, and pins 10 and 11 for the other.</p>
  
  <p>While trying to add a <a href="http://www.farnell.com/datasheets/30500.pdf" rel="noreferrer">TSOP 4838</a> in order to control the tank with an IR remote I noticed that moving the motor on pins 10/11 in reverse only works at full speed - that is, a HIGH (255) value on pin 11. Anything below that value doesn't output anything on pin 11 (measured voltage on those pins is 0&nbsp;V).</p>
  
  <p>For the remote I use <a href="https://github.com/shirriff/Arduino-IRremote" rel="noreferrer">this library</a>. The <a href="https://en.wikipedia.org/wiki/Infrared" rel="noreferrer">IR</a> receiver is connected on pin 2 (but the pin doesn't matter). The problem is the library code itself. The line that enables IR listening <code>irrecv.enableIRIn();</code> is what is causing the problem. I learned that there is a conflict of internal Arduino timers and the pins used for PWM by the shield.</p>
  
  <p>This is the code to power the motor in reverse:</p>
  
  <pre><code>#include &lt;IRremote.h&gt;
  
  // IR receiver configuration
  const int irPin = 2;
  IRrecv irrecv(irPin);
  
  // Motors configuration
  const int mLeftPin1  = 10;
  const int mLeftPin2  = 11;
  const int mRightPin1 = 5;
  const int mRightPin2 = 6;
  
  void setup()
  {
    // Start IR
    irrecv.enableIRIn();
  
    // Setup motors
    pinMode(mLeftPin1, OUTPUT);
    pinMode(mLeftPin2, OUTPUT);
    pinMode(mRightPin1, OUTPUT);
    pinMode(mRightPin2, OUTPUT);
  
    // Move left motor in reverse, slower speed
    analogWrite(mLeftPin2, 100); // This works only with 255 instead of 100
    digitalWrite(mLeftPin1, LOW);
  }
  </code></pre>
  
  <p>Now, I found <a href="http://blog.oscarliang.net/arduino-timer-and-interrupt-tutorial/" rel="noreferrer">here</a> that the pins used by the timers on <a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="noreferrer">Arduino Uno</a> are:</p>
  
  <ul>
  <li>Pins 5 and 6: controlled by Timer0</li>
  <li>Pins 9 and 10: controlled by Timer1</li>
  <li>Pins 11 and 3: controlled by Timer2</li>
  </ul>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>Why does the shield in the instructable use pins 10 and 11 for PWM ? They correspond to 2 different timers. Why not 9 and 10?</p></li>
  <li><p>In order to use the IR along with the motor shield, what timer should I configure the IR library to use?</p></li>
  <li><p>If the answer is 2, a line should be uncommented in <code>IRremoteInt.h</code>. I am guessing the Uno would take the <code>else</code> branch at line 68, although only timer1 and timer2 are there. I wonder why timer0 couldn't be used for the Uno.</p></li>
  </ol>
  
  <p>Although I'd like to leave cutting traces and resoldering as a last option, another possibility would be to change the pins used by the shield, but which? And I am guessing this would also be paired with configuring the timers to PWM on other pins than default, but I don't know anything about timers/interrupts and my knowledge of Arduino and C is limited.</p>
  
  <p>I made this a long question, because I want to learn not just solve the problem, so feel free to explain more than what is asked.</p>
  
  <p>While looking up for a solution I also found other conflicts to keep in mind when using <a href="http://en.wikipedia.org/wiki/Pulse-width_modulation" rel="noreferrer">PWM</a> or timers:</p>
  
  <ul>
  <li><em>Timer0</em> is an 8-bit timer, it can hold a maximum value of 255. It is used by <code>delay()</code> and <code>millis()</code>, so there are consequences when messing with it</li>
  <li><em>Timer1</em> is a 16-bit timer, it can hold a maximum of 65535 (an unsigned 16-bit integer). The Arduino Servo library uses this timer</li>
  <li><em>Timer2</em> is an 8-bit timer used by the Arduino <code>tone()</code> function</li>
  </ul>
  
  <p>And, of course, the <em>IRremote</em> library uses <code>TIMER_RESET</code>, so depending on which timer it uses it can conflict with the associated pins.</p>