We are going to learn how to program an Atmel chip, using only an Arduino and the ISP programming method. This method works (to my knowledge) with every Atmel chip and every Arduino board, as long as the chip has SPI communication : ATmega, ATtiny, Arduino pro mini, etc…
Why not an Arduino in each project ?
In my opinion, Arduino are really cool, but they are a prototyping component, just like the breadboard. When the prototyping stage is over, things have to move on a more permanent and specialized board, and that frees the Arduino for the next prototype.
It has a lot of advantages :
– it’s economic : the ATmega chips cost a fraction of the Arduino you’re are using to reprogram them,
– it is easy to do, when you know how to do it
– it is a nice way to learn about electronics.
This article covers in details ATmega328P-PU (on his own oscillator to save components) and Arduino Pro Mini 3.3v 8MHz programming, but one can reproduce the method with every Atmel SPI chip or Arduino board without a USB.
You will need :
– an Arduino (Uno in my exemple)
– an ATmel chip with SPI : ATmega, ATtiny, Arduino
– a breadboard
– some jumper wires
- Step 1 : Preparing the software
For the software side, you will need :
– the Arduino IDE (1.0.5 is working well)
– the chip bootloader (not needed for an Arduino pro mini, or an already bootloaded ATmega328 chip)
– a special arduino sketch to make the Arduino act like a programmer.
The bootloader
What is a bootloader ?
The bootloader is a kind of BIOS for microcontroller. It handles the low-level settings of the chip. Each chip has his own bootloader version, so an ATmega328-PU bootloader is not compatible with an ATmega328P-PU one (notice the added « P » for Pico-Power. So, when you want to program a chip, first make sure you downloaded its bootloader if needed, and set it up the way you want it.
This time, the bootloaders we are using are already part of the Arduino IDE. We just have to add a new board to the list, for the ATmega328 3.3v 8MHz. Other chips could require a few extra steps, we’ll cover this part in a future article.
Open your boards.txt config file located in %ARDUINO PROGRAM%\hardware\arduino\ with a text editor and add this to the end of the file :
############################################################## atmega328_8.name=ATmega328 (3.3v, 8MHz) atmega328_8.upload.protocol=arduino atmega328_8.upload.maximum_size=32256 atmega328_8.upload.speed=38400 atmega328_8.bootloader.low_fuses=0xE2 atmega328_8.bootloader.high_fuses=0xde atmega328_8.bootloader.extended_fuses=0x05 atmega328_8.bootloader.path=optiboot atmega328_8.bootloader.file=optiboot_atmega328.hex atmega328_8.bootloader.unlock_bits=0x3F atmega328_8.bootloader.lock_bits=0x0F atmega328_8.build.mcu=atmega328p atmega328_8.build.f_cpu=8000000L atmega328_8.build.core=arduino atmega328_8.build.variant=standard
Now, if you restart your Arduino IDE, you should see your new « board » in the list : ATmega328 (3.3v, 8MHz)
The Arduino Pro Mini is already in the list : Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328
The Arduino sketch Our Arduino Uno needs a special sketch to be able to act as a programmer. Unfortunatly, the ArduinoISP sketch provided with the Arduino IDE doesn’t seem to work, so there is a another, better one available : mega-isp Download this sketch, unzip it in you sketchbook directory and restart your IDE. Now, you could upload this sketch into your Arduino the normal way.
You are now ready with the software part.
- Step 2 : now, lets make the wiring
Nothing really difficult here, when you know how to conect two chips with SPI :
Arduino programmer | ATmega328P | Arduino Pro Mini |
10 | PC6 (RESET) | RESET |
11 (MOSI) | PB3 (MOSI) | 11 (MOSI) |
12 (MISO) | PB4 (MISO) | 12 (MISO) |
13 (SCK) | PB5 (SCK) | 13 (SCK) |
It should give you this result :
You should choose the right voltage for your chip, (3-5V for ATmega328P-PU or 3-12V for the Arduino Pro Mini 3.3v). The ground has to be common.
In the ArduinoISP code, one can notice some extra components could be added for debuging purposes, but they are not needed.
Your hardware setup is ready, go to the last part.
- Step 3 : Programming the chip
Now your software and you hardware are ready, plug the programmer Arduino in the computer and start the IDE. You should see your Arduino appear in the COM ports like normal. If it is not the case, unplug it, exit every IDE windows, plug it back, and restart the IDE.
Now, in the Tools menu, you have to choose the right chip and the right programmer. So, if you are programming an ATmega328, choose the newly created ATmega328 (3.3v, 8MHz), if you are programming an Arduino Pro Mini, choose the Arduino Pro or Pro Mini (3.3V, 8 MHz) w/ ATmega328 (or the 5V mini if you are using the 5V version, of course…)
In the programmer menu, you should now choose Arduino as ISP.
Now, you can burn the bootloader : just click on ‘burn bootloader‘ in the Tools menu. After a few seconds and a lot of activity on the Arduino, the IDE should show you a nice :
avrdude: verifying ... avrdude: 1 bytes of lock verified avrdude: Send: Q [51] [20] avrdude: Recv: . [14] avrdude: Recv: . [10] avrdude done. Thank you.
You are done with the bootloader.
Now, you can upload your sketch in you chip the normal way, using the ‘Upload with a programmer‘ option in the File menu. If you try to use the regular ‘Upload‘ button, you would obtain a ‘not in sync: resp=0x00’ error message.
If everything went well, you should this nice :
avrdude: verifying ... avrdude: 1108 bytes of flash verified avrdude: Send: Q [51] [20] avrdude: Recv: . [14] avrdude: Recv: . [10] avrdude done. Thank you.
Congratulations, you are now able to build your own Arduinos on breadboards, perfboards and design your own PCBs !!! But when you will build your first one, don’t forget to add a programming socket to your board, if the chip is soldered 😉