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