Arduino - Burning bootloaders and uploading sketches - A Crash course (no pun intended)

Introduction

Today I decided to finish two related projects that have been sitting around for at least 6 months:
  1. Finish my generic programming and test board board (ICSP and Serial)
  2. Get my USBasp programmer working
I have a couple of shop-bought Arduino boards, but tend to build custom boards for each project once the 'breadboard phase' proves it will work.  This makes it much cheaper and enables additional components to be added to the board to provide a complete solution.
Example homemade Arduino (with darlington array)

To do this I need to be able to turn a standard Atmega 328P-PU (or 328-PU) into an 'Arduino', and upload sketches whilst embedded in a custom circuit (i.e. without a USB connection).

First : What is an Arduino ? 

Its important to explain what an Arduino is before I continue this post.  Arduino's are basically ATMEL micro-controllers with an 'Arduino bootloader' (well that was easy).  OK, it needs a little more explanation....

The 'Arduino Bootloader' is a small program that executes when the micro-controller is powered-up and enables the uploading of sketches using a serial connection (via USB in most cases); which is how the Arduino IDE uploads your sketches.

Without the boot-loader you would need an external programmer (e.g. USBasp) and a lot more know-how; making the whole experience more complex.  You can see the bootloader at work when you restart an Arduino, the LED connected to digital pin 13 will flash straight after power-up; at this point the IDE can trigger the upload of a sketch via USB (Serial really).

My Dev board 

As I build custom 'Arduino' boards for each project I have also created a dev board to program either via serial or an external programmer (via ICSP).  It has a zif socket to allow chips to be quickly swapped, serial programming pins and an ICSP socket.  It also has all the necessary components to run as a stand-alone Arduino.

My dev board/programmer

Loading Sketches via serial

All of my custom boards have serial connection pins to enable the uploading of sketches with the micro-controller left in-place (i.e. in-circuit).  I did it this way because I couldn't get my USBasp external programmer to work; it also means that the serial lines have break-out pins for debug output via the same USB-to-serial cable that I used to upload the sketch (win-win)

I have a couple USB to Serial devices ( CP2102 and CH230 ) both cheap EBay purchases ( a couple of pounds each ).  They have different order pin outs, so I tend to use the CP2102 for custom boards (historical) and the CH230 for Arduino mini pros (the pin orders match).


My serial programmers

When uploading a sketch using the Arduino IDE just select the COM port in the same way as when you are using USB to a normal Arduino and upload.

Loading sketches with an external programmer

You can also upload your sketch directly to the micro-controller without using the Arduino bootloader using an external programmer or another Arduino.

My USBasp external programmer

Arduino shield to program another amtega 328
There are a couple of reasons why you might not want to use the Arduino bootloader and upload your sketch directly to the Atmel micro-controller:
  1. The bootloader uses about 0.5K of memory, this may not sound like a-lot, but the Uno only has 32K so you might need all of it.
  2. The bootloader pauses the start-up while it checks for a sketch upload, which you can avoid by uploading your sketch by itself.
To do this I use an USBasp external programmer bought from ebay for a couple of pounds.  It connects to the ICSP (In-Circuit Serial Programming) pins of the Arduino.  Within the Arduino IDE verify your sketch as normal then use the 'File->Upload using Programmer' menu item to upload to the micro-controller.  Make sure you have selected the correct external programmer and board in the IDE.

Burning a bootloader

You may need to burn a bootloader to a micro-controller for a couple of reasons:
  1. You have purchased a blank Atmel chip and want to convert it to use the Arduino bootloader
  2. The bootloader has become corrupt (I have heard this can happen but have not experienced)
  3. A new version of the bootloader is available and you want to upgrade
Again I use the same USBasp external programmer to upload bootloaders.

Make sure you make selected the correct programmer and board in the Arduino IDE, then select 'Tools->Burn bootloader'.  After a short wait the upload will finish and you can reboot your new Arduino.

Bootloader Gotcha

One problem I have encountered is trying to put a bootloader on a atmega 328-PU (rather than the 328P-PU).  The 328P-PU has a low power sleep mode which the 328-PU doesn't.  For all other purposes they are the same and are often sold cheaply without the bootloader.  Sounds good until you try and burn the bootloader, the 328-PU isn't supported by the Arduino IDE (avrdude used under the hood actually).

Avrdude checks the signature of the chip before writing, unfortunately the 328-PU has a different signature;
  • ATmega328   0x1E 0x95 0x14
  • ATmega328P 0x1E 0x95 0x0F

Quick Hack

The quick fix is to change the expected signature in the avrdude configuration file '{Arduino install}\hardware\tools\avr\etc\avrdude.conf'.

Open the file and locate the 328P section (search for 328P), near the top of the definition you will see the 'signature' statement.  Change this to '0x1e 0x95 0x14', save and restart the Arduino IDE.

You will obviously have to change it back to program atmega 328p-pu micro-controllers.

Proper Fix

To perform a more permanent fix you need to create a new board in '{Arduino_install}\hardware\arduino\avr\boards.txt'.  Copy the Uno definition and change the name etc and 'mcu'; you will also need to create a new mcu, based on '328p-pu' with different signature, in the file '{Arduino install}\hardware\tools\avr\etc\avrdude.conf' to link to.

Once you have done this you will be able to select your new board in the Arduino IDE.

Hope this helps someone.

Comments

Popular Posts