Programming an Arduino is easy and a lot of resources are available online. The Arduino boards contains ATMEL AVR 8 bit microcontrollers. Arduino Uno, Nano and Mini Uses the ATmega328p chip as the main processor. These boards are custom bootloaded that makes the programming easier. However, we can burn that bootloader into ATMEL Chips manually that would allow us to run Arduino code into AVRs directly which is very cost efficient compared to an Arduino. The process for burning bootloader into ATmega328 (0x1e9514) is slightly different.
What is the difference between ATmega328 and ATmega328p?
The “P” in ATmega328p stands for Pico-Power Technology. It means this chip can run with minimal power. It turns of peripheral modules that hasn’t been utilized by the code and thus saves power. The pin configuration and other features are identical. we can run the same code these two devices but the problem occurs when the device is called by it’s signature because the two devices have two different identities. ATmega328 has device signature 0x1e9514 and ATmega328p has device signature 0x1e95of. So we need to know which chip we are using when programming it through the Arduino IDE. The Arduino IDE is configured for ATmega328p so when we use ATmega328 we need to edit the “avrdude.conf” file. The procedure for burning bootloader and uploading program into the chip for the two variants has been described below.
What is Bootloader and How to Upload it?
Bootloader is a small program that runs in Arduino which receives data from pc and writes it to Arduino memory. By using this method we can easily program an Arduino without using any special hardware. Programming AVR boards require using UBSAsp programmer. But the bootloader utilizes the In System Programmer(ISP) to program the chip.
Bootloader Burning into ATMega328p
The bootloader Uploading process is simple. First You have to Upload a sketch to the Arduino called “ArduinoISP” which you can find into the Arduino IDE Examples. This sketch tells the main processor to act as a programmer. The codes coming into the processor are not for itself. Now, go to Arduino IDE > Tools > Programmer > Arduino as ISP. Hit “Arduino as ISP” this is the hardware setup for Arduino to behave as a programmer.
After that prepare the chip on which you are going to burn the bootloader. You will need the following components.
- ATmega328p Chip
- Crystal Oscillator (16 MHz)
- 2x 22pF Ceramic Capacitor
The Arduino bootloader requires a 16 MHz Crystal Oscillator. If you don’t use this you will get the following error.
avrdude: Device signature = 0x000000 avrdude: Yikes! Invalid device signature.
The crystal oscillator is the heart of the Arduino and without it the Arduino is dead. So, it return the following error. Using the ceramic capacitors are recommended but I found the system working without using them. It time to build the circuit. Build it as follows:
Now, Arduino IDE > Tools > Burn Bootloader and hit the “Burn Bootloader” Buttton. If the process is is successful then you will see the message “Done Burning Bootloader”.
avrdude: Expected signature for ATMEGA328P is 1E 95 0F
You will get this error if you are using ATmega328. Because, Arduino UNO/NANO bootloader is written for ATmega328p that device signature 0x1e950f. So, we have to change the device signature to 0x1e9614. Firstly, navigate to the Arduino Installation folder. In my case, it is “C:\Program Files (x86)\Arduino\hardware\tools\avr\etc“. Copy the file “avrdude.conf” to somewhere for safety. If there is any problem, you can restore it anytime. After that, open that file using “notepad ++” or any text editor. Press “CTRL+F” to open the find window and type “0x1e 0x95 0x0f” . You will find two occurrences. Replace them with “0x1e 0x95 0x14“. Save the file. Now, do the same thing you used to burn bootloader to atmega328p.
Note: You need to change the signature back to “0x1e 0x95 0x0f” when you will you use your Arduino. Because your Arduino still has the atmega328p chip. We used “0x1e 0x95 0x14” to make Arduino believe that the device we are going to bootload is the required device. Additionally, you have to change the programmer to “AVRISP mkll”. This programmer programmers the main programmer in the Arduino board.
Upload Programs to Custom Bootloaded ATmega328 chip
This process is same for both of the chips. Plug an LED to Digital Pin 13. You can find it from the figure below. Open the blink sketch. Go to, Arduino IDE > File > Examples > Basics > Blink. Check the programmer. It should be “Arduino as ISP”. After that, Sketch > Upload Using Programmer.
You can find the physical pin number of ATmega328 for Arduino digital pin 13 from the picture above. It is pin 19.
Note: Don’t get confused between selecting the programmer. When you are burning or uploading code to the external atmega328/atmega328p chip use “Programmer > Arduino as ISP”. But choose “Programmer > AVRISP mkll” when you are uploading the code to the Arduino itself.