Tutorial Using Atmel Studio 6 With Arduino Projects
Tutorial Using Atmel Studio 6 With Arduino Projects
com/tutorial-using-atmel-studio-6-with-arduino-projects/
In our previous Atmel tutorial , we talked about how to set up the powerful AVR Studio 5 IDE to incorporate Arduino
libraries and projects. As flexible as AVR Studio 5 is, it had a few issues, and Atmel has been hard at work hustling
the next major version out the door. Now, rebranded as Atmel Studio 6 (no longer just for AVRs!), the new version
promises to be better, faster, and easier to use. Here, we’ll show you the quickest way to get up and running if you
want to use Arduino code with all of the new features.
Note: This article explains how to set up the Atmel Studio 6 IDE for use with Arduino projects, step-by-step. It also
notes on general setup for working with Atmel devices, background on the pros/cons of working with AVR Studio, and
a few other tips. A table of contents is below; feel free to skip to any section that interests you.
Introduction
Preparing AVR Studio and the Arduino core library
Compiler and linker setup
Build your project
Flash!
Final notes
Further reading
Introduction
Atmel Studio 6 delivers a lot of the value that AVR Studio 5 promised but never quite gave. Released in 2011 and
based on Microsoft Visual Studio, Studio 5 was a large change from AVR Studio 4, which was based on the tried and
true Eclipse IDE. Studio 4 is seriously showing its age these days, so a refresh was welcome. However, version 5
came with a long list of bugs and didn’t deliver on a lot of the feature list, which left a lot of people wondering whether
they should upgrade. The new version appears to have addressed a lot of those bugs, and gets higher marks from us
in our initial testing.
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 6. 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.
1. Now we have to tackle proper compiler setup and linking. Go to “Project”->” Properties” (or press Alt-F7),
then click on “Toolchain”. We need to setup a bunch of compiler options here. By default, this will only edit
your “Debug” build configuration, so select “All Configurations” from the Configuration drop-down menu if
you want to modify both release and debug configurations at the same time.
2. First, click on “Symbols”, in the left-hand “AVR/GNU C++ Compiler” dropdown menu. We need to add a
symbol to tell the compiler the speed of our chip: for example, click the green plus icon, then enter
"F_CPU=16000000L" for a 16Mhz chip. Most 5V Arduinos are running at 16Mhz, hence 16000000 (L stands
for long integer). If you have a 3.3V Arduino, it will likely be set at 8Mhz, so use F_CPU=8000000L instead.
Next, add another symbol to define your Arduino software version: "ARDUINO=100" for v1.0,
"ARDUINO=101" for v1.01, etc.
3. Click on to “Directories” in the same C++ Compiler menu. We need to add the directories that contain our
Arduino core code and libraries, so the compiler can string it all together for us. For any Arduino project, we’ll
need to tell the compiler where to find “Arduino.h” and “pins_arduino.h”. Starting in your Arduino installation
directory, add the folders “./hardware/arduino/cores/arduino” and “./hardware/arduino/variants/standard”.
4. You’ll need to add the directories of any extra Arduino libraries you’re using. For example, if you include the
SoftwareSerial library, go ahead and add that directory to this section so the compiler knows where to find it. If
you forget anything, the compiler will tell you when you try to build, with a message such as “SoftwareSerial.h:
No such file or directory”. You should also add the home directory of your project, so the compiler can find
source files there.
5. You’ll also need to add the .cpp source files for those same linked libraries to your actual Atmel Studio project.
Add a directory to your project (“Project”->”New Folder”) and name it “libraries”, to keep things organized.
Then, go to “Project”->”Add Existing Item” (or Shift+Alt+A), and find the source directories for your
included libraries (usually in “/libraries”, unless you have a custom setup). Select the source files, but instead
of clicking the “Add” button, 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.
6. 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.
7. Now we’ll move on to the linker. In the left-hand menu, click on “AVR/GNU Linker”->”Libraries”. In the top
Libraries section, you should already see an entry for “m”, which is the AVR math library. Add an entry called
“core”, which is our libcore.a file that we grabbed earlier.
8. We also need to tell it where to find libcore.a, so add our earlier “arduinoCore” directory under “Library
search path“.
9. Click on “AVR/GNU C++ Linker”->“Optimization”, and check the box for “Garbage Collect unused
sections (-Wl, –gc-sections)”. This tells the linker to leave out unused portions of each library, which
reduces final code size.
10. Awesome, we’re done with the project setup. Save your settings, and we can get back to the code.
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 you have the main .cpp source file open in your IDE window –
the serial programmer will try to access a .hex file for whatever tab is open, so this is the only one that will
work. Ensure your Arduino is connected to your computer, then click the Programmer menu item. You should
see AVRDude open up in your output window, then a progress bar showing flash status. 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
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. 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.