NOTE: The note below is out of date! Instead of an mbed, I recommend a micropython-compatible board such as the Raspberry Pi Pico or BBC micro:bit.

NOTE: This page is old and out of date! I do not think hobbyists should be using AVRs any more, but if you want to, it is much easier now, since the relevant packages are available in standard linux distributions. Instead of an AVR-based board I would recommend getting a Pololu Wixel, which is about $20 and comes with wireless and USB, or an mbed, which has a powerful 32-bit processor, tons of peripheral features, and a web-based development environment that requires NO software or driver installation.</b> </p>

The following information is of historical interest only.

Introduction

The Atmel AVR family is a set of powerful microcontrollers that were designed to be used with the C programming language. You can now compile a C program for the AVRs with gcc, then load the program onto the chip using an inexpensive programmer and free tools. For Linux users who want to work on digital electronics, robotics, and automation, these chips are the way to go. This page is a step-by-step tutorial that presents the simplest, cheapest way to start experimenting with AVRs.

Specifically, on this page you'll find instructions for assembling and programming the AVR ATmega32 using a AVR-P40-USB prototyping board from Olimex (available at Sparkfun) and an AVR programmer that can be purchased from Digi-key. The ability to install programs on your Linux computer is the only skill required. It will cost you about $60 and 50M of disk space, and you'll be up and running about three hours after you unpack the electronics.

1. Order electronic parts

Order the AVR-P40-USB from Sparkfun; buy it with the USB extension cable - this will be used for power and serial communication (total price: $37.50). Also order the ATAVRISP-ND programmer for $29 from Digi-key. As long as you are putting in Digi-key order, you'll want to get some miscellaneous electronics for future experimentation. That is however beyond the scope of this tutorial.

picture of assembly

2. Plug in

After familiarizing yourself with your new electronics, attach the rubber feet to the prototyping board, connect the usb cable to your computer, and attach the programmer via serial port as shown. The LED on the programmer will cycle through several colors and stop on green if everything is okay.

3. Install software

You'll need a computer running linux to get gcc running; you could probably also set this stuff up under cygwin. I used Slackware 10, but the same instructions should work on any modern linux system.

Get binutils-2.16 from ftp://ftp.gnu.org/gnu/binutils/. Untar the archive and make a separate directory in which to build it. From this directory, install with

../binutils-2.16/configure --target=avr --prefix=/usr/local/atmel
make
make install

Get gcc-core-4.0.1 from ftp://ftp.gnu.org/gnu/gcc/. Again make a separate directory and install with

export PATH=/usr/local/atmel/bin:$PATH
../gcc-4.0.1/configure --target=avr --prefix=/usr/local/atmel --enable-languages=c
make
make install

Get the latest avr-libc.

./configure --build=`./config.guess` --host=avr --prefix=/usr/local/atmel>
make
make install

Get the latest Uisp, the program you will use to access the AVRISP. Install with

./configure
make
make install

Try a simple program

Save this code as blink.c:

#define F_CPU 10000000UL
#include <avr/io.h>
#include <avr/delay.h>

void delayms(uint16_t millis) {
  uint16_t loop;
  while ( millis ) {
    _delay_ms(1);
    millis--;
  }
}

int main(void) {
  DDRB |= 1<<PB0; /* set PB0 to output */
  while(1) {
    PORTB &= ~(1<<PB0); /* LED on */
    delayms(100);
    PORTB |= 1<<PB0; /* LED off */
    delayms(900);
  }
  return 0;
}

And save this as Makefile:

CC=/usr/local/atmel/bin/avr-gcc
CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=atmega32
OBJ2HEX=/usr/local/atmel/bin/avr-objcopy 
UISP=/usr/local/bin/uisp 
TARGET=blink

program : $(TARGET).hex
	$(UISP) -dprog=stk500 -dserial=/dev/ttyS1 --erase -dpart=atmega32
	$(UISP) -dprog=stk500 -dserial=/dev/ttyS1 --upload -dpart=atmega32 \
		if=$(TARGET).hex -v=2
%.obj : %.o
	$(CC) $(CFLAGS) $< -o $@

%.hex : %.obj
	$(OBJ2HEX) -R .eeprom -O ihex $< $@

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

With the programmer plugged in, type make. Your LED should now start to blink!

Further reading