Crystal/ceramic resonator issue with VCO sketch

Hi all,

Having a tuning issue with my Arduino mega2560 running the attached sketch.

Note: this sketch ran OK with Arduino Uno.

int Pwmpin = 11 ; //Pwm Output Pin
int Fchange = A0; //Frequency change through Potentiometer

void setup()
  pinMode(Pwmpin, OUTPUT);

  TCCR1A = _BV(COM1A1) | _BV(COM1B1); //Non-inverted Mode
  TCCR1B = _BV(WGM13) | _BV(CS11); //Prescalar 8


void loop()

  float frequency = 440 + (analogRead(A0) * 0) / 1024; 
  uint16_t TOP = ((F_CPU / 8) / (frequency * 2)) - 1;
  ICR1 = TOP;
  OCR1A = TOP / 1.5;

I checked the frequency with a chromatic tuner.

The result was 4 to 5 hz low (flat).

For maths convenience i selected 4.4 hz.

my calculation is as follows:

4.4/440 x 16,000,000 = 160,000 hz

I then checked the clock using the Fade sketch.

The result was 495 to 496 milli hz. 4 to 5 milli hz low.

Again for maths convenience i selected 5 milli hz

my calculation is as follows;

5/500 x 16,000,000 = 160,000 hz.

Both suggest the clock is +/- 10,000 ppm , in this case minus.

If my maths is right , then the clock is kaput.

Any thoughts?

Go back to the crystal, it will be more stable.


Thanks for your reply.

Having read further , this seems not that unusual for ceramic resonators.

Wouldn't even attempt to change the resonator , i can hardly see the the little bugger.

This board is as useful as a wooden fry pan and will soon meet it's maker.


Ceramic resonators have no place in modern electronics in my opinion, and its a great annoyance that
some common kinds of Arduino are not quartz accurate, as that's a requirement for many projects and
a false economy - if you want the absolute lowest BOM cost a bare-bones board is indicated anyway.

I managed to transplant crystals on to some of the Uno boards when they changed to resonators and
still had pads for a crystal.