Saturday, June 23, 2012

Burning a bootloader into a bare ATmega328

Introduction 

Burning the standard Arduino boot-loader onto a bare ATMEGA328 is fairly straightforward but the steps that make it a more streamlined process are not found altogether on the web. This post gives you all you need to do it yourself. If in the end you decide not to do it yourself you can get an ATmega328P with a boot-loader already installed here for $4.95.

You will be using an existing Arduino Uno or Mega as an ISP (In-System Programmer) to program a bare ATmega328 micro-controller. You do not require any ancillary components at all despite what you may have read elsewhere. Neither do you need any other software than the Arduino IDE. You will need to create a simple circuit on a breadboard that you will use to interconnect with the Uno/Mega.

Here are the steps:
  1. Prepare the Arduino environment
  2. Wire up the simple breadboard circuit
  3. Burn the boot-loader

Prepare the Arduino environment

The Arduino IDE is already set up for use with multiple boards. However you need to be able to specify the minimal bread-boarded ATMEGA328 as a target. You can enable the IDE for this by using a predefined boards.txt file. You can download the required boards.txt file in a zipped breadboard folder containing it here .

Once you have downloaded the zip file, unzip it and place the breadboard folder in a folder called hardware in your Arduino sketches folder.  You need to restart the Arduino IDE  to see an option in the Tools/Boards menu for ATmega328 on a breadboard (8 MHz internal clock). The reason it shows 8Mhz rather than 16Mhz is that you will be using the built-in internal clock on the ATmega328 of 8Mhz rather than an external 16Mhz clock to simplify the bread-boarding (no external crystal required).

Next you need to load the ArduinoISP sketch provided with the IDE. Just make one change to the sketch for Arduino 1.0. Locate the delay(40) in the heartbeat() function and change it to delay(20).

Make sure you specify Uno/Mega or whatever Arduino you are using as the programmer in Tools/Boards. Once you have done this you are ready to bread-board your ATmega328.

 

Wire up the simple breadboard circuit


This is taken from the Arduino tutorial that can be found here. Pin connections are per this table (all boards except the Mega use the Uno connections):


Arduino Programmer                      ATMega328 Target
Uno Pin   Mega pin  Function Function Pin
9 9  GND

10 10  RESET TARGET (LOW) RESET 1



+5V 7



GND 8
11 51  MOSI MOSI 17
12 50  MISO MISO 18
13 52  SCK SCK 19



+5V 20



GND 22

Once you have finished bread-boarding you're ready to burn the boot-loader.

 

Burn the boot-loader

Select  Tools/Boards/ATmega328 on a breadboard (8 MHz internal clock).  Then  Tools/Burn Bootloader. You should see avrdude do its thing. The result should be similar to this:



################################################# | 100% 0.06s






avrdude: 1 bytes of lock written


avrdude: verifying lock memory against 0x0F:

avrdude: load data lock data from input file 0x0F:

avrdude: input file 0x0F contains 1 bytes

avrdude: reading on-chip lock data:








Reading | avrdude: Send: V [56] X [58] . [00] . [00] . [00]   [20] 
avrdude: Recv: . [14] 



avrdude: Recv: . [cf] 



avrdude: Recv: . [10] 



################################################## | 100% 0.02s






avrdude: verifying ...



avrdude: 1 bytes of lock verified


avrdude: Send: Q [51]   [20] 


avrdude: Recv: . [14] 



avrdude: Recv: . [10] 









avrdude done.  Thank you.



And that's it! Use your bootloaded ATMega328 as you like. Of course, if this sounds like too much effort you can get an ATmega328P with a bootloader already installed here for $4.95.

oddWire



8 comments:

  1. hi, nice tutorial thankyou, do you know how to make working on Arduino 1.0.1 ? isn't working for me in that version, only in 0023 or less. thanks!

    ReplyDelete
  2. Note for Arduino 1.0: you need to make one small change to the ArduinoISP code. Find the line in the heartbeat() function that says "delay(40);" and change it to "delay(20);

    ReplyDelete
  3. Nice tutorial. Is there a simple way to modify the breadboard to use a crystal so the chip can run at full speed?

    ReplyDelete
  4. Hi John, 20Mhz is not a speed formally supported by the Arduino team. There was a 20Mhz board that Sparkfun produced and there is a reference on this page http://wtfmoogle.com/?p=1381 to a boards.txt file that configured it for 20Mhz.

    I do know that millis() will also not work correctly as it is hard-coded for 16Mhz but there is code here

    http://forum.arduino.cc/index.php?topic=70475.0

    that addresses the issue.

    I have not used either so good luck! oddWires.


    1. Need a boards.txt file:

    pro328_20.name=Arduino pro 328 20mhz

    pro328_20.upload.protocol=stk500
    pro328_20.upload.maximum_size=30720
    pro328_20.upload.speed=57600

    pro328_20.bootloader.low_fuses=0xFF
    pro328_20.bootloader.high_fuses=0xDA
    pro328_20.bootloader.extended_fuses=0×05
    pro328_20.bootloader.path=atmega
    pro328_20.bootloader.file=ATmegaBOOT_168_atmega328_20.hex
    pro328_20.bootloader.unlock_bits=0x3F
    pro328_20.bootloader.lock_bits=0x0F

    pro328_20.build.mcu=atmega328p
    pro328_20.build.f_cpu=20000000L
    pro328_20.build.core=arduino

    2. Modify AVR

    ReplyDelete
  5. Sir, I downloaded the breadboard file, Extracted it and placed it in the hardware folder of arduino, yet I am not getting the option ' ATmega328 on a breadboard (8 MHz internal clock)' in the tools/boards menu. I am using Arduino IDE 1.0.1 version.
    your kind and immediate help is requested

    ReplyDelete
  6. sir, I some how managed to sort out the problem of getting the option ' ATmega328 on a breadboard (8 MHz internal clock)' in the tools/boards menu. But now ended with the error as 'error while burning bootloader. missing 'bootloader.tool' configuration parameter.
    the description it gives-
    missing Arduino.h:215:26:
    error Arduino.h no such file found
    immediate help is requested

    ReplyDelete
  7. sir, I have the same error
    imediate help is requested

    ReplyDelete
  8. sir, I have the same error
    imediate help is requested

    ReplyDelete