title: Setting up Arduino Uno (ATMEGA328P) PWM with Timer1
body: <p>I want to setup a custom frequency(12Hz) and duty cycle (20%) with a 16MHz Arduino Uno (ATMEGA328P).</p>
  
  <p>AVR Calculator yields:</p>
  
  <pre><code>ICR1 = 20833
  OCR1A = 4167
  </code></pre>
  
  <p>I have read a tonne of forums and tuts but for some reason I cannot get this to work.</p>
  
  <p>Below is my code:</p>
  
  <pre><code>void setup()
  {
    // PB1 is now an output (Pin9 Arduino UNO)
    DDRB |= (1 &lt;&lt; DDB1);
    // PB2 is now an output (Pin10 Arduino UNO)
    DDRB |= (1 &lt;&lt; DDB2);
  
        // Set PWM frequency/top value
        ICR1 = 20833;
  
        // Set PWM duty cycle
        OCR1A = 4167;
  
        // Set inverting mode (start low, go high)
        TCCR1A |= (1 &lt;&lt; COM1A1);
        TCCR1A |= (1 &lt;&lt; COM1B1);
        TCCR1A |= (1 &lt;&lt; COM1A0);
        TCCR1A |= (1 &lt;&lt; COM1B0);
  
        // Set fast PWM Mode
        TCCR1A |= (1 &lt;&lt; WGM11);
        TCCR1B |= (1 &lt;&lt; WGM12);
        TCCR1B |= (1 &lt;&lt; WGM13);
  
        // Set prescaler to 64 and starts PWM
        TCCR1B |= (1 &lt;&lt; CS10);
        TCCR1B |= (1 &lt;&lt; CS11);
      }
  
      void loop() {
        // Refresh PWM frequency
        OCR1A = 4167;
      }
  </code></pre>
  
  <p>If someone can help, that would be great!</p>
  
  <p>Thanks,</p>
  
  <p>Dylan</p>