The Visual Studio Build Tool, bundled with MegunoLink Pro, supports Arduino library projects as well as Arduino program projects.
What’s the difference?
Well, program-projects get compiled and linked into programs: hex files that you can upload onto your Arduino using AVRDude or MegunoLink Pro’s Program Device visualizer.
But code that’s in a program-project can’t be reused by any other projects. That’s where libraries come in. They let you group together a bunch of code and build new programs with it. They work just like standard Arduino libraries, but live in Visual Studio making them easier to work with.
As your Arduino programs get larger it can get more difficult to test parts in isolation. Say, you want to add a GPS sensor to a project that is already monitoring a dozen sensors and sending data over a wireless link. Then it might be easier to build a separate program to get the GPS sensor working before copying the new code into the main program. However if you put the new sensor code into a library, you can use it from the test program and the main program and be sure there aren’t subtle differences between the different versions of your GPS sensor code.
Better yet: when you build a new project and want a GPS sensor, the code for it is readily available in the library.
Download MegunoLink Pro and follow our walk-through for the Visual Studio Arduino Build tool to get started. In brief:
- Install the Build Tool from MegunoLink Pro’s cog-menu
- Create a new Arduino project in Visual Studio by selecting File→New→Project
- Choose Arduino Program (it will be in the Visual C++ template group) and give it a name
- Hit OK to create the project
In Visual Studio’s solution explorer you’ll see the new Arduino program project.
Adding a Library Project
A library project is added to the solution pretty much the same way. Only this time, you’ll choose the Arduino Library project template. So:
- Select New→Project from the File menu
- Choose Arduino Library (still in the Visual C++ template group) and give it a name
- Choose Add to solution from the Solution drop down
- Hit OK to create the project
Now you have two projects in your solution:
Referencing a Library Project
Even though they are in the same solution, the Main Program doesn’t yet know about all the library project. To make the all the goodies in the library available to the main program we need to tell it about the library project using the Visual Studio property manager.
- Open the property manager using View→Other Windows→Property Manager. You’ll see an entry for each of the projects in the solution.
- Right click on the Main Program, the Arduino-program project that wants to access the library project and choose Add Existing Property Sheet… from the context menu
- Browse to the folder that contains the library project and select the LibraryName.props file and hit Open.
If you expand the Debug folder in the Arduino program project’s properties, you’ll see the library properties entry. If you no longer want the program to have access to the library, select its property entry and choose delete from the context menu.
Using code from the Library
With the Arduino program connected to the Arduino library using the property file, you can use functions and classes as you would with any library: simply include the header file!
The Arduino-library template bundled with MegunoLink Pro includes a simple-heart beat class that flashes a LED every 400 ms. Here’s a simple program that uses that class to flash the LED on an Uno, or any Arduino with a LED attached to pin 13. This code goes in the program-project’s Program.cpp file:
const int gc_nLEDPin = 13;
Line 2 is including the Heartbeat.h header file from the library project which defines the
CHeartBeat class. On line 6 we create a CHeartBeat class and on line 15 call its only method: Update. The update method checks the time and turns on the LED every 400 ms.