In this article you will find an example project for Atmel Studio that you can use with Arduino. I have also included two Python files to easily upload the code to your board.
Arduino, pro’s and con’s
There are many reasons to like the Arduino, to name a few:
- Many libraries available and lots of example code on the web.
- Uploading code to your board with one click
- Portability of libraries
- Integrated serial monitor
But there are also many things that drive seasoned programmers away from the Arduino environment:
- The IDE has about as many features as Windows Notepad.
- No auto-completion or any other assistance that modern IDE’s offer to let you program faster
- No debugging
- Very unhelpful error messages
- No ability to jump to the line of the error
- Although all Arduino files are open source, it’s almost impossible to jump into a file to see for example what digitalWrite() actually does. The Arduino documentation is very basic and doesn’t even link to the source files.
Basically Arduino is a collection of great libraries with a shitty IDE. So this article will tell you how to ditch the shitty IDE and still use the libraries.
Enter Atmel Studio
Atmel Studio, especially since version 6, is an awesome IDE. It has the Visual Studio 2010 shell, which is a joy to use. If you try it once, you will never move back to the Arduino IDE.
The reason I moved to Atmel Studio in the first place is that I wanted to use in-circuit-debugging: setting breakpoints on the device. Say you are debugging a rotary encoder that uses interrupts. The only thing the Arduino environment offers you is to write something to the serial port. With in-circuit-debugging, you can set a breakpoint in the interrupt, turn the encoder, and get the following information:
- You can see the value of all your variables.
- You can see all your registers: are the interrupts configured correctly? What triggered the interrupt?
- You can step through your code line by line to see where it takes a wrong turn.
If you want to use in-circuit-debugging, you will need a debugger like the AVR Dragon. I also recommend to get the Arduino Leonardo, which has JTAG. It’s a lot faster than debugWire, which is the only option for all other Arduino’s.
My example project
The guides I have found on using Arduino’s with Atmel Studio all involve many steps and usually require manual editing of a makefile. I wanted to make things simpler and wanted to be able to use the toolchain settings in Atmel Studio. I am also using the avr-gcc toolchain that comes with Atmel Studio instead of the Arduino toolchain.
My example project is hosted on GitHub. You can download it with the ‘Clone in Windows’ button, or the ‘ZIP’ button.
I used the non-blocking example from my ShiftPWM library. If you don’t have the ShiftPWM library on your computer, you can get it here. Git it a try, it can give you up to 768 PWM outputs on one Arduino.
To get the project to compile on your computer, you only have to take the following steps:
- Open the solution file
- In the project properties –> AVR/GNU C++ Compiler –> Directories, change the 3 directories to your point to your local Arduino directory. Also add the path for any other libraries you are using
- If you are not using a 16MHz Arduino, change F_CPU under AVR/GNU C++ Compiler –> Symbols
- In ArduinoFunctions.cpp, uncomment/include any .cpp files that you are using from the Arduino directories.
Programming your Arduino with Python
One of the benefits of the Arduino environment is that you can compile and upload your program to your Arduino with one button click. If you are compiling your code Atmel Studio, you will have to use avrdude from the command line. This can be complicated.
To make it a easier for you, I have include two Python files. You only have to edit a few lines in this file to have one click uploading without the Arduino IDE. Well, two button presses: F7 to compile in Atmel Studio. And F7 again in Python/Sublime text to upload your code.
The Python script will automatically get the right settings for your Arduino from boards.txt, reset your Arduino and upload a hex file with avrdude.
And what about the serial monitor?
Allright, the one thing we will still use from the Arduino IDE is the serial window. You don’t have to have any files open, just click the serial monitor button to use Arduino’s serial window.
So there you have it, the best of both worlds: awesome libraries AND an awesome IDE.