Level Up Your Arduino Skills — An AVR Tutorial

Writing code for your favorite AVR device can be complex, whether you are seasoned engineer, or a hobbyist, chances are during the development of your project you will be hit sooner or later with an unexpected behavior that will leave you scratching your head for hours (if not days) wondering where your code is not behaving as expected.

Trust me, I have been there plenty of times, but once you find out the root cause of your issue, the feeling of accomplishment is tremendous.

On electronic projects errors can come from hardware or software; assuming there were no misrouted traces, tumbstoned components, etc. It leave us with software errors.

Depending on your chosen setup, if you are using an Arduino compatible board as the brains of the operation, odds are you will be using the all mighty Arduino IDE to develop and load your software onto your board, and sure enough plenty of online tutorials will advise you to use the serial print when you hit a bug, like so:

But that solution comes with it’s drawbacks, if you want to monitor every variable during runtime, you are up for a wild ride; not only will eat away your ram, but also flash memory; you can not set breakpoints which in a nutshell halts the microcontroller’s CPU so you can review the information it was working on at any given time.

The solution? Use dedicated programmer/debugger such as the AVR Dragon or Atmel ICE along with the Microchip Studio IDE (formerly Atmel Studio).

That combo will not only give you more access to the debugger to set breakpoints to your heart content, but it will allow you to set or change variable values at any time, verify if a given pin is enabled among many other goodies, which will save you a lot of time narrowing down complicated bugs!

It certainly does come with it’s learning curve, don’t sweat it, although Arduino IDE does a good job at abstracting many steps to facilitate new comers into the field, migrating and using Microchip Studio is a valuable skill to learn as you progress on your career; as if you plan to join an established company or team, chances they are using Arduino IDE are slim to none.

To get started head over Microchip’s website and grab a copy of the software; careful that it’s almost 1GB in size. Launch the installer, you can leave most settings as default.

Microchip owns Atmel, so they bundle their proprietary compiler XC8 along with the AVR-GCC compiler, so keep it in mind.

Run the program and you should be greeted with a screen similar to this one:

Hit “New Project” and select the last option with the Arduino icon, for now you can only import an existing Arduino project, maybe in the future that might change, but to start a brand new project, a workaround is create an empty Arduino file and use it to create the empty project.

Select the file you have been working on, choose the appropriate board and hit ok, like so:

You should be greeted with a screen similar to this one, to compile the project hit on ‘Build > Rebuild’; it is always advisable to ‘rebuild’ instead of ‘build’ each time you make a change as Microchip Studio might not properly address all the changes you made during compilation.

After you build it, you should get a console similar to Arduino’s IDE detailing any errors, warnings, and program usage.

In the project tree you will find all of the files that make up the Arduino core, usually this is hidden from you in the Arduino IDE, but here you have more transparency to what is happening.

Let’s explore the produced main file; at the very top you will see an Arduino.h import, this allows you to leverage well known functions such as digitalWrite, pinMode, etc. Right after, you will be greeted with library imports as usual and the familiar setup and loop functions; like so:

You can start developing your code as you normally would with Arduino IDE. When you code along you might start wondering, how in the world would you add new libraries to your newly created project? Fear not!

Every Arduino library zip file must contain at least 1 file with the extension “.h” plus a “.cpp” pair, but it might include many more.

From the project structure navigate to the libraries folder inside the “include” and “src” folders respectively, like this:

Create a new folder with the name of your library (preferably) by right clicking libraries > Add > New folder, if you create the new folder inside the include directory, copy and paste all “.h” files there, likewise for the “src” folder and the “.cpp” files; and finally hit “Add > Existing Item” to add both type of files.

Rinse and repeat for all the libraries you need in your project, granted, some manual work is required. Then from your main sketch include the library as normal.

Next steps

With the basics now covered, you are now ready to start leveraging the true power of your hardware and create more complex gadgets without fearing of any undesired bug.

Moving away from the Arduino IDE is crucial as you progress in your electronic career as there is considerably more capable hardware to the common Arduino board at a lower price point with far more better peripherals such as native USB support, more timers, built in LCD drivers, etc. which will give you extra room to develop a more powerful software.

I hope you’ve enjoyed this quick intro the wild world of AVR devices!

If you liked this post, stay tuned as I am currently working on to share more related stuff on Electronics!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lesther B.

Lesther B.

Electronic Engineer, passionate about embedded programming, data analysis, and web development