37

Is there a way to set source-level breakpoints, run the code on actual hardware, and be able to inspect variables and continue?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

31

The Visual Micro plugin for Microsoft Visual Studio 2015 Community Edition (free) provides a USB debugger for Arduino. It allows you to do exactly as you describe. (Don't forget to click to install C++ during IDE install)

The debugger supports serial, RF, Bluetooth and some Wi-Fi. It enables the values of variables to be watched or update while the Arduino runs. Chart and data/pin visualizations along with watch, trace, break are included.

Enter image description here

News: January 2016 also includes release of a GDB debugger for the Arduino Zero. As with the Serial/Bluetooth debugger, the GDB version supports ino/cpp source code, but additionally supports debug of Arduino core and libraries. It supports many features such as: step over, step out, step instruction or source line, memory, registers, locals, watch, live expressions, and stack trace.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Visual Micro
  • 1,561
  • 13
  • 26
  • Can you also use this to debug C and assembly? – Pyjong Mar 02 '17 at 09:34
  • @pyjong The default debugger uses Serial or Wifi which won't work from c, it will only work with .Ino and .cpp. However if your board supports Gdb or you have a hardware debugger there are other options, so please open a thread in our forum and raise a question. We will answer it and assist. Thanks – Visual Micro Mar 02 '17 at 10:27
  • Aren't there some requirements for the hardware for this to work? E.g. would it work on an Arduino Uno? (One lead may be *[Debugger for Arduino](https://www.codeproject.com/Articles/1037057/Debugger-for-Arduino)* (The Code Project).) – Peter Mortensen Sep 16 '17 at 11:19
  • No the Serial debugger does not need extra hardware it uses the same simple usb connection that most users are using to upload their code. It can also use software serial or other hardware serial if available. An Uno is a good example of where the usb/serial debugger can be useful because hardware debug of an Uno can be difficult (especially for non-expert). However if you have a hardware debugger for the Uno then install Visual Micro into Atmel Stdudio 7 and use that. Make sure you read the AS7 page on visualmicro.com. – Visual Micro Sep 16 '17 at 15:21
24

The way to set "source breakpoints" on the Arduino is to add a serial output to send the value the you want to see to the Serial Monitor.

When you are ready the next step is to move to WinAVR, AVR Studio and a Atmel JTAG Mark II or a Atmel Dragon.

These programs and devices will allow you to create C code and single step through the code and monitor variables and registers. The JTAG devices can single step through your C code or the assembly code created by the compiler.

Be forewarned that high level embedded C programming is still very close to the machine and you have to be careful single stepping interrupt routines, timer routines and other low level routines because many times it will prevent the code from operating correctly.

Jeff
  • 1,364
  • 1
  • 8
  • 17
  • 1
    "The way to set "source breakpoints" on the Arduino is to add a serial output to send the value the you want to see to the Serial Monitor." What happens if you're using your serial port for something else? – Jules Apr 10 '16 at 14:07
  • 2
    Three choices of many, upgrade your development environment using AVR Studio and a JTAG device, use an Arduino mega which has multiple serial ports or use the software serial library. – Jeff Apr 11 '16 at 13:32
  • Solution I'm actually using: stick an LCD display on the device and use that for debugging rather than the serial console, but those options are interesting too. Hadn't come across software serial before, but that looks useful for future projects... – Jules Apr 11 '16 at 18:11
  • LCD is a great option too! Takes a few more I/O and some more code, however if you have experience with LCD displays it would have benefits the other options I offered don't have. Once you upgrade your development to include a JTAG I don't think you will ever go back. – Jeff Apr 11 '16 at 18:14
2

For inspecting variables I just print them to the serial monitor. Here's an example I'm working on at the moment:

# Setup
Serial.begin(9600);

# Loop
Serial.println(String(index) + " : " + String(total) + " : " + String(average));
datu-puti
  • 1,306
  • 14
  • 33