Tutorial: Using AVR Studio 5 with Arduino projects

AVR Studio Welcome Page

Note: This tutorial has been replaced with an updated version that covers the same topic with Atmel Studio 6. Studio 6 makes a lot of improvements over the prior version, so there’s really no reason not to upgrade unless you have a very specific need. We’ve also incorporated a lot of fixes, tips, and great user feedback. Check it out here:

Tutorial: Using Atmel Studio 6 with Arduino projects

This article explains, step-by-step, how to set up the AVR Studio 5 IDE for use with Arduino projects.  It also includes some background on the pros and cons of working with AVR Studio, notes on general setup for working with Atmel devices, and a few other tips we’ve picked up along the way.  Feel free to skip around to the sections that interest you.

Introduction

My my, what a temperamental beast we are.  AVR Studio 5 is an Integrated Development Environment (IDE) fresh off the presses from Atmel in 2011.  On paper, it seems like a great value.  A single dev environment for all AVR’s, integrated avr-gcc toolchain, and much of the functionality of Microsoft Visual Studio, all for the low, low price of free.  The question: does it live up to all of these claims in reality?

The answer: well, partially.  The environment is quite well done, with a modern interface and some of the niceties you may be used to such as auto-complete and some nifty function and solution explorers.  However, it seems Atmel has taken a few steps backward since the release of AVR Studio 4.  Getting projects to build and link correctly can be quite the adventure, and the integrated toolchain caused more problems that it’s worth, at least in our testing.  Overall, it’s a nice upgrade from the relatively ancient AVR Studio 4, and a good alternative to Eclipse and other IDE’s available for AVR.  Just don’t expect it to run smoothly until you learn the ins and outs.

Why should I switch?

A primary use case for AVR Studio has always been to serve users that have grown out of the integrated Arduino IDE.  The Arduino IDE does so much under the hood that it can actually be quite limiting for experienced programmers.  All compiler warnings are suppressed, without even the option to display them?  Really?  As much as we love Arduino, the EngBlaze team would like to meet the person who made that design decision and give them a stern talking to.  Beyond that, there are really no debugging capabilities whatsoever (Serial.println() after every statement doesn’t count), and not much flexibility in configuration or build options.

AVR Studio is a huge step up from those limitations, but for many, making the switch cold turkey is just that: a huge step.  If you have a big project, porting the entire thing to pure C can be a daunting task.  Plus, some of those Arduino libraries are just so darn convenient. Software serial, anyone?  Ethernet capability?  Aw yeah.

So why not have the best of both worlds?  Arduino is basically a wrapper on top of C/C++ anyway, so technically, it’s possible to combine any Arduino sketch or library with your own custom code.  The trick is in setting up your project properly.  Here are the steps to create a fully functional Arduino project in AVR Studio 5.  Once accomplished, you can keep access to the huge Arduino user-contributed code library, but enjoy all the features of advanced AVR and a real IDE.

Preparing AVR Studio and the Arduino core library

  1. First, a few preparation steps.  This tutorial assumes you have AVR Studio installed already.  To start, you need to install the C++ extension for AVR Studio.  You’ll find it under “Tools”->”Extension Manager”->”Online Gallery”.  Install, restart, and you’re good to go.
  2. Next, you’ll need to give AVR Studio access to the Arduino core library, which is hidden by the Arduino IDE by default.  Take the Arduino project you’re looking to convert (or any Arduino project) and compile it.  Then, starting in your Windows user folder, enter the AppData folder, then Local, then Temp.  AppData is hidden by default, so change this if needed in Windows Explorer->”Folder and Search Options”->”View”- >”Show hidden files and folders”.  On Windows Vista or 7, the path should look like this:  C:/Users/<MyUser>/AppData/Local/Temp/
  3. Find a folder called “buildxxxxxxxx.tmp” where “xxxxxxxxx” is a long string of numbers.  This is where the Arduino IDE stores all of its compiled code before flashing it to your chip.  The file you’re looking for is called “core.a”; that’s the core Arduino library.  Copy it and paste it into your development folder or somewhere you’ll remember (for example: C:/arduino/).  Next, rename “core.a” to “libcore.a” so that AVR Studio recognizes it as a standard library.
  4. If you’re converting an existing project called <project name>, open the <project_name.cpp> file from the bldxxx.tmp directory in a text editor.  You can simply copy and paste this code into your AVR Studio project to speed things up in a minute.
  5. Sweet, we’re ready to switch environments.  Open AVR Studio and create a new AVR Studio C++ executable project.  Remember to name it in the wizard that pops up, because it’s a pain to rename a project later.
  6. Copy and paste the source code from the compiled .cpp file you opened earlier into the project’s main .cpp file.  You can also copy/paste the source from your Arduino .pde sketch.  The first option is easier because it includes necessary function prototypes that the Arduino IDE automatically generates, while in the latter case you must add them yourself.  In either case, you have to have these prototypes, or else the compiler will complain that every single function is “not declared in this scope” when you try to build your project.  This is one of those things that the Arduino IDE hides from you.  Adding your own prototypes is a no brainer once you’re used to it, and it can actually save headache because automatic generation can cause problems in more advanced projects.  You’ll also need to #include "WProgram.h" if it’s not there already, or "Arduino.h" instead if you’re using Arduino 1.0.

