ATmega328P-PU with internal 8MHz oscillator

Hi, I am trying to build a project running on LiPo batteries. For this purpose from what I have available, an ATmega using its internal 8MHz oscillator seems to be a good idea (or an ATtiny, but the only thing I have is an ATmega328PU).

ArduinoISP with a Nano@328 is working (with the common setup and a capacitor between RST and GND). When a 16MHz crystal is connected to the ATmega, I can flash it as an Arduino Uno or Mini Pro 3.3V/8MHz bootloader (at least, it says it is successful).

Now, from what I understood, I need to change the fuses (though I have only a vague idea what that is :)) to change to the internal oscillator. In the boards.txt, I copied the entry for the Arduino Mini Pro@8MHz. I entered the fuse values into the calculator http://www.engbedded.com/fusecalc/ and changed it to 8MHz internal oscillator.

I end up with this entry:

pro328.upload.protocol=arduino
pro328.upload.maximum_size=30720
pro328.upload.speed=57600

pro328.bootloader.low_fuses=0xFF
pro328.bootloader.high_fuses=0xDA
pro328.bootloader.extended_fuses=0x05
pro328.bootloader.path=atmega
pro328.bootloader.file=ATmegaBOOT_168_atmega328_pro_8MHz.hex
pro328.bootloader.unlock_bits=0x3F
pro328.bootloader.lock_bits=0x0F

pro328.build.mcu=atmega328p
pro328.build.f_cpu=8000000L
pro328.build.core=arduino
pro328.build.variant=standard

When I try to flash this, I get

 ***failed;  
avrdude: verification error, first mismatch at byte 0x0000
         0x05 != 0xfd
avrdude: verification error; content mismatch

Why is that (apart from "Because I don't really know what I am doing") and what can I do?

Additional question: I also have some 8MHz oscillators lying around. So since I can flash the Mini bootloader, this seems to an almost working alternative. Do I use the same capacitors as for the 16MHz crystal? What would be the advantages (more accurate?) and disadvantages (space, power?)?

Is is a ATmega328-PU or a ATmega328P-PU ?
You need an ATmega328P-xx, that is the ‘PicoPower’ version, and that is the one that is used with Arduino projects. The ATmeg328-PU is not the PicoPower version.

I think there is an Arduino version that uses the internal oscillator, perhaps one of the wearable versions. If you can make your design compatible with that, you can burn that bootloader from within the Arduino IDE, and that will also set all the fuses that are needed.

A crystal or external resonator is more accurate for timing and for the baudrate of the Serial data.
A baudrate of 38400 is the best baudrate for 8MHz and 16MHz, and if you use your project at moderate temperatures indoors, you can use the internal oscillator also for the Serial data.

A 8MHz crystal needs the same two 22pF to GND. An oscillator runs on its own and doesn’t need those. Can you make a photo of it ?

Peter_n:
Is is a ATmega328-PU or a ATmega328P-PU ?
You need an ATmega328P-xx, that is the ‘PicoPower’ version, and that is the one that is used with Arduino projects. The ATmeg328-PU is not the PicoPower version.

Sorry, I didn’t notice the difference. They are P-PU. I think also else, I couldn’t flash the Uno/Pro bootloaders.

I think there is an Arduino version that uses the internal oscillator, perhaps one of the wearable versions. If you can make your design compatible with that, you can burn that bootloader from within the Arduino IDE, and that will also set all the fuses that are needed.

Ok, the the bootloader also has to be different for the same frequency depending on internal or external oscillator? Then I’ll probaby go with the crystal and the two capacitors. Seems easier.

A crystal or external resonator is more accurate for timing and for the baudrate of the Serial data.
A baudrate of 38400 is the best baudrate for 8MHz and 16MHz, and if you use your project at moderate temperatures indoors, you can use the internal oscillator also for the Serial data.

Seems like another reason to go with the external variant. I’m really not keen on getting problems with serial data at some point. I would probably never find that bug.

A 8MHz crystal needs the same two 22pF to GND. An oscillator runs on its own and doesn’t need those. Can you make a photo of it ?

