Learn how you can use Visual Studio to debug your Arduino programs as you are creating them!

The VisualMicro extension for Visual Studio adds some useful features for developing Arduino applications that the standard Arduino IDE is lacking. One such feature is the debugger, which helps you find errors in your programs. 

In this article, we’ll go over the basics of the debugging feature and how it works.

Monitoring GPIO Pins

When you use GPIO pins as inputs you may experience unexpected behaviors. To better understand what’s going on, you can monitor the ports of your Arduino.

While it’s possible to monitor the ports with the Arduino IDE by using console outputs to print the pin state, VisualMicro can do this for you and also generate a handy chart displaying the state of each pin.

Start by enabling the monitor:

monitor digital pins

The next time you upload your program, this panel should appear:

GPIO pin monitoring

Each GPIO pin is represented by a square and the color each square indicates the pin state. Red indicates a low state while green indicates a high state. A similar chart can be generated for the analog pins.

Inspecting the Program Flow

You’ve probably already put console outputs in the code to test whether your application reaches a specific line of code. While you can do this, there’s an easier solution. You can use breakpoints that won’t pause the program execution. Instead, it’ll output a line to the console when they are reached.

To add a breakpoint, right click and select Actions. A panel should appear allowing you to input the text that prints to the console when the breakpoint is reached:

set your message for breakpoints

Set the message you'd like connected to your breakpoint.

To enable “classic debugging”, where reaching a breakpoint will pause the program execution, deselect the “Continue execution” checkbox.

Printing and Inspecting Variable Values

The VisualMicro debugger works differently from the Visual Studio debugger because the applications are not executed on the computer they are developed on. However, you can still make the debugger display variable values to easily determine their behaviors.

One way to set up additional displays is by using the breakpoints from above. Not only can you set a custom text output when a breakpoint gets hit, you can also add variable values to the text by adding the variable name in curly brackets:

add variables to breakpoints with commands in curly brackets

Add variables to your breakpoints with commands in curly brackets.

When you run the code on your Arduino, the output panel should display your debug messages:

output panel displaying debug messages

You’ll also notice that VisualMicro generates a table of each variable that’s used in a breakpoint:

table of each variable

The table should automatically appear when you debug your code with each breakpoint set up to print a variable value. If it doesn’t, try opening the serial monitor then re-upload your code.

Changing Variable Values

Many IDEs allow you to alter variable values when the program is paused. You’ll have to update the breakpoint definition from above to enable this feature. Append “=?” to the variable names that you want to be able to change during debugging:

update variable names for debugging

After re-uploading the code to your Arduino, you can use the table from before to change values. However, the breakpoint has to stop the program when it’s reached.

Debugging Code: An Essential Task

This basic guide teaches you everything you need to know to start debugging Arduino applications in VisualMicro. This should make it easier to understand errors in your applications as they happen and write better code.

Daniel Hertz
Hi! I am a software engineer and owner of nerdhut.de who loves to experiment with electronics, gadgets and tech in general.