Compiler and linker setup

Project toolchain and build properties in AVR Studio

  1. Now we have to tackle proper compiler setup and linking.  Go to “Project”->”<ProjectName> Properties” (or press Alt-F7), then click on “Toolchain”.  We need to setup a bunch of compiler options here.  In the left-hand menu, under “AVR/GNU C++ Compiler”, click on “Symbols”.  You’ll need to define your CPU speed for the compiler, so add "F_CPU=16000000L" to “Defined symbols”, where the number is changed accordingly for your chip (e.g. F_CPU=8000000L for a 8Mhz chip, etc).  Note: As CraigKC points out in the comments, you may also want to add  a symbol for your Arduino version, e.g. "ARDUINO=100".  This may help with compatibility when using libraries from the recent Arduino 1.0 release, but it won’t hurt if you add it to a project using an older version.  You can then change it to match any future versions.
  2. Next, click on “Directories” immediately under “Symbols”.  Add the directory for the Arduino hardware core source code, which will be in your Arduino install directory (for example: C:arduino-0022hardwarearduinocoresarduino).  Make sure all paths are non-relative.
  3. Add the folders for any Arduino libraries you want to include in your project (SoftwareSerial, Ethernet, etc.).
  4. Click on “Optimization” immediately under “Directories”.  Choose “Optimize for size” under “Optimization Level“.  Add "-fdata-sections" under “other optimization flags”, and check the box for “prepare functions for garbage collection”.  Next, click on “Miscellaneous” in the same list, and add “-fno-exceptions” to the “Other flags” field.  Keep in mind that the Arduino IDE keeps all other optimization flags off – feel free to leave the other default boxes in “Optimization” checked as they may improve program size, but if you’re having build problems, try turning them off.
  5. Still in Toolchain options, go to “AVR/GNU C++ Linker”->”Libraries”->”Library Search Path” and add the path to libcore.a that you renamed earlier.
  6. Click on “AVR/GNU C++ Linker”->“Optimization”, and check the box for  “Garbage Collect unused sections (-Wl, –gc-sections)”.  This ensures that the linker will leave out unused portions of the library, keeping your final code size down.  (Thanks to CraigKC in the comments for the tip!)
  7. Below the “Optimization” tab, click on “Miscellaneous”, and add “-Wl,-lcore  -Wl,-lm” to the “Other linker flags” field.  This tells the compiler to link libcore.a and also libm.a, the avr-gcc math library, which is needed by certain Arduino functions.
  8. We’re almost done!  Here’s where AVR Studio gets ornery.  Technically, we should be about ready to build our project.  At compile time, AVR Studio constructs a nicely formatted Makefile for us that tells the compiler what to do.  If you’ve ever messed with Makefiles before, you know how valuable this is – we’re talking depression-avoiding, marriage-saving, homicide-preventing valuable.  However, it doesn’t quite get things right.  Go ahead and try to compile by hitting F7 to build your project, so that AVR Studio builds our Makefile.  You should get “undefined function” errors for every single Arduino function in your project; this is OK.  These errors are because AVR Studio puts the link to libcore.a in the wrong place, so the linker doesn’t find it when it needs it.
  9. To fix this, we need to click on the “Build” tab in our project options, and check “Use external Makefile”.  Browse to the Debug subdirectory in your AVR Studio project folder, and choose the “Makefile” there.  This is the same automatically generated Makefile as before, but by specifying it externally, AVR Studio won’t automatically edit it anymore.  Open that Makefile in an editor and find the lines below "# AVR/GNU C/C++ Linker".  You’ll see a command to avr-g++.exe followed by a lot of flags, including the "-Wl,-lcore  -Wl,-lm" you specified earlier.  Cut "-Wl,-lcore  -Wl,-lm" and paste it to the end of that command line, normally just after the “$(LIBS) variable.
  10. Remember, if you change compiler or toolchain options, you’ll need to uncheck “Use external Makefile”, build your project so that AVR Studio generates a new Makefile, then repeat Step 9 to get your build working again.  This is a known bug in Studio 5 but the fix takes only a couple of seconds, so it’s not too bad.
  11. One last thing.  Now that our compiler is all set, we also need to add the .cpp source files for all of our linked libraries to the project.  We can do this by going to “Project”->”Add Existing Item”, and finding the Arduino hardware core .cpp files.  Select them, but instead of clicking “Add”, click the small arrow next to it, and click “Add as link”.  You’ll notice the file show up in your Solution Explorer with a small shortcut icon next to it.  At a minimum, you’ll need to do this for Print.cpp, WString.cpp, and HardwareSerial.cpp if you’re using them, and any other core libraries used by your project.  You’ll also have to link to any custom libraries you’re using.  I’m not sure why AVR Studio makes you include these headers, link to the files in the compiler, and link to them in the project, but it does.  If any readers know a better way to do this, let me know.

