Calculate OCR2A from audio frequency I'm working on a Arduino-based synthesizer using this tutorial, specifically using a wavetable and 1-bit DAC. I understand that the value for OCR2A register sets the frequency, but how do I calculate the value?

I've seen this list of notes which is great, but I want arbitrary frequencies in Hz...

Here's the code I'm working from:

#include &lt;avr/interrupt.h&gt;

// sinewave parameters
#define FREQ 18
#define PI2 6.283185    // 2*PI saves calculation later
#define AMP 127         // scaling factor for sine wave
#define OFFSET 128      // offset shifts wave to all &gt;0 values

#define LENGTH 256      // length of the wave lookup table
byte wave[LENGTH];      // wavetable


void setup() {

  // populate wavetable
  for (int i = 0; i &lt; LENGTH; i++) {
    float v = (AMP * sin((PI2 / LENGTH) * i));
    wave[i] = int(v + OFFSET);
  }

  // set timer1 for 8-bit fast PWM output
  pinMode(9, OUTPUT);       // make timer’s PWM pin an output
  TCCR1B = (1 &lt;&lt; CS10);     // set prescaler to full 16MHz
  TCCR1A |= (1 &lt;&lt; COM1A1);  // pin low when TCNT1=OCR1A
  TCCR1A |= (1 &lt;&lt; WGM10);   // use 8-bit fast PWM mode
  TCCR1B |= (1 &lt;&lt; WGM12);

  // set up timer2 to call ISR
  TCCR2A = 0;             // no options in control register A
  TCCR2B = (1 &lt;&lt; CS21);   // set prescaler to divide by 8
  TIMSK2 = (1 &lt;&lt; OCIE2A); // call ISR when TCNT2 = OCRA2
  OCR2A = FREQ;           // set frequency of generated wave
  sei();                  // enable interrupts to generate waveform!
}


void loop() {
  // nothing to do here!
}


// called every time TCNT2 = OCR2A
ISR (TIMER2_COMPA_vect) {   // called when TCNT2 == OCR2A
  static byte index = 0;    // points to each table entry
  OCR1AL = wave[index++];   // update the PWM output
  asm("NOP; NOP");          // fine tuning
  TCNT2 = 6;                // timing to compensate for ISR run time
}
The equation to calculate it is given in the first article you linked:



The 2MHz value comes from the factor of 8 prescale on the 16MHz clock and 256 is the size of the lookup table specified by your LENGTH define. If you start from a frequency you want to synthesize, just use the above equation to solve for the necessary value of OCR2A. Note that since you have to input integer values to OCR2A, you can't really achieve arbitrary frequencies, but you're certainly not locked in to the values given in the supplemental musical note PDF you reference.

So, to solve for OCR2A:  

OCR2A = TCNT2_rate / (desiredFreq * wavetableLength);