But when I received my Atmega32u4 Breakout Board+ I was a bit confused on how to get started programming it. The product page mainly covers hooking it up with the Teensydunio IDE but has little info on using programming in C and using gcc-avr, make and avrdude.
I presume a basic familiarity with using the Linux command line and how to install packages on your distribution. Some experience with programming C using make and basic electronics knowledge will might help but you can probably get through this tutorial without it. This tutorial is written for Linux users but it's probably not too different from people using WinAVR etc but I have never tried these so I cannot adapt it for it. Maybe a helpful forum user would be able to post amendments to make this work for other platforms.
This basic tutorial will cover getting all the right tools in place and hooking up an LED and writing, compiling and flashing a simple program that will flash the LED.
Requirements:
- Atmega32u4 Breakout Board+
- Mini-USB to USB cable to hook up the Breakout Board
- Breadboard
- LED and appropriate current limiting resistor*
- A computer running Linux
(5V - LED forward voltage drop) / LED forward current = resistance
Firstly, follow the product page up to the point where it mentions Teensyduino. You should have now have a Breakout Board with headers soldered and you can place it onto a breadboard and hook it up with your USB cable to your PC. You should see the red PWR LED light up.

Now on your computer make sure you have all the appropriate packages installed. You will need packages that contain:
- GNU Make
- avr-gcc
- avr-libc
- avrdude
If you have all of those then you can proceed. Make a new folder and cd into it in your terminal. Make a new file "main.c" and edit it to contain:
- Code: Select all
int main(void) {
return 0;
}
This program does nothing at all except return successfully and we will use it merely to test if all our tools are in place and working correctly.
To compile it we use gcc-avr:
- Code: Select all
$ avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
If this worked correctly you should now see a main.out file. Let's break this down: "-mmcu=atmega32u4" selects the AVR, "-Wall" tells the compiler to output all warnings, "-c main.c" selects the C file to compile and "-o main.out" selects the output file.
We will now determine what the serial interface on our Breakout Board. With the board still connected press the reset button on it. This should enable the bootloader on the Breakout Board and the green "Boot" LED should be dimming and lighting up (breathing). Now type "dmesg" in your terminal. You may have to do "sudo dmesg" depending on what privileges your distribution gives you. On the last few lines you should see something like this:
- Code: Select all
[ 208.440047] usb 5-1: new full speed USB device number 2 using uhci_hcd
[ 208.836738] cdc_acm 5-1:1.0: ttyACM0: USB ACM device
[ 208.839901] usbcore: registered new interface driver cdc_acm
[ 208.839906] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapte
What is important here is "ttyACM0" on the second line. This is the usb-serial port of the Breakout Board. If you have other usb-serial devices connected this may appear as "ttyACM1" or "ttyACM2" or even higher. You will need to to replace "ttyACM0" with whatever shows up for you in the steps following this.
Now we will use avrdude to upload your compiled file to the Breakout Board. Press the reset button again to make sure the bootloader is enabled and then invoke avrdude while the Boot LED is still green:
- Code: Select all
$ avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
You should see avrdude do it's thing and say "Thank you" at the end. Let's break this down: "-p m32u4" selects the type of AVR, "-P /dev/ttyACM0" selects the correct port, remember to change this to the port we determined earlier if yours is differemt, "-c avr109" selects the programmer id which is determined by the bootloader and main.out selects the file to write.
As we will be recompiling and re-flashing quite often, it makes sense to write a Makefile which will automate the task. Write a file called "makefile" that contains the commands we are using:
- Code: Select all
all:
avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
writeflash:
avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
Now you can compile by simply typing "make" and re-programm using "make writeflash". If you want to try this first remove the compiled file:
- Code: Select all
$ rm -f main.out
Then:
- Code: Select all
$ make
Then, press the reset button and run
- Code: Select all
$ make writeflash
You should see the same outputs as when we invoked avrdude directly. Let's add the removal to the makefile as well:
- Code: Select all
all:
avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
writeflash:
avrdude -p m32u4 -P /dev/ttyACM0 -c avr109 main.out
clean:
rm -f main.out
Now you can remove main.out by typing "make clean". This is needed sometimes if you make changes to the code and the compiler doesn't realize and may not recompile properly.
Now, this hasn't been very impressive as the code does absolutely nothing. Let's now hook up the LED to pin B0. The long, positive end of the LED goes into the port, then to the resistor and the resistor goes to ground.

Now let's change the code in main.c to blink the LED:
- Code: Select all
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRB = 0x01; //set up pin 0 on port B
while (1) //loop forever
{
PORTB = 0x01; //set pin 0 on port B high
_delay_ms(500);
PORTB = 0x00; //set pin 0 on port B low
_delay_ms(500);
}
return 0;
}
The first line set's the CPU frequency to 16Mhz which is what it is clocked at on the BReakoutBoard. The first include enables the IO pins and the second the delay function. The main function runs an infinite loop which turns the LED on and off delaying 500ms after each action.
We have to modify our makefile slightly to include compiler optimizations which the delay library needs. Change:
- Code: Select all
avr-gcc -mmcu=atmega32u4 -Wall -c main.c -o main.out
to
- Code: Select all
avr-gcc -mmcu=atmega32u4 -Wall -Os -c main.c -o main.out
Now you can run "make", press the reset and run "make writeflash" and you should see your LED blink! Celebrate!
There are people out there that are much better at writing makefiles than we are and it might be worth using a pre-written one instead. I recommend the one from psychogenic and with the following modifications it is usable for this project:
- Code: Select all
MCU=atmega32u4
PROGRAMMER_MCU=m32u4
PROJECTNAME=main
PRJSRC=main.c
AVRDUDE_PROGRAMMERID=avr109
AVRDUDE_PORT=/dev/ttyACM0