Build your project

  1. At last… at this point, your environment should be completely set up.  Hit F7 to build your solution and watch the output.  Is it successful?  If so, awesome.  If not, take a look at the errors to see if there’s a problem with the code or if it’s likely a problem with the linked files.  Also take a look at the beautiful new world of compiler warnings you’ve uncovered.  If your project is at all complex and you’ve only compiled using the Arduino IDE previously, you’ll likely have at least a few warnings.  These warnings won’t necessarily break your program, but they could.  Eliminating them usually means you’re following best practices.  There are a few warnings generated by the Arduino core libraries – if you’re feeling adventurous you can alter that code, but everything will work fine if you leave them alone.

Configure AVRDude to flash your compiled code

  1. Once you have a successful build, you have to get it onto your device.  Luckily, you can do this with the exact same hardware that you use with the Arduino IDE.  Arduino uses the avrdude utility to flash via serial, and we’ll do the same.  We just have to tell AVR Studio what options to use.  Click on “Tools”->”External Tools…”, then click “Add” in the window that pops up.
  2. If you’re using a USB to Serial cable like most Arduino boards do, note which COM port it uses and make the title something like “Usb to Serial Programmer: COM10″ as the Title.
  3. In the “Command” field, put the path to avrdude.exe in your Arduino installation.  For example: "C:/arduino-0022/hardware/tools/avr/bin/avrdude.exe" (quotes removed).
  4. In “Arguments”, paste this line: -CC:/arduino-0022/hardware/tools/avr/etc/avrdude.conf -v -v -patmega328p -cstk500v1 -P\.COM10 -b57600 -D -Uflash:w:"$(ProjectDir)Debug$(ItemFileName).hex":i   Edit the path to your Arduino installation and change the COM port, chip target, and baud rate if necessary (ATMega328′s need 57600 baud, older ATMega168′s flash at 19200).  The rest of the flags are the exact same as the Arduino IDE uses.  The “-v” flags control the level of verbosity – feel free to play with this parameter.  You can include up to four “-v”s (which logs every serial byte transmitted, woohoo!), but we’ve found that two flags provide enough information without being overwhelming.  Note: if you have any trouble with this step, go back to the Arduino IDE and flash a project in verbose mode by holding Shift while you press the “Upload” button.  This will display all of the command line output in the bottom status window, and you can see exactly what command your system is using to call avrdude.exe.  Edit accordingly for the AVR Studio options.  You may also want to check the “Use Output window” box so you can see the results of the flash; otherwise AVRDude will open a command window and close it the moment it’s done.

Flash!

  1. Boo yah.  Go back to your project and click on the “Tools” menu; there should now be a new menu item for your “USB to Serial Programmer”.  Make sure your Arduino is connected to your computer, then click that item.  You should see AVRDude open up in your output window, then a whole bunch of serial data fly by as it transfers to your device.  Look for the “AVRDude is done.  Thank you!” message after a successful flash.

A lot of this prep work only needs to be done once.  You’ll need to set up the toolchain properly for each project you create, but now that you have the Arduino core library and some practice, things go much quicker the 2nd time.  The AVRDude setup is universal and can be reused for every project.

You’re done!  You now have an Arduino with a fully working project, and a huge amount of new development possibilities ahead of you.  Explore AVR Studio and everything it has to offer, you’ll be impressed.  The AVR Simulator and step-through debugging are absolutely priceless when you’re working on a complex project, and you can now mix Arduino and avr-libc code to your hearts content.