Looks like these:
http://www.aliexpress.com/wholesale?catId=0&initiative_id=SB_20141019080417&SearchText=8mhz+crystal
with L8.000 written on them.

What about the crystal while flashing a bootloader? At the moment, everything seems to work with a 16MHz crystal. Is there any connection with the final version of the bootloader?

That's nice, use those 8MHz crystals plus two 22pF. That way you stay out of timing/clock inaccuracy problems. Use for example the Arduino Pro Mini 8MHz bootloader for the ATmega328P. When you can not burn that bootloader, you have a wiring/programmer/timing problem.

A bootloader for 3.3V or 5V is the same. But a bootloader for 8MHz is different than a bootloader for 16MHz. I think that the bootloader doesn't know if that clock is internal or external. The bootloader just runs and assumes that the clock frequency is right to set the right baudrate. The baudrate should match the baudrate of the Arduino IDE to upload a sketch.

Some kind of clock is needed when burning the bootloader. The chips are set factory default to internal oscillator, but after setting the fuse for external you need to have a clock, but the frequency can be 1MHz (or less) to 16MHz as far as I know.

I hope I answered your questions. Try to get a 8MHz bootloader into the chip, burn the bootloader using the Arduino IDE.

I use Nick Gammon's method with his Atmega_Board_Programmer: http://www.gammon.com.au/forum/?id=11637

And it works excellent even for ATmega328 ( without P ) on internal 8MHz oscillator.

Peter_n: That's nice, use those 8MHz crystals plus two 22pF. That way you stay out of timing/clock inaccuracy problems. Use for example the Arduino Pro Mini 8MHz bootloader for the ATmega328P. When you can not burn that bootloader, you have a wiring/programmer/timing problem.

As I said, I already successfully flashed the Mini Bootloader.

A bootloader for 3.3V or 5V is the same. But a bootloader for 8MHz is different than a bootloader for 16MHz. I think that the bootloader doesn't know if that clock is internal or external. The bootloader just runs and assumes that the clock frequency is right to set the right baudrate.

Ok, that is what I thought I understood before. What I don't understand is why my first try didn't work, then. Does the entry in the boards.txt also SET the fuses, when burning the bootloader, or is that just an information how they are expected to be set?

Some kind of clock is needed when burning the bootloader. The chips are set factory default to internal oscillator, but after setting the fuse for external you need to have a clock, but the frequency can be 1MHz (or less) to 16MHz as far as I know.

Ok, thanks. Makes sense. I already burned the Uno bootloader some time ago, so I set the ATmega to external fuse before. Since the exact clock doesn't matter, that's fine, then.

I hope I answered your questions.

Yes, that was very helpful.

common_ground: I use Nick Gammon's method with his Atmega_Board_Programmer: http://www.gammon.com.au/forum/?id=11637

And it works excellent even for ATmega328 ( without P ) on internal 8MHz oscillator.

Ill keep that in my bookmarks, if I really want to use the internal clock at some point and nobody tells me what I did wrong above. At the moment, everything seems to work fine with my method and external crystals.

ElCaron: Does the entry in the boards.txt also SET the fuses, when burning the bootloader,

Yes.

There are special bootloaders, that check which microcontroller it is running on. Adafruit and Nick Gammon have those. There are also mini Optiboot bootloaders of less then 512 bytes. Very handy when using a ATmega8. But when you have something that is compatible with one of the official Arduino board, it is easier.

You might want to put a label on your ATmega chip, which bootloader from which Arduino version is has. I wish I had done that before with my own project .....

Looks like I solved it. I had a closer look at the error message. It complained about the extended fuses value. So I tried what changes if I just set 0x05 for the external fuse value, and it turns out that doesn't change anything in the fuse calculator. With

atmegasa.bootloader.low_fuses=0xE2
atmegasa.bootloader.high_fuses=0xDA
atmegasa.bootloader.extended_fuses=0x05

it flashes fine. Couldn't test anything yet, though.

Low fuse set to 0xF2 should set the internal 8 MHz oscillator.
See Table 28-9 of the datasheet.

Correct me if I am wrong, but with internal oscillator, 11 is reserved. 10 is +65ms, so it should be 0xE2. That also matched the Lilypad 168 entry.