IMPORTANT!!: We have now stopped supporting this build tool and are recommending Visual Micro, a plugin for Microsoft Visual Studio, as an alternative.

img_55507b80322ce

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.

Getting Started

img_5550768a282eb

The Arduino program in the Visual Studio solution explorer

Download MegunoLink Pro and follow our walk-through for the Visual Studio Arduino Build tool to get started. In brief:

  1. Install the Build Tool from MegunoLink Pro’s cog-menu
  2. Create a new Arduino project in Visual Studio by selecting File→New→Project
  3. Choose Arduino Program (it will be in the Visual C++ template group) and give it a name
  4. Hit OK to create the project

In Visual Studio’s solution explorer you’ll see the new Arduino program project.

Adding a Library Project

img_5550782dd980b

The library-project (bottom) and program-project (top) shown in Visual Studio’s solution explorer

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:

  1. Select New→Project from the File menu
  2. Choose Arduino Library (still in the Visual C++ template group) and give it a name
  3. Choose Add to solution from the Solution drop down
  4. Hit OK to create the project

Now you have two projects in your solution:

Referencing a Library Project

img_55507a9b4c6e2

Choose Add Existing Property Sheet… to reference the 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.

  1. Open the property manager using View→Other Windows→Property Manager. You’ll see an entry for each of the projects in the solution.
  2. 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
  3. Browse to the folder that contains the library project and select the LibraryName.props file and hit Open.
img_55507b80322ce

A reference to the library project’s property sheet will appear in the property manager. This reference makes all the code in the library available to the main project.

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:

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.

Recommended Posts

Leave a Comment

Start typing and press Enter to search