Final Notes:

  1. In some cases, your build may produce ‘cxa_pure_virtual()’ errors.  This is due to a missing error handling function for pure virtuals in C++, but we can provide it ourselves:  add
    extern "C" void __cxa_pure_virtual() { while (1); }

    to your main source file.  This function doesn’t do much, but from what we’ve read these errors can safely be ignored, so we just provide a dummy function to make the compiler happy.  You could also place this in a "pure_virtual.cpp" file and then include it in every new project.

  2. Atmel has released AVR Studio 5.1 Beta as of December 2011.  They haven’t been very forthcoming about the update, but there is definitely a new avr-gcc toolchain and a new AVR Software Framework. Early reports suggest that debugging has been improved as well.  Beta software comes with the usual disclaimers, but 5.1 may fix some of the idiosyncrasies described in this tutorial.  We’ll provide an update once we have more experience with the changes.
  3. EngBlaze has had mixed results with the included AVR Studio toolchain.  If you have problems with it (in particular, we had strange errors related to “math.h”), you may want to try using the WinAVR toolchain instead, as it’s been around longer and has more testing under its belt.  You can download the latest version of WinAVR here: WinAVR downloads.  Install it.  In AVR Studio, go to “Tools”->”Options”->”AVR Toolchain”->”GCC 8-bit”, then change the C/C++ folder path to your WinAVR bin directory: for example: “C:WinAVR-20100110bin”.  Once we did this, we had no more difficulty.
  4. Are you looking for a cross platform solution?  You won’t find it here, as AVR Studio is Windows only.  If that’s alright with you, full steam ahead, but otherwise, you may want to look at Eclipse as a full-featured, cross-platform IDE.  AVR Studio has some features that Eclipse doesn’t (the AVR Simulator is huge, among other things), but the latter is no slouch.  The Eclipse setup process is similar and is outlined in great detail on the Arduino website.

Further reading

Smiley’s Workshop, a site dedicated to AVR programming and projects, has a comprehensive article on switching from the Arduino IDE to AVR Studio.  It’s a bit older and discusses AVR Studio 4 instead of 5, but is excellent background reading if you’re still trying to wrap your head around everything.

61 thoughts on “Tutorial: Using AVR Studio 5 with Arduino projects

  1. Do NOT use core.a!!!!!!!!!!!!!!!!!! Core.a may need to be recompiled every time you change a project setting. Just add all the core source code files from the “hardware/arduino/cores/arduino” directory into your project instead.

    • Core.a may need to be recompiled if you change devices or clock frequencies, but it shouldn’t need to be recompiled every time you change something.  The Eclipse setup page on the Arduino website also mentions that using the static library is the preferred method: http://www.arduino.cc/playground/Code/Eclipse#Eclipse_Arduino_core_library

      We haven’t had any problems doing it this way so far, though we haven’t tested every case.  Any other developers out there have experience with core.a vs. recompiling the core source every build?

      • The program becomes relatively very large for the small code.
        /*  Blink  Turns on an LED on for one second, then off for one second, repeatedly.   This example code is in the public domain. */#include “WProgram.h”void setup();void loop();extern “C” void __cxa_pure_virtual() { while (1); }void setup() {                  // initialize the digital pin as an output.  // Pin 13 has an LED connected on most Arduino boards:  pinMode(13, OUTPUT);     Serial.begin(9600);    }void loop() {  digitalWrite(13, HIGH);   // set the LED on               // wait for a second  digitalWrite(13, LOW);    // set the LED off  Serial.print(“kako je “);               // wait for a second}Device: atmega328p Program:   11004 bytes (33.6% Full) (.text + .data + .bootloader) Data:        237 bytes (11.6% Full) (.data + .bss + .noinit)

          • What do you mean build, I sent a picture to see, as I do. Do you have these problems? I do not know much about this, I’m a beginner, can you send a picture ofthe instructions. Thank you very much for your help.

          • Build refers to when you “build” your project by compiling all necessary code.  I was asking about the core libraries because you replied to earlier comments about them, and thought you were having a problem with them specifically.

            If you followed the steps of the tutorial but are having problems with final program size, it’s likely something is off with your optimization settings.  Check Step 4 in “Compiler Settings” above and make sure your setup is similar.  If that still doesn’t help, compare AVR Studio’s command line output to the Arduino IDE’s verbose output (Hold Shift while compiling).  Look at the flags for any call to avr-g++.exe – they should be the same.  If not, you may have to make further adjustments to your compiler settings.

          • Also, try going to “Toolchain”->“AVR/GNU C++ Compiler”->”Miscellaneous” and adding “-fno-exceptions” to the “Other flags” field.  This was originally omitted from the tutorial (now corrected).  It prevents the compiler from generating exception handling code, which uses a lot of overhead.  Your resulting hex file should be smaller.

        • Your code is becoming huge because you are using the Arduino Serial library (Serial.begin()). Remove that and the Serial.print lines and it will be a lot smaller.

          Serial is useful, but adds a lot of overhead.

  2. I too had the same problem as others where my final .hex was significantly larger than when compiled using Arduino (37k instead of 24k).  The problem is that you forgot to include a critical linker parameter so the entire core is linked including functions and code never referenced or used.

    To correct this you’ll want to update step 6 on your compiler and linker setup section of the tutorail.  Under “AVR/GNU C++ Linker” / “Optimization” make sure you check “Garbage Collect unused sections (-Wl, –gc-sections)”.  You’ll have to now recreate the Makefile, make the change to the parameter order on the line documented here, and then change to using an external Makefile again.  This eliminates unused code segments of the core from being included in the final .hex.

    Secondly, for any users that might try migrating Arduino 1.0 code using this tutorial you may want to add a defined symbol of “ARDUINO=100″ (or the appropriate version for future releases) as this is starting to be used in libraries because of the core filename changes.

    For the sake of accuracy in your tutorial I’d go ahead and add both.  The second isn’t required unless they’re using cores or a library written to use cores from Arduino 1.0 or later.  Since it won’t hurt anything for those who aren’t though it’s a highly recommended safety precaution now.

    • Thanks Craig!  You’re absolutely right, we forgot to include that flag.  Sorry for the trouble and thank you for the tip – we’ll make sure the tutorial is updated.

  3. Hi, and thanks for great tutorial, however I have a problem. Followed your instructions to the letter but get the following error that stops the “Build” : “make: *** No rule to make target `HardwareSerial.o’, needed by `Project2.elf’. Stop.”
    Being a newby I would appreciate your guidance.

    regards

    John

    • Hi John,

      That error means that make can’t find HardwareSerial.o, and can’t figure out how to create it. Usually this is because of a problem with your linked libraries – I’m guessing make can’t find HardwareSerial.cpp when it tries to build the object file.

      Did you link HardwareSerial.cpp and the rest of your libraries in your AVR Studio project (Step 11 in Compiler Setup)? If they’re linked properly, try having AVR Studio regenerate your Makefile as outlined in Step 10, then Build the project again. Do you get the same error?

  4. Hi and thanks for your quick reply. Its Sunday so dog walking will take priority for me today. Yes I get the same error and yes all the *.cpp files in the Arduino Hardware ect sub directory are included as links in project explorer. If I look at my Makefile and compare with a Example project supplied with AVR Studio I notice that on the example “# Every subdirectoy with source files…… SUBDIRS :=” lists about 9 sub directories all relative ,my Makefile shows none at all. may be relevant. I am running AVR Studio as administrator and my Win 7 is completly up to date. Arduino1.0 and 0022 directory structures are much as you describe and I have tried with and without Use External Makefile button checked.
    “Still confused and still learning ”
    many regards

    John

  5. Supplementary information. I started a new project and started to add links to *.cpp files step 11. The first file linked whatever Library function it comes from causes the error. Tried copy the *.cpp to different directories then linking to them Unfortunately still no cigar.
    Still learning still trying.

    Regards

    John

    • Hm… that’s a tough one. Makefile syntax is notoriously finicky to get right. One thing that we’ve had problems with in the past is spaces in directory paths, as Make doesn’t like them. If paths with spaces are inside of double quotes (such as a -I library include), they’re often ok, but elsewhere they can cause problems. The error message is often similar to the one you’re encountering.

      Other than that, it’s tough to say without seeing your actual Makefile. If you like, you can send us an email via our contact form and we’d be happy to look over the file to see if we can spot anything. Or you could try posting a question to StackOverflow or similar… many of the experts there are far more knowledgeable than we are at this stuff.

      Hope that’s a least a bit helpful. We’re new to a lot of this ourselves, so we’re sharing what we learn but learning as we go.

  6. Hi,
    I solved my last problem by deleteing just about every thing and starting a new project with the same files, low and behold it worked, no idea what was wrong with the first attempt. However I now have another problem. I added the Wire and SotftwearSerial library files as decribed in the tutorial and started a new project in AVR. I also started the same project in Arduino 1. Transfered the compliled and linked files from the build folder (“Project”.cpp and core.a renamed libcore.a. When I try and build in ARV I get a copiler error “Arduino.h no such file or directory in twi.c” Yes its there in Arduino////ect hardware/core/arduino . I changed the compile order both in the compiler options and latterly in the makefile but still the error. I even tried copying the Arduino file in to other directories, in desperation but starte to generate lots of other errors. It seems that the Files SoftwareSerial.cpp and serLED.cpp can find Arduino.h but not twi.c.
    Any suggestions would be greatly appreciated

    Regards

    John

  7. Just wondering … is there any reason not to just add core and m ,,, literally just add core and m as two separate entries… to the toolchain linker libraries list (the libraries list at the top)? Rather than going through the hassle of modifying the makefile?

    Then LIBS will be defined as -Wl,-lcore -Wl,-lm and everything will be in the right place … since $(LIBS) usage is at the end of the line, where we want it to be, and you don’t have to use/change an external make file at all. Seems to work.

    Maybe I’m missing something.

    • Hi Jeff, are you running AVR Studio 5.0 or the 5.1 beta? In 5.0, there’s actually a bug where the linked libraries are added to the wrong place in the command line, even when linked properly in the project config. It’s possible they’ve changed this in 5.1, we haven’t had a chance to test it yet. We’ll update the tutorial when we find out more.

      • No, I’m running 5.0 … haven’t tried the 5.1 beta yet. When I put the core and m in the libraries list … it puts this towards the top of the make file … it was just empty.

        LIBS := -Wl,-lcore -Wl,-lm

        Then, since it does that, it seems when it actually performs the link, since $(LIBS) is at the end of the linker all is good.

        Please note: I DO NOT perform this step either, since that would be redundant.

        7.Below the “Optimization” tab, click on “Miscellaneous”, and add “-Wl,-lcore -Wl,-lm” to the “Other linker flags” field. This tells the compiler to link libcore.a and also libm.a, the avr-gcc math library, which is needed by certain Arduino functions

        • Figured it out … was using the ‘C’ project type rather than C++ project type. Now it doesn’t work (and it’s really because the LIBS := doesn’t get populated with the core and m libs) … because if I manually add -Wl,-lcore -Wl,-lm to the LIBS: = (using an External Make file) it works.

          • Gotcha… yes, C projects work as they should, but the problem is with the CPP compiler setup. Hopefully Atmel realizes how annoying this is in time for the next release.

  8. i use Arduino 1.0 and the newest AVR Studio 5.0.1163.

    1. “Compiler and linker setup” point 2: i had to additionally add “C:\Program Files (x86)\Arduino\hardware\arduino\variants\standard” , otherwise i got missing “pins_arduino” blabla errors. the last directory is what type of arduino you use (uno = standard)
    2. in “Compiler and linker setup” i did not do point 11 and it works
    3. “Configure AVRDude to flash your compiled code” point 4: i had to set the path in quotes since my arduino environment is located in C:\Program Files (x86), e.g.: -C”C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf” -v ETC.
    4. “Configure AVRDude to flash your compiled code” point 4: flashing in verbose mode does not work, i only get “avrdude: usbdev_open(): did not find any USB device “usb”", but you can set verbose mode for upload in the settings of arduino (and that works).
    5. “Configure AVRDude to flash your compiled code” point 4: -P\.COM10 is wrong and should be -PCOM10
    5. “Configure AVRDude to flash your compiled code” point 4: the values for an “arduino uno” are: -C”C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf” -v -v -patmega328p -carduino -PCOM3 -b115200 -D -Uflash:w:”$(ProjectDir)Debug\$(ItemFileName).hex”:i
    6. “Configure AVRDude to flash your compiled code” point 4: the directory to the hex file was wrong, i had to add a backslash: “$(ProjectDir)Debug\$(ItemFileName).hex” i really have no idea why, but afterwards i was able to flash.

    almost everything works now, thanks for your tutorial.

    now to my problem: i want to use the SD library, i added the path and the compiler finds it, but the linker is producing “undefined reference to `File::File()’” errors. can someone point me in the right direction?

  9. I followed your instructions and I was able to successfully compile my Arduino project in AVR Studio 5.1 Beta without manually editing the makefile!

    Also, you can export your project as a template (File – Export Template) so that you don’t have to go through these steps every time you create a new project.

    • Hi Karl

      I tried the tutorial with AVR Studio 5.1.208 and Arduino 1.0. During compile i’ve got the error “skipping incompatible ..\libcore.a when searching for -lcore”. Do you know this message?

  10. For anyone else trying to debug Arduino apps using AVR Studio, the simplest solution that I found was to compile under Arduino tools and then connect the JTAG and use the ELF file for debug. I never did get the Arduino libraries to link
    As noted in other places, you need to first connect with ISCP to turn on JTAG debugging fuse and the bootloader may get trashed (or a fuse set wrong to use it) so when I wanted to go back to the Arduino environment, I had to reload the bootloader. Not at all a guru’s view of things but I was able to step and set breakpoints. There were some issues with watching values that stayed in registers.

  11. Everything seems to setup right, but when I use the discussed method i get the following:
    —————————————————————————————-
    avrdude.exe: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
    avrdude.exe: Recv:
    avrdude.exe: stk500v2_ReceiveMessage(): timeout
    —————————————————————————————-
    -AVR Studio 5.1
    -ATMega2560
    I setup my arguments as follows
    —————————————————————————————-
    -pATmega2560 -cSTK600 -b9600 -PCOM3 -D -Uflash:w:”$(F:\colegio\micro2\avrtesting\AVRTesting\AVRTesting)$Debug$(AVRTesting).hex” :i
    —————————————————————————————-
    thats ATmega2560, STK600 tool, baut rate of 9600 on com3

    Any ideas?

    • The ATmega2560 should have a default baud rate of 115200, so it might not respond to 9600 baud. My first try would be to change your baud flag to “-b115200″, then give it another try. Do you you get a different response?

    • Ok i followed this man’s seemingly incredible adventure
      http://false.ekta.is/2011/05/avrdude-5-10-arduino-mega-2560-command-line-uploading/
      and using a technique he used: holding the freaking reset button down whilst actually using this technique.
      Im not sure how backend this is but it works. And im ok with it.
      ——————————————————————————————————-
      -CF:\colegio\micro2\avrtesting\avrdude.conf -v -v -v -v -pATmega2560 -cstk600 -b115200 -PCOM3 -D -Uflash:w:F:\colegio\micro2\avrtesting\AVRTesting\AVRTesting\Debug\AVRTesting.hex:h
      ——————————————————————————————————-
      Thanks a lot!

      • whait thats
        ————–
        -CF:\colegio\micro2\avrtesting\avrdude.conf -v -v -v -v -pATmega2560 -cstk600 -b115200 -PCOM3 -D -Uflash:w:F:\colegio\micro2\avrtesting\AVRTesting\AVRTesting\Debug\AVRTesting.hex:i
        ————–
        with an I at the end, just as described above

  12. I have another possible solution, that i believe could be futher investigated. looking through the directories of avr studio i noticed a zip file that contained the sources for asf….maybe we could come up with a similar zip containing everything needed for an arduino project complete with project templates and using the avr studio asf drivers dialog to add needed libraries to our arduino projects. I think it would just be a matter of changing xml files and adding files with a similar directory structure as the asf one….. the file is com.atmel.avr.sf.source.zip

  13. Hello – Followed above steps and I am unable to talk to Arduino board through usb. I still can upload just using Arduino IDE.
    Following error in the out put window, when I click under tools –> Serial Program

    —-
    avrdude.exe: Version 5.11, compiled on Sep 2 2011 at 19:38:36
    Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
    Copyright (c) 2007-2009 Joerg Wunsch

    System wide configuration file is “N:\Microcontrollers_AVR\ARDUINO\arduino-1.0\hardware\tools\avr\etc\avrdude.conf”

    Using Port : \.COM4
    Using Programmer : stk500v1
    Overriding Baud Rate : 57600
    avrdude.exe: ser_open(): can’t open device “\.COM4″: The system cannot find the file specified.
    ——
    It is right COM port.
    Thanks for any help

    -VJ

    • Hi VJ – On our machines, the COM port argument is formatted a little differently, and looks like this in the command line: “-P\\.\COM13″
      Our output window configuration info reads:

      Using Port : \\.\COM13
      Using Programmer : stk500v1
      Overriding Baud Rate : 57600

      Maybe try making that change using COM4 and see if it makes a difference? What is the full command line string you see when using the Arduino IDE?

  14. hi
    i have done the instructions, but i have a some errors:

    Error 1 ‘fabs’ was not declared in this scope c:\program files (x86)\atmel\avr studio 5.0\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/util/delay.h 149 42 joystick_test
    Error 2 ‘ceil’ was not declared in this scope c:\program files (x86)\atmel\avr studio 5.0\avr toolchain\bin\../lib/gcc/avr/4.5.1/../../../../avr/include/util/delay.h 149 43 joystick_test

    how can i fix that?

    • Hi Cornel, those errors are usually related to a missing math library. Are you sure the linker is finding ‘libm.a’ as defined in Step 7 of “Compiler and Linker” setup?

  15. The AVR studio is just a simulator right? So if my Arduino is waiting for something to be received or an action to happen with a input pin, the AVR studio will not know right?

    • Hi Justin,

      AVR Studio is an IDE, so it’s used to develop and debug embedded applications. It includes a simulator, which has no awareness of what your physical Arduino is doing, but also has some debugging capabilities. If set up properly, you can actually view pin conditionsl, input/output, and breakpoints on a physical unit, though doing so is outside the scope of this article.

  16. I have gone through your tutorial and have had great success except for the following issue. 135 errors have been thwarted except for one. After compilation, the error reads this:

    Description File Line Column
    twi.h: No such file or directory Wire.cpp 24 19

    I am using AVR Studio 5.1 and Arduino 1.0.

    There is a twi.h and twi.c in the utility folder for the Wire library. There are also other twi.h’s in other locations, but I think the aforementioned twi.h is the one I need.

    Is it a Makefile issue? Compiler directory issue in the Toolchain directory options? Is it a C++ vs. C compiler issue?

    I don’t know. I’ve included the path to: ..\arduino-1.0\libraries\Wire for both the AVR/GNU C++ and C Compilers.

    I need a nudge as I’m out of ideas. Thanks a million.

    ~Ben

  17. Thank You for your article.
    I have just install a new AVRStudio 5.1.
    That trick with makefile is not necessary any more. core and m library can now be passed as options in project file Linker/Libraries

    MiSo

  18. Thanks for the article.

    I found that the arguments above for avrdude were not working for me. I am using an Arduino Nano V3.0. I found the following worked for me:

    -c arduino -p atmega328p -P COM30 -b 57600 -D -U flash:w:”$(ProjectDir)Debug\$(ItemFileName).hex”:i -v -v

    Two significant differences, it only worked for me with -c arduino and the \ was missing after Debug. I also copied the avrdude.conf file to the same directory as avrdude to simplify the command.

    I also found (for me) that holding shift while pressing upload in the Arduino IDE didn’t upload the program or inform of settings, but it did upload the program when shift was not pressed.

    Jason

  19. I have tried to program the example BLINKY using avrstudio 5. I successfully completed the procedure for compiler and linker setup as mentioned on the forum above , but while building the project avrstudio is showing an error that “pins_arduino.h : no such file or directory”. so I tried to copy the pins_arduino.h in the local folder of the project and including the file(as existing file) in the the project but sill cant get rid of this error . I found the pins _arduino.h in the .\arduino-1.0\hardware\arduino\variants\mega folder .

  20. First of all thanks for your tutorial.
    I’ve already make some sketches in AVR Studio and successfully uploaded them into board (Arduino Mega2560). The only thing i need to change was “-cwiring” in AVRDude command Line.

    Today when i try to upload sample working with Keyboard 4×4 the only led (pin 13) blinks and no any response. I’ve tried to compile same sample in Arduino IDE and result .hex upload via the same AVRDude. The sketch works absolutely correct. So the difference only in compiler. What should be reason of problem?

    Aleksandr

    • Aleksandr, have you verified that your command line call to avr-gcc or avr-g++ in AVR Studio is exactly the same as in the Arduino IDE? If there’s a flag or option that’s different, it can cause problems. Also, we might suggest setting up your own toolchain in AVR Studio to actually use the Arduino IDE’s included compiler, just to make sure that minor version differences aren’t causing hiccups.

  21. Hi dude, Could you please update your article with AVR Studio 5.1 or 6 beta if you have time? thanks for your hard work :)

    • Definitely – we’re hoping to have a 5.1 update out very quickly, and 6 soon to follow. You’re welcome, glad the first version has been useful enough to warrant another!

  22. First of All, many thanks for our precious work, help and support –

    tried to follow your advice using AVR studio 6, but didn’t succeed – it seems, the C++ extension are alreadey there, the Make and link settings were used as adviced by your tutorial as well as the avr dude settings – But no matter what i try – i allways get an error message: “arduino_pins.h missing in Arduino.h” – i have pointed the linker to the additional directory where the Arduino IDE keep it’s sources and made sure both files are in there.
    Have you had some time to play around with 6 and any idea, what can help to get it going.

    Thanks in advance,
    kind regards, Mike

  23. I am using AVR Studio 5.1 & followed the tutorial carefully and skipped over the makefile part since I couldn’t find the particular syntax mentioned. I don’t know if that is related but when I try to use to the USB to Serial discussed in the Flash! section of the tutorial I get the following errors in the output display in the IDE

    avrdude.exe: Version 5.11, compiled on Sep 2 2011 at 19:38:36
    Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
    Copyright (c) 2007-2009 Joerg Wunsch

    System wide configuration file is “”
    avrdude.exe: can’t open config file “”: Invalid argument
    avrdude.exe: error reading system wide configuration file “”

    Would appreciate some direction in debugging this issue, since I am completely lost in understanding what is going on in the background. Thank You for the help and the tutorial has been very helpful. (I should mention that when using Arduno IDE the cpp file isn’t generated when Arduino Ethernet is selected as the device… but it is generated when selecting Arduino Uno)

    -JasonG

  24. Hi, I just compiled my first program in AVR Studio. While uploading using avrdude, I got the following error:

    System wide configuration file is “C:/Users/Anandinho/Desktop/arduino-1.0.1/hardware/tools/avr/etc/avrdude.conf”

    Using Port : COM8
    Using Programmer : stk500v1
    Overriding Baud Rate : 115200
    avrdude.exe: stk500_getsync(): not in sync: resp=0×00

    avrdude.exe done. Thank you.

    Unlike flashing with Arduino IDE, with Studio the Rx light flashes a couple of time during upload, nothing on Tx. If I press the reset button after executing upload I get:

    Programmer Type : STK500
    Description : Atmel STK500 Version 1.x firmware
    Hardware Version: 3
    Firmware Version: 4.4
    Vtarget : 0.3 V
    Varef : 0.3 V
    Oscillator : 28.800 kHz
    SCK period : 3.3 us

    avrdude.exe: AVR device initialized and ready to accept instructions

    Reading | ################################################## | 100% 0.01s

    avrdude.exe: Device signature = 0×000000
    avrdude.exe: Yikes! Invalid device signature.
    Double check connections and try again, or use -F to override
    this check.

    avrdude.exe done. Thank you.

    Please help, Thank you.

    • The “not in sync” error is a known problem – occasionally avrdude doesn’t send the proper DTR reset pulse to start the flashing process. Pressing reset on the board is the correct solution.

      Your second error is a tougher one. I’m concerned about those readings under the Programmer Type – Vtarget and Varef of 0.3V with an Oscillator of 28.8kHz? That can’t be right. Are you sure your chip is healthy, hooked up to power, and working normally under other conditions?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>