Programming AVR chips-“Open” way!

Phew!!!!! Because quite a few people asked me how to do programming in AVR microcontrollers.  There may be the case that they are starting up all this or they are switching from another microcontroller to AVR now [though it is old too 😦  or you may say obselete…]  So, here I am going to explain in short and working way to “make” the things up.

Obviously this tutorial is for linux users, OPEN SOURCE advocates, so we will be learning how to program AVR chips in linux environment open source way.  We will be using Open source tools/techniques and softwares.  The programmer AVRDUDE, the toolchain, VIM editor, usbasp programmer; All are Open Source.  One can use Windows being loyal to Microsoft, (Ughhhh,, I am not going to get a job there if I praise them or boast about them…ooopss…is  there is anything to boast upon for them… 😉 lolzzz..)  Really, In case of windows things works `I DONT KNOW HOW & WHY` and one actually needs to do nothing to make the program compile and burn and thus getting no knowledge about the things being done, what all is involved, who is doing what, etc. etc. etc. So, in case of linux systems, programming AVR is really easy NOW because of some lazy linux users.  People want that they can program in linux without getting much of the pain.  (Yeah, you are thinking right, I am saying for the scary compilation of the toolchains).

One has to make the binary utilities ready for cross compilation, gcc for AVR architecture, avrdude, avrlibc, simulAVR, gdb for avr before starting to think to program AVR in linux.  Frankly saying, I was screwed up when I compiled the  toolchain for the first time.  I referenced 4-5 tutorials for it even then it took a lot of time.  Thanks to Atmel AVR because they provide toolchain for AVR architecture both 8 bit and 32bit.

One can simply download the toolchain, and start working out.  For very obvious reasons, things are not as easy as they appear to be.  Toolchain contains essential binutils and avr-gcc, avr-objcopy etc. but the two most important things are still left.

  • avrlibc
  • avrdude

So, download avrlibc from here :

Avrdude from here:

Go for our favorite method of building program from source code, (obviously iff you like it, you may opt for direct use of it, but I just like to make things work from as low level as I can do).Download the avr-libc from the link provided, and just unzip it, then go for compiling 1.AVRLIBC:

  • $ cd avr-libc-<version>
  • $ ./configure –build=`./config.guess` –host=avr
  • $ make
  • $ sudo make install

now here, config.guess is intelligent script that checks for your systems and configures itself.  Rest is self-explanatory. Or download directly in case of package managers. Ubuntu/debian:

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

Fedora/Red hat: yum install avr-gcc avr-binutils  avr-libc avr-gdb avrdude eclipse eclipse-cdt


  • $ cd avrdude-<watsoever-version>
  • $ mkdir build
  • $ cd build
  • $ ../configure
  • $ make
  • $ make install

I am using a seperate directory build just because to seperate the build noise from the pre-existing files. Add the downloaded toolchain and go to the bin folder inside that.  Just add the PATH of this folder containing the binaries to the system PATH

  • export PATH=$PATH:/your/path/comes/here

Now, this has to be done everytime you do a reboot or end a session.  To get rid of the problem you can simply add it to you startup scripts or bash profile etc. etc. Now since, we are having every necessary tool, so now we can compile a C program for our AVR architecture and burn to our Atmega chip.  Write a simple program like that of led blinking like this,

int main()
return 0;

Save it with some name.c and now exit from vi/vim/nano editor.  I always use VIM.  On the terminal one simply need to do three simple and obvious things.  Compile the file, make the `proper binary`, burn it to the chip.

–Compiling the file 

  •  avr-gcc -mmcu=atmega16 -Os servo.c

executing avr-gcc and specifying the microcontroller used is atmega16, giving the optimising level, and specifying the file and thus our a.out will be generated.

 –Generating the hex file

  • avr-objcopy -j .text -j .data -O ihex a.out a.hex

Executing the avr-objcopy program to make the proper file in Intel hex format, where ihex is the format specifier here.

–Burning to the chip

  • avrdude -p m16 -c usbasp -U flash:w:a.hex

Avrdude is the one which communicates with the burner/programmer and actually writes the data to the chip. Specifying the controller with -P option, and specifying the burner/programmer type with -C option and writing in flash the a.hex file.NOTE:-You may get this error just because you haven;t connected usb asp correctly.  Or you may get RC=-1 error which simply means that you have connected the programmer the wrong way.  Generally it indicates the hardware wiring error.  Double check the pinout and wiring to burn. Thus, you got your led blinking program written successfully in the chip and working!!

You may want to purchase the USBASP because I have found it the most convinient for burning AVR chips.  You may make your own following the Fischel or purchase from us.  Any time order !! Bulk order are highly appreciated !


About Beyond

an electronics hobbyst, 8 bit microcontroller(8051/AVR/Arduino) programmer, Linux lover, in love with Embedded Linux systems ... TGL: just push it "BEYOND THE LIMITS"..

Posted on October 6, 2012, in 8-Bit Embedded World, Embedded Systems, Linux and tagged , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s