I uploaded the blink example to a project that uses an AtMega328PB with a 16MHz external crystal and two 22pF capacitors. The blink example, as we know, is supposed to blink an LED every second. When I upload the blink sketch to the AtMega, it blinks on and off ebout every 16-17 seconds. Is the chip not keeping time because of my crystal or is it another problem?
to a project that uses an AtMega328PB with a 16MHz external crystal and two 22pF capacitors
What has the rest of the project got in the way of decoupling capacitors and power supply?
Lack of adequate decoupling can cause oscillations on the supply which can basically screw up the clock.
Grumpy_Mike:
What has the rest of the project got in the way of decoupling capacitors and power supply?
Lack of adequate decoupling can cause oscillations on the supply which can basically screw up the clock.
I am wondering if it is because I am using a 3.7v lipoly (little over 4.2 when fully charged) battery to power it.
Ok. So you are using the chip with the factory fuse settings (clock divider, internal oscillator etc.).
You need to either use a avrdude to change these to match the core or do the following in the IDE:
Burn a bootloader. The burning process sets the fuses for Arduino environment as a side effect.
Reload your blink sketch as you did before.
As an exercise, look in the data sheet for the MCU to see what the factory fuse settings are.
You will have to reduce the speed to no more than 13 MHz to be able to run in spec at 3.2V - the minimum for that type of battery. A 12 MHz crystal would do fine for this. 16 MHz may or may not work when the battery reaches end of life, minimum specification for 16 MHz is 3.78V.