Running ATmega328P on internal 8MHz clock


  • Contest Winner

    Playing around a little bit with GertSanders 50 by 30 mm PCB i was wondering if this board also could run without the external clock (8MHz crystal). Saving not only a couple of components but even better save battery lifetime. It appears the ATmega328 uses less power when running on the internal clock.

    It turned out to be a bigger puzzle than expected i was not able to find a straight answer nor a ready-mate solution. Fortunately i did found a couple partial solution and will try describe the way it worked out for my setup.

    It all starts with a bare ATmega328P chip, they are sold in basically 2 flavors. One with-out a bootloader (see Wikipedia) (or with a non Arduino) they are programmed to run on the 1 MHz internal clock. The second flavor is shipped with a pre-programmed Arduino R3 bootloader these will expect a 16 MHz external clock, (but an 8 MHz crystal will do).

    The clock settings are made by so-called fuses, a better name would be internal dip-switches since a fuse sounds like one-time only programming , while the ATmega's fuses can be re-programmed over and over again. So for both flavors we need change theses fuses and for the first flavor we also need to program a bootloader.

    On the Arduino website we can read how to program a bootloader by connecting it to an Arduio UNO (see here) BUT in this case the programming it selves did not worked out. So wiring is OK the program sequence not:

    A better way to program the Arduino is using the sketch from Bill Westfield which can be found here: https://github.com/WestfW/OptiLoader So download this sketch into the Arduino UNO and when wired liked described above the new Atmega is programmed with a fresh bootloader.

    The last step is the set the correct fuses so the interval 8 MHz clock is used. This turned-out to be not so easy to figure out . There are 3 fuse bytes which need to be set. The "Low", "High" and "Extented" , in the end i did run this page: http://www.engbedded.com/fusecalc/. Where you can selected the appropriate configuration and the site will "calculate" the correct byte's (and vice versa).

    The setting which does work for me are:

    • Low := 0xE2
    • High := 0xDE
    • Ext. := 0xFD

    To set these fuse bytes in the Atmega one needs to change Bill's OptiLoader script around line 730 you can find
    this peace of code:

    const image_t PROGMEM image_328p = {
      {
        "optiboot_atmega328.hex"    }
      ,
      {
        "atmega328P"    }
      ,
      0x950F,				/* Signature bytes for 328P */
      {
         0x3F,0xFF,0xDE,0x05,0   }
      ,
      {
        0x2F,0,0,0,0     }
    

    Once change to this the correct fuse byte's are used

    const image_t PROGMEM image_328p = {
      {
        "optiboot_atmega328.hex"    }
      ,
      {
        "atmega328P"    }
      ,
      0x950F,				/* Signature bytes for 328P */
      {
        // 16 MHz external crystal
    //    0x3F,0xFF,0xDE,0x05,0   
        // 8 MHz internal crystal
          0x3F,0xE2,0xDE,0xFD,0  
    }
      ,
      {
        0x2F,0,0,0,0   
      }
    

    So in short:

    • Connect your new Atmega to an Arduino UNO
    • Download Bill Westfield skecth WITH fuse modifaction into the UNO
    • Give a reset and look at your terminal if the bootloader and fuse where set correctly

    Once the correct bootloader is in. You can place the ATmega328 in GertSanders PCB and program a sketch in in with these type of FTDI USB serial convertors: http://www.ebay.com/itm/320907184616?rmvSB=true.
    Set the Ardiuno IDE to:

    • Tools -> Board -> Arduino Pro or Pro Mini
    • Tools -> Processor -> ATmega328 (3.3V 8 MHz)
    • Tools -> Board -> ArduinoISP

    And have fun with it...



  • Hi BartE,

    I´ve read the internal clock at 8 MHz is not as good as an external 8MHz clock because the accuracy of the internal oscillator seems to be only 10% (Atmel Datasheet page 323). What are your experiences? Would you recommend using internal or external OScillator?

    Also I am wondering why most people are running their custom PCB´s using 8MHz clocks instead of 16MHz. Is it only because of reducing the energy consumption? I think so.

    I originally planned to use 16MHz clocks, but now, from the energy consumption point of view, I will probably switch to the 8MHz variant...


  • Admin

    @siod

    Yes, the lower your frequency is, the lower the power consumption is while running the program, but also the longer it takes to execute the program. So there is some trade offs on it.

    Although the RC oscillator isn't that accurate, it doesn't matter that much. Not many node types are that dependent on highly accurate timings, so it's not necessary to use an xtal oscillator, and thus you can save some $ on components.


  • Contest Winner

    @siod Using a 16 MHz clock requires a 5 volt source. So running on 2 AA(A) batteries and thus 3 volt also limits the clock frequency to 8 MHz



  • @BartE
    Oh, didn´t know that 3 V automatically limits to 16MHz but good to know. I´ve already ordered a bunch of 16MHz Oscillators which I could still install and just run in 8MHz by powering with 2 AA batteries. Also I would stay flexible If I´d need 16MHz; Just use 5V!

    @tbowmo
    thanks for your explanation! After I used up my 16MHz Oscillators I will test a setup using the internal clock.



  • If you use an 16MHz oscillator and power the board with 3V, the board will run unstable as the ATMega does not support 16Mhz with 3V.
    If you just change the power, will not change the oscillator frequenzy. You have to Switch to the internal clock signal when powering the board with 3V and you have a 16MHz clock installed.


  • Admin

    @gloob

    I think that there is a typo in your comment? I think you mean that "If you use an 16Mhz oscillator and power the board with 3V..."

    That would make the most sense in my world 🙂



  • @tbowmo
    You are right. I did modify my post.



  • Ah Ok, so I misunderstood BartE´s post.

    So what should I do now with my bunch of 16MHz Osc´s? Maybe useful for some other project in the future...



  • Have a look here:
    http://www.gammon.com.au/breadboard

    This is the easyiest way to bootload you Atmega328p with 8Mhz. Works like a charm.



  • @HarryDutch Hu awesome link. Thanks!



  • a bit late but a word of warning for anyone embarking on this idea: the internal 8MHz clock is very inaccurate (%10 according to the datasheet), which likely will break asynchronous protocols like UART. As long as you are using only synchronous protocols like I2C you should be fine. More details can be found in this excellent thread on stackexchange https://electronics.stackexchange.com/a/32433/216828



  • @siod yes, mostly because of power consumption. You can run an ATmega328P on a 3V battery, but then you need to use 8MHz instead of 16MHz according to the datasheet.
    As I replied separately, you don't want to use an inaccurate clock for async protocols like UART that require precise timing.



  • @eeichinger
    Yes an old thread, but still useful.
    well if you plan to run on a low voltage, you must disable BOD, else per default the 328 will switch off at 2,7V. And then you need to run 1 at MHz


Log in to reply
 

Suggested Topics

  • 87
  • 2
  • 6
  • 3
  • 10
  • 1

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts