debugging-profesional-nanoframework

Debugging in nanoFramework: Breakpoints and Variables

  • 5 min

Debugging in nanoFramework is the ability to pause and inspect code while it runs on the microcontroller.

One of the clearest advantages over the usual trial-and-error cycle in embedded systems is integrated debugging.

Anyone who has programmed microcontrollers knows how painful the “trial and error” cycle is:

  1. You write code.
  2. You fill everything with Serial.println("Entered the IF");.
  3. You compile and upload (wait 1 minute).
  4. You look at the serial monitor and pray to understand what happened.

With nanoFramework and Visual Studio, this is history. We can pause time, inspect the microcontroller’s brain while it’s running, and see exactly what’s happening.

This is one of the clearest advantages of nanoFramework. In this article, we will learn how to use these tools.

The Output Window

Let’s start with the basics. In Arduino, to see messages on the PC, you needed to initialize the serial port, configure the baud rate (115200), and use a terminal program.

In nanoFramework, the debugging communication is integrated into the protocol Visual Studio uses.

using System.Diagnostics;

// ... inside your code
Debug.WriteLine("Hello from the ESP32");
Copied!

When you run your program (F5), this message will automatically appear in the Output window of Visual Studio.

You don’t need to configure baud rates or open COM ports. Visual Studio channels these messages through the debugging connection automatically.

Breakpoints: Pausing Execution

A Breakpoint is a signal that tells the microcontroller: “When you reach this line, stop and wait for orders”.

To set one, simply click on the grey margin to the left of your code, or press F9 on the line. A red circle will appear.

When the program reaches that point:

Execution on the chip freezes.

Visual Studio enters “Debug” mode (orange frame at the bottom).

A yellow arrow indicates the next instruction to be executed.

Step-by-Step Navigation

Once paused, we can control execution. In the top toolbar you’ll see the corresponding buttons, but you can also use these keyboard shortcuts:

  • F10 (Step Over): Executes the current line and moves to the next. If the line is a function call, it executes the entire function and moves to the next line of your code.
  • F11 (Step Into): If the line is a function call, it steps inside that function so you can debug it line by line.
  • Shift + F11 (Step Out): Executes the rest of the current function and returns to the caller.

Imagine you have a for loop that fails on iteration 3. You can press F10 repeatedly and see how everything changes cycle by cycle. It’s like watching a movie frame by frame.

Variable Inspection

While the program is paused, you can know the value of any variable at that exact moment.

There are three ways to do this:

Hover

Simply hover your mouse cursor over a variable in your code. A small box (DataTip) will appear showing its current value. If it’s a complex object (like a sensor), you can expand it and see its internal properties.

Locals Window

At the bottom, look for the Locals tab. Here, Visual Studio automatically shows you all the variables that exist in the current function context. It’s great for seeing at a glance how the state of your entire method changes.

Watch Window

Want to watch a specific variable that isn’t in the local scope, or a mathematical expression? Right-click on a variable -> Add to Watch. This creates a custom list where you can always see specific values.

Modifying Values on the Fly

This usually leaves first-timers speechless.

Imagine you have this code:

if (temperature > 50) 
{
    TurnOnFan();
}
Copied!

You’re debugging, but your sensor reads 25°C. You don’t want to heat the sensor with a lighter to test the if.

  1. Set a Breakpoint on the if.
  2. When it stops, find the temperature variable in the Locals window.
  3. Double-click the value 25.
  4. Type 60 and press Enter.

You have just changed the microcontroller’s memory in real-time! Pressing F10, the program will evaluate 60 > 50 and enter the if.

This ability to force conditions and simulate error states is very useful for testing robust systems without having to physically reproduce extreme conditions.

Conditional Breakpoints

Sometimes you don’t want to stop always. Imagine a loop that runs 1000 times and fails on the last one. You’re not going to press F5 1000 times.

  1. Set a Breakpoint (red dot).
  2. Right-click on the red dot -> Conditions.
  3. Write a boolean expression, for example: i == 999.

Visual Studio will only stop the microcontroller when that condition is true.

What about Exceptions?

In Arduino, if you access an array out of bounds or divide by zero, the microcontroller usually resets or hangs (the famous ESP32 Guru Meditation Error).

In nanoFramework, Visual Studio will catch the Exception.

Execution will stop automatically, it will take you to the exact line of the error, and it will show a popup message explaining what happened (System.IndexOutOfRangeException, System.NullReferenceException, etc.).

Visual Studio’s debugger with nanoFramework transforms embedded systems development. We go from “guessing what’s happening” to “seeing what’s happening”.