soch.cc

← back to posts

Programming AVR chips with an Arduino

This is a quick reference on programming any AVR microcontroller using any Arduino as an ISP.

Required Materials

Hardware

Software

Step 1 - Flashing the Arduino

This step sets up the Arduino as an ISP. The easiest way to do so is with the Arduino IDE. Simply load the “ArduinoISP” sketch and upload it to the board. This can be found in the examples. Otherwise you can simply copy and paste it from this gist.

Step 2 - Wire AVR microcontroller to the Arduino

Here’s a table of the connections. Refer to the pinouts from the respective datasheets to findout which pins correspond.

Arduino AVR
VCC VCC
GND GND
SCK SCK
CIPO CIPO
COPI COPI
SS (Pin10 on UNO) RESET

Step 3 - Compile

Below is an example command to compile a C source file into an ELF to the attiny85.

avr-gcc -Os\
    -ffunction-sections -fdata-sections\
    -Wl,--gc-sections\
    -DF_CPU=1000000UL\
    -mmcu=attiny85\
    main.c -o main.elf 

Explanations:

Step 4 - Convert ELF to HEX

The command below transforms the compiled ELF into an intel hex binary format, removing the .eeprom section in the process. This is the format required by AVR chips.

avr-objcopy -O ihex -R .eeprom main.elf main.hex

Step 5 - Flash

This is the last step. The command below uses avrdude to flash the hex file onto the microcontroller.

avrdude -c avrisp -p t85 -P /dev/ttyACM0 -b 19200 -U flash:w:main.hex:i

Explanations:

Note: depending on your system, you may have to specify an avrdude.conf file with the -C flag. On my system, installing avrdude created an avrdude.conf in /etc. Installing the Arduino ide created one in /usr/lib/arduino/hardware/tools/avr/etc/avrdude.conf. If you’re on a UNIX based system, you can use find / -iwholename "**/**/avrdude.conf" to find this file. This file is essentially a mapping of programmers, part numbers, baud rates and so on.

It is also in this step where you can set the fuse bits to change the clock divider of the AVR chip’s CPU.

Resources