Support » Pololu USB AVR Programmer User’s Guide » 4. Getting Started in Linux »
4.b. Programming AVRs in Linux
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.
To program AVRs in Linux using the Pololu USB AVR Programmer, you will need to install four software packages, which can be downloaded from their respective websites. In Ubuntu Linux, these packages are provided in the “Universe” repository.
- gcc-avr: the GNU C compiler, ported to the AVR architecture
- avr-libc: a library giving access to special functions of the AVR
- binutils-avr: tools for converting object code into hex files
- avrdude: the software to drive the programmer
Once these packages are installed, you will be able to compile C programs for the AVR with gcc to produce hex files. These hex files can be loaded on to your AVR using avrdude and a programmer.
We will not go into the details of writing C programs for the AVR here, but, as an example, we will show you how to use your Linux computer and the USB AVR Programmer to make an LED connected to PD1 of an AVR blink. On any of the Orangutan robot controllers and the 3pi Robot, this program will blink the red user LED. If you want to program an AVR that does not have an LED connected to pin PD1, the LED-blinker code in this tutorial will have no visible effect.
If your device is an ATmega48, ATmega168, or ATmega328P, download the corresponding archive below:
- mega48: BlinkLED_m48.zip (9k zip)
- mega168: BlinkLED_m168.zip (9k zip)
- mega328: BlinkLED_m328.zip (9k zip)
If your device is not one of the above, you will need to download one of the above archives and modify the makefile to use your particular device.
Unpack the archive on your Linux computer. Copy the file BlinkLED/linux/Makefile
into the BlinkLED/
directory. You will need to edit this file. Change all instances of “/dev/ttyUSB0” to the name of the programming port device, usually /dev/ttyACM0. Additionally, it may be necessary to change the settings at the beginning to reflect the locations where the AVR utilities were installed.
Note: You will probably want to edit BlinkLED.c slightly if the clock frequency of your AVR is not 20 MHz. F_CPU should be defined as the clock frequency of your AVR in units of Hz. If you do not make this change, the timing of delayms() will be off, but the LED will still blink.
At this point, you should be ready to compile the example program and load it on to the AVR. Plug in the programmer and type make. You should see output like this:
/usr/bin/avr-gcc -g -Os -Wall -mcall-prologues -mmcu=atmega168 -c -o BlinkLED.o BlinkLED.c
/usr/bin/avr-gcc -g -Os -Wall -mcall-prologues -mmcu=atmega168 BlinkLED.o -o BlinkLED.obj
/usr/bin/avr-objcopy -R .eeprom -O ihex BlinkLED.obj BlinkLED.hex
/usr/bin/avrdude -c avrispv2 -p m168 -P /dev/ttyACM0 -e
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.08s
avrdude: Device signature = 0x1e9406
avrdude: erasing chip
avrdude: safemode: Fuses OK
avrdude done. Thank you.
/usr/bin/avrdude -c avrispv2 -p m168 -P /dev/ttyACM0 -U flash:w:BlinkLED.hex
avrdude: stk500_2_ReceiveMessage(): timeout
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.03s
avrdude: Device signature = 0x1e9406
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "BlinkLED.hex"
avrdude: input file BlinkLED.hex auto detected as Intel Hex
avrdude: writing flash (224 bytes):
Writing | ################################################## | 100% 0.39s
avrdude: 224 bytes of flash written
avrdude: verifying flash memory against BlinkLED.hex:
avrdude: load data flash data from input file BlinkLED.hex:
avrdude: input file BlinkLED.hex auto detected as Intel Hex
avrdude: input file BlinkLED.hex contains 224 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 0.05s
avrdude: verifying ...
avrdude: 224 bytes of flash verified
avrdude: safemode: Fuses OK
avrdude done. Thank you.
rm BlinkLED.o BlinkLED.obj
This output indicates the AVR was successfully programmed. The LED connected to PD1 of your AVR should now be flashing! Note that if you are trying this on a 3pi robot and you have not yet soldered in the optional through-hole LEDs, the flashing LED will be on the bottom of the robot. If there was a problem, please see Troubleshooting (Section 8) for help identifying and fixing it.