5.5. AVR programming using AVRDUDE

If you have an Orangutan or 3pi Robot or wish to use the Pololu AVR C/C++ Library for some other reason, we recommend following the Pololu AVR Programming Quick Start Guide instead of this tutorial.

While it is possible to program AVRs using an integrated development environment like Microchip Studio or the Arduino IDE, you can also set up your own development environment using a collection command-line utilities. This tutorial will explain how to set up such an environment on Windows, Mac OS X, or Linux. For each of these systems, the first thing we need to do is install AVR GCC, GNU Make, and AVRDUDE. In this tutorial, the phrase “AVR GCC” means the full toolchain, including the compiler, binary utilities (binutils), and AVR Libc. The instructions for installing these prerequisites depend on the specific operating system you are using. Once you have those tools, you can write a simple Makefile that supports compiling a program and loading it onto the AVR.

To successfully complete this tutorial, you will need to know how to use cd and ls to navigate the files on your computer. You will also need to know how to use a text editor to create and edit files.

Installing prerequisites in Windows

On a computer running Microsoft Windows, we recommend that you install the Microchip AVR 8-bit Toolchain for Windows, which is a standalone version of AVR GCC for Windows. The installer is just a simple extractor that will extract the toolchain folder to a location you specify. We recommend specifying “C:\” as the location, which means all the tools will be installed in “C:\avr8-gnu-toolchain”. To make it easy to use the toolchain, you should add “C:\avr8-gnu-toolchain\bin\” to the end of your PATH environment variable, making sure to separate it from any other paths using a semicolon. You can edit environment variables like PATH from the Control Panel.

To get GNU Make and AVRDUDE on Windows, we recommend using MSYS2, a distribution of open-source software for Windows. Be sure to follow the instructions on MSYS2’s website to update the packages that come with MSYS2. Then install AVRDUDE and make by running the following command:

pacman -S make mingw-w64-i686-avrdude

Next, launch MSYS2 using the “MinGW-w64 Win32 Shell” shortcut in your start menu (or by running mingw32_shell.bat). There are other ways to start MSYS2, and if you start it the wrong way, then the 32-bit version of AVRDUDE that we installed above will not be on your PATH, and you will get a “command not found” error later when you try to run AVRDUDE.

If you have trouble using the AVR GCC provided by Microchip or the AVRDUDE provided by MSYS2, another option is to use the versions of AVR GCC and AVRDUDE provided by the Arduino IDE. You would have to add the “hardware\tools\avr\bin” folder inside your Arduino installation to your PATH environment variable.

Installing prerequisites on Linux

GNU Make is very commonly used to build software on Linux, so if you are using Linux then you might have it already. You can run “make -v” in a shell to see if you have it. If you do not have make, you should install it using your system’s package manager.

Next, you should install the AVR toolchain and AVRDUDE. On Ubuntu, you can do this by running:

sudo apt-get install gcc-avr avr-libc avrdude

On Arch Linux, you can run:

sudo pacman -S avr-gcc avr-libc avrdude

For other Linux distributions, you should look in your distribution’s list of packages to find the relevant AVR development tools.

Installing prerequisites on macOS

To install the prerequisites on macOS, we recommend first installing Homebrew. Then run the following commands to install AVRDUDE and homebrew-avr:

brew install avrdude
xcode-select --install
brew tap osx-cross/avr
brew install avr-gcc

Checking the prerequisites

After you have installed the prerequisites, you should open your terminal/shell and try running each tool to make sure it is available. Run the following commands:

avr-gcc -v
make -v
avrdude -v

If you installed the prerequisites correctly, each of the commands above should print a version number and some other information about the corresponding tool.

Compiling a program

First, create a file named main.c with the following contents:

#define F_CPU 20000000    // AVR clock frequency in Hz, used by util/delay.h
#include <avr/io.h>
#include <util/delay.h>
 
int main() {
    DDRD |= (1<<1);        // set LED pin PD1 to output
    while (1) {
        PORTD |= (1<<1);   // drive PD1 high
        _delay_ms(100);    // delay 100 ms
        PORTD &= ~(1<<1);  // drive PD1 low
        _delay_ms(900);    // delay 900 ms
    }
}

The C code above attempts to blink an LED connected to pin PD1 of your AVR. If you do not have an LED connected to PD1, you should edit it to use a different pin. You should also adjust the definition of F_CPU so that it is equal to the clock speed of your AVR in Hz.

Next, create a file named Makefile with the following contents:

MCU=atmega328p
PORT=$(shell pavr2cmd --prog-port)
CFLAGS=-g -Wall -mcall-prologues -mmcu=$(MCU) -Os
LDFLAGS=-Wl,-gc-sections -Wl,-relax
CC=avr-gcc
TARGET=main

all: $(TARGET).hex

clean:
	rm -f *.o *.elf *.hex

%.hex: %.elf
	avr-objcopy -R .eeprom -O ihex $< $@

$(TARGET).elf: $(TARGET).o
	$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@

program: $(TARGET).hex
	avrdude -c stk500v2 -P "$(PORT)" -p $(MCU) -U flash:w:$<:i

You should edit the MCU variable in the Makefile to match the part name of the AVR you are using. This will be passed as an argument to both GCC and AVRDUDE.

If you did not install the software for the programmer, then you will not have pavr2cmd, and you should manually set the PORT variable in the Makefile to be equal to the name of the programmer’s programming port. See Section 4.5 for more information about determining port names.

You can now run make to build the program.

Note that this is a simple Makefile that does not have real dependency tracking. It knows that if main.c changes, then the other files need to rebuilt, but it does not account for changes in other files you might add, such as header files.

Programming the AVR

You can run make program to build the code and also program it onto your target AVR using AVRDUDE. If this does not work, see Section 5.6 for help troubleshooting.

AVRDUDE’s terminal mode (the -t option) is not compatible with the programmer because the programmer will exit programming mode and release the target AVR from reset if it receives no programming commands for 1500 ms.

Related Products

Pololu USB AVR Programmer v2
Pololu USB AVR Programmer v2.1
Log In
Pololu Robotics & Electronics
Shopping cart
(702) 262-6648
Same-day shipping, worldwide
Menu
Shop Blog Forum Support
My account Comments or questions? About Pololu Contact Ordering information Distributors