confused about bootloader and ATMeag328

Hi Everyone,

I want to use the Arduino Software (and it’s collection of libraries) to program some ATMega328.

I read that in order to do that, I need to burn the bootloader into the ATMega. This can be done, using another Arduino (such as a Nano) and use it as an ISP programmer.

Then, on the example that I found, they use a USB-to-Serial circuit to be able to upload sketches into the ATMega…

Some other threads I found, mention that the bootloader is not required…

So Here are my questions:

  • What is the bootloader doing, and is there many version ?
  • Can I use that same Nano to upload sketch (to the ATMega328 ) using it as ISP ?
  • In which case is the bootloader required ?
  • What other chips (ATTiny2313, ATMega8/16, etc …) can be used the same way ?

Thanks everyone !

You can do without a bootloader. You do need to burn the fuses first using a Programmer to set the system clock type (16 MHz external full swing crystal for an Uno for example, among other things).

Then File:Upload Using programmer to load the sketch with no bootloader.

I recommend Nick Gammon's bootload installer sketch to burn the fuses and put a bootloader on.

or

Then you can upload sketches via serial port, and since you then have the serial port connected you can use it to debug your sketches using the Serial Monitor with simple

Serial.println("program is here now"); 
//or 
Serial.println(variable_name);

type statements.

An inexpensive USB/Serial adapter can be used
http://www.tinyosshop.com/index.php?route=product/product&filter_name=ftdi&filter_description=true&filter_sub_category=true&page=2&product_id=186
or you can put your Nano into reset (connect its reset to Gnd) and use the FTDI adapter on your board, connecting to Rx & Tx, and manually resetting your breadboard when the IDE shows "Compiled xxx of 32xxx bytes". Timing the reset press can easier if select File:Preferences and check Verbose outputs.
Also check the Arduino Accessories on the Products page, I've not used nor do I know about availability.

If you really get into it, you can look at my Standalone Programmer from SD Card board, for when you get out in the field and want to install pre-compiled programs without dragging your laptop along.
http://www.crossroadsfencing.com/BobuinoRev17/Programmer.html

The bootloader helps you upload sketches via the serial port. It is not required. If you only need to upload one sketch (code) then you can do without. Either:

  • Use an ISP programmer to upload a bootloader.
  • Use the bootloader to upload your sketch

or:

  • Use an ISP programmer to upload your sketch

The bootloader is useful if you are uploading many sketches, because it saves plugging in the ISP programmer all the time. Useful, but not necessary.

Nick:

So if I understand well, the same set-up that I could use to burn a bootloader (MOSI to MOSI, MISO to MISO, pin 10 to Reset, etc ...) could be used to upload the sketch ?

Then only advantage of using the bootloader is to be able to upload a sketch using a serial mean ? correct ?

On the other hand, NOT having a bootloader, frees up a bit of memory...right ?

That ISP upload possibility, does it apply to most AVR chip or only specific ones, like ATMega328, ATTint45/85 ???

Could I use it also, let's say with an ATTiny2313 or ATMega16 ?

Thanks

IPS upload applies to all the chips. In some cases it is the only way.

Having serial upload in place means, during debugging, not having to plug & unplug the ICPS cable for every upload. Pretty convenient to upload and then have the sketch start without having to change any cabling. I find having the Programmer attached keeps the sketch from running - likely due to the Programmer holding the Reset line low.

Does free up a bit of memory - 1kbyte on '328P chips (512 words, 1 Kbyte). I've never managed to write a huge enough sketch where that was a problem. Nick's code for the standalone programmer is pages long with lots of functions, and compiles to 14K. I've written other sketches that were 25 pages long when printed, and those compiled to ~15K as well.
4K, 8K chips, you might run into problems, but there is also less IO, and some chips don't have a UART even.

thanks CrossRoad for the precision.

My concern about memory was because I once wrote something on an ATTiny2313, that got full because I had a lot of custom messages going to the LCD.

I had to select an ATTint4313 as I ran out of memory....

20 pin chip with 2K/4K of memory vs 28 pin chip with 16/32K of memory - space would have to be really tight for me to save that 4/10 of an inch on a board ...

it was an earlier design, before I discovered the ATMega328....

On the other hand, NOT having a bootloader, frees up a bit of memory...right ?

Yes, it frees up the bootloader space. Could be 512 bytes or 2048 even.

That ISP upload possibility, does it apply to most AVR chip or only specific ones, like ATMega328, ATTint45/85

You mean the ICSP one? MISO/MISO/SCK etc? All AVR chips support that (all I am aware of) which is how the bootloader gets there in the first place.

I have successfully run Arduino programs on a raw Atmega328

Atmega328 needs to be placed on a breadboard. Vcc and GND lines connected. Then connect the MOSI, MISO, CLK, RESET, VCC and GND wires to the USBasp programmer (jumper cables). Reset pin of ATmega should be pulled high with a 10K resistor. Need a proper 5V line (bench PSU or LM7805 etc). Using internal clock no need for crystal.

Copy avrdude.exe in a directory with avrdude.conf. Find the Arduino compiled binary (*.hex) in temp directory like "C:\Users\john\AppData\Local\Temp\build4279101614248787228.tmp"

The "-B 10" is needed to slow down avrdude because Atmega328p with internal RC is slow (1 MHz?)
From within the Arduino IDE the way to do it is to modify the avrdude.conf file there is an entry "# default_bitclock = 2.5" set it to whatever

The avrdude.conf file must have a signature change to accept the Atmega328p:
part
id = "m328p";
desc = "ATMEGA328P";

signature = 0x1e 0x95 0x0F; // proper Atmega328p

signature = 0x1e 0x95 0x14; // probable Atmega328

To read:

C:\Usbasp\SelfProgramming>avrdude -c usbasp -p ATmega328p -U flash:r:somefileorother:i -B 10

To write:

C:\Usbasp\SelfProgramming>avrdude -c usbasp -p Atmega328p -B 10 -U flash:w:"\Users\john\AppData\Local\Temp\build4279101614248787228.tmp\battery_monitor.cpp.hex"