Bootload an ATmega328P Microcontroller
Bootload an ATmega328 Microcontroller
This page adds to any of the Arduino on a Breadboard projects out there – there are a number of great resources on building your own Breadboard-based Arduino. I have documented my own (see here), which I used for various projects. I then found that I was struggling to program some of the boards I made, usually when I switched suppliers. After some investigation, I found that:
1. Not all suppliers provide microcontrollers with a Bootloader (even though they may say they do)
2. Not all ATmega328s are equal
A bootloader, simply put, is a programme that sits on the chip and manages the upload of your sketches onto the chip. There are plenty of bootloading resources out there, but I struggled to find a single one that pulled everything together in a way that made sense to me. Here is the process that I followed.
Step 1: The parts
- 1 x Arduino on a Breadboard
- 1 x Arduino UNO
- Connecting Wires
- Arduino IDE installed on your PC
Step 2: The Approach
We’re going to use the Arduino UNO to bootload the ATmega328 that is sitting on the Arduino-on-a-Breadboard. This is fairly straightforward if you have an ATmega328P-PU, but needs an extra step for an ATmega328-PU. I’ll tackle the differences later on.
Step 3: Program your Arduino UNO as an ISP
We need to program the Arduino UNO to act as a an ISP (In-System Programmer), so that it can burn the bootloader onto the Breadboard chip.
- Open the Arduino IDE
- Open the ArduinoISP sketch (under File, Examples)
- If you’re using version 1.0 of the IDE:
- search for “void heartbeat”
- Change the line that reads:
- delay(40); to delay(20);
- Connect your UNO to the PC, making sure it’s not connected to the Arduino on a Breadboard.
- Ensure your UNO is selected under the Boards menu option, and upload the sketch.
Step 4: Connect your ATmega328P
Now connect your ATmega to your UNO as follows:
- UNO 5v —> ATmega pin 7 (VCC)
- UNO GND —> ATmega pin 8 (GND)
- UNO pin 10 —> ATmega pin 1 (RESET)
- UNO pin 11 —> ATmega pin 17 (MOSI)
- UNO pin 12 —> ATmega pin 18 (MISO)
- UNO pin 13 —> ATmega pin 19 (SCK)
Make sure that you don’t have anything else connected to the ATmega pins used above.
Step 5: Which ATmega328 are you using?
I learnt the hard way that there is more than one type of ATmega328. The two variants that are of interest to us are the ATmega328-PU and the ATmega328P-PU.
To decode the naming conventions:
- -PU means that the chips are in a PDIP package, the format we need for our breadboard.
- The 328P is a picoPower processor, designed for low power consumption, and is the chip used on the Arduino boards.
The 328 does not have picoPower technology, and is not used on the Arduino boards – as a result it is not explicitly supported by the Arduino IDE.
What this means is that we can easily bootload the ATmega328P, but not the ATmega328. This was a problem for me, as I had bought a load of ATmega328-PU chips from a supplier that had listed them at ATmega328P-PU.
Workaround: Scroll to the end of this page if you need the workaround to get the ATmega328-PU working.
Step 6: Bootload the ATmega328
To bootload your chip, open the Arduino IDE:
From the Tools menu:
- under the Board option choose Arduino UNO
- under the Serial Port option ensure the correct port is selected
- under the Programmer option choose Arduino as ISP
To burn the Bootloader, choose Burn Bootloader from the Tools menu.
You should see a message “Burning bootloader to I/O Board (this may take a minute)”
Once the bootloader has been burned, you’ll see a message confirming the success.
Congratulations: You’re now ready to load sketches onto your ATmega microcontroller!
Workaround for the ATmega328-PU
If you try to bootload an ATmega328-PU, you’ll get a message something along the lines of:
avrdude: Device signature = 0x1e9514
avrdude: Expected signature for ATMEGA328P is 1E 95 0F
Double check chip, or use -F to override this check.
You could also get a more exciting version:
avrdude: Yikes! Invalid device signature.
What does this all mean?
Each microcontroller has a signature – a unique code that identifies its model. When you bootload a chip (or even upload a sketch) the Arduino IDE checks that the chip selected matches the type it’s connected to – this is to protect the mcrocontroller from user error – if this check wasn’t in place and you accidentally select the wrong controller in the IDE and burn the bootloader you could “brick” your controller.
Even though the ATmega328-PU in essence functions in the same way as the ATmega328P-PU, it has a different signature, and one that isn’t recognised by the Arduino IDE. (Behind the Scenes: The Arduino IDE actually uses a utility called AVRDUDE to programme the chips, so you’ll see error messages from avrdude. (Adafruit have a good tutorial on avrdude)
The ATmega328 Bootloading Workaround
Find and open the folder that the Arduino IDE is installed in, then:
- open the subfolder ..\hardware\tools\avr\etc
- Make a backup copy of the file avrdude.conf
- Open the file avrdude.conf in a text editor – I use the cross-platform Geany as it formats the file better than Notepad.
- Search for: 0x1e 0x95 0x0F (this is the ATmega328P signature)
- Replace it with: 0x1e 0x95 0x14 (this is the ATmega328 signature)
- Save the file
- Restart the Arduino IDE
- Continue with the rest of the steps, and once bootloading is complete restore the backup copy you made.
You are now good to bootload ATmega328-PU microcontrollers as well!
Note: A number of people, including myself, have tried to add an ATmega328 entry into the avrdude.conf and boards.txt. This process will allow you to use avrdude from the command-line (quite daunting), but the Arduino IDE will not compile.
You can leave your setup as it is, and use the Arduino UNO to upload sketches to your newly bootloaded ATmega (File, Upload using Programmer).
The IDE will notice that the signature isn’t valid – so you’ll have to either alter the avrdude.conf file again or use an FTDI board to upload. I prefer using an FTDI board anyway as it doesn’t take my UNO out of circulation and is quicker to connect.
0 Comments on “Bootload an ATmega328P Microcontroller”