How to use Arduino as AVR Programmer (for ATMega328p)

You can use an Arduino as an AVR Programmer to upload a program onto another miniprocessor microchip (in this document, an Arduino UNO and ATMega328p).

(Much of the following information can be found on arduino.cc’s tutorial pertaining to this topic. More of it can be found at high-low tech’s tutorial. Here I combine all the details necessary for getting away from the Arduino IDE for writing and compiling code. For the best read, I recommend the tutorial at Inside Gadgets.)

  1. Connect the programmer Arduino to the receiving ATMega chip
  2. Write the program
  3. Upload the program onto the receiving ATMega chip
    1. Visit the ArduinoISP file
    2. WINAVR and the Makefile
    3. Make and upload

Connect the Arduino to the ATMega chip

According to the image above, connect the Arduino to the ATMega328p:

(click image to enlarge)

  • pin 1 (Reset): connect to ground via a 10KOhm resistor; also connect to pin 10 on the Arduino
  • pins 7 & 20 (VCC & AVCC): connect to +5V
  • pins 8 & 22 (GND): connect to ground
  • pins 9 & 10 (X1 & X2): connect to a 16MHz crystal; also attach both leads on the crystal to ground, through two 10uF ceramic capacitors.
  • pin 17 (D11): connect to pin 11 on the Arduino
  • pin 18 (D12): connect to pin 12 on the Arduino
  • pin 19 (D13): connect to pin 13 on the Arduino

Remember that Ground on the ATMega328p (receiving programming) must be connected to Ground on the Arduino (programmer).

Write the program

You won’t define the methods setup() and loop() as you did in the Arduino IDE. Instead, just define main(), as you would with any C or C++ program.

The following code will replicate the Blink program for the Arduino. (Plug an LED into D9 of your ATMega328p to see it work after we compile and upload it.)

#include <avr/io.h>
#include <util/delay.h>

#define ledPin PB1 // pin 9

int main(void)
{
  DDRB |= (1 << ledPin); // pinMode WRITE

  while(1)
  {
    PORTB |= (1 << ledPin); // digitalWrite HIGH
    _delay_ms(400);
    PORTB &= ~(1 << ledPin); // digitalWrite LOW
    _delay_ms(400);
  }
  return 1;
}

Upload the program to the ATMega328p

The ArduinoISP file

In the Arduino IDE, you can find an entry named ArduinoISP under File > Examples. This sketch must be uploaded onto your programmer Arduino. Before you upload it, though, we need to change one line in the file. The Arduino IDE won’t let you save changes to this file, so you’ll have to open it in an ordinary text editor.

Find the definition of void heartbeat() and change the delay(40) (on line 98) to read delay(20).

Alright. Save changes. Restart the Arduino IDE, and upload this sketch onto your programmer Arduino.

WINAVR and the Makefile

You can download WINAVR for free. It’s an IDE you can use to manage your projects, compile, and upload them. (I actually just use it for the sake of its sample Makefile. With the help of this Makefile, I use Mingw to compile and upload my projects.)

Whatever you do, find the sample Makefile in the ‘sample’ subdirectory of the WINAVR program folder. Copy it, and paste it into your own project folder. (If you haven’t used make and Makefiles before, that goes beyond the scope of this tutorial, so you’ll need to do some outside research.)  There are a few adjustments we must make to this Makefile:

Find the definition of F_CPU and set it to match the speed of your clock (crystal): F_CPU = 16000000

Find the definition of MCU and set the identity of your receiving miniprocessor: MCU = atmega328p

Find the definition of AVRDUDUDE_PROGRAMMER and set the identity of your programmer (Arduino): AVRDUDUDE_PROGRAMMER = stk500v1

Find the definition of AVRDUDE_PORT and set it to the port where your programmer Arduino is attached to your computer (for me, always COM8): AVRDUDE_PORT = com8

Compile and Upload

Using the (altered) sample Makefile, we have only to make use of two tasks. In WINAVR, under Tools, select Make (all). This compiles the project to the appropriate format for the miniprocessor. Then under Tools, selec Make (program). This will upload the program onto the receiving miniprocessor.

You are done.

Join the Conversation

2 Comments

  1. Hi, I follow the tutorial step by step, I made the LED connected to pin9 on the Arduino to blink, but not the LED that was connected to the chip (PB1) to blink. Does the standalone chip need a bootloader?

    1. I assume that it does. I got mine from Sparkfun, who sends out all their processor chips with bootloaders burnt first. (I don’t have any truly blank ones to allow me to check.)

Leave a comment

Leave a Reply to AaronJu Cancel reply

Your email address will not be published. Required fields are marked *