Is it possible to modify AVR8-STUB source code to Debug Arduino Mega 2560 while keeping Serial Port 0 printing functionality?

Hi everyone, I’m using PlatformIO with AVR8-STUB and after a little research I realize that right now is not an option to use this debugger and Serial.print at the same time, as this link illustrates:

But I’ve been thinking and I would like to know if the following could be a workaround:

The idea is to modify Arduino Mega 2560 Bootloader and AVR-STUB definitions to use UART3 instead of UART0 for debugging
i.e. using RX3-TX3 with an FTDI for debugging and keeping RX0-TX0 printing functionality at the same time

Does anyone have tried something similar? And if yes, would you guide me on how to do it?


Why do I need avr-stub debugging instead of serial prints?

  • This project has 8 custom made libraries and uses another 10 open source libraries. As you could imagine the code is getting bigger. Serial prints for debugging are no longer pretty nor useful choice

Why I would like to keep functionality on Serial Port 0?

  • As I mentioned before I use third party libraries that already use Serial port printing (a lot)
    some of them are mentioned here:

  • I could comment out Serial.prints in the whole code but it would be a real pain and not sustainable at all, there are more than 500 usages in the mentioned libraries

  • And last, the aim of my device is to connect with a computer through serial port 0 and send data to a python app, so the idea is to use built in USB host port for this. I could add an FTDI over another serial port but I think it would be a waste and will not solve the previous mentioned issues

Fork GitHub - jdolinay/avr_debug: Source level debugger for Arduino - GDB stub for Atmega328 microcontroller used in Arduino Uno. and off you go :smiley:

1 Like

Amazing!!!. It works!

Sorry for the late reply, I couldn’t test it until today.

My original idea was to modify Arduino bootloader to use UART3 also for program uploading but it was not necessary, I realize there are two different parameters inside of platformio.ini, upload_port and debug_port, that allowed me to run those options separately

So I ended up using built in Arduino USB host (UART0) for program uploading, and, at the same time, USB to Serial TTL CP2102 connected on RX3 TX3 (UART3) for program debugging

In case that someone is struggling with this, here it is my setup:


  • I put avr8-stub in libraries folder, in my case, ~/Arduino/libraries and included that path on lib_extra_dirs

  • platformio.ini file:

    platform = atmelavr
    board = megaatmega2560
    framework = arduino
    lib_extra_dirs =  ~/Arduino/libraries
    lib_ldf_mode = deep
    debug_tool = avr-stub
    debug_init_break = tbreak setup
    upload_port = /dev/ttyUSB0  
    debug_port = /dev/ttyUSB1
    build_flags =
  • This is how I modified avr8-stub.c:

    line 386:

      #define	UART_ISR_VECTOR	USART3_RX_vect

    lines 949 to 954:

      UCSR3A = _BV(U2X3);		/* double UART speed */
      UCSR3B = (1 << RXEN3 ) | (1 << TXEN3 );		/* enable RX and Tx */
      UCSR3C =  (1 << UCSZ30 ) | (1 << UCSZ31 ); /* Use 8- bit character sizes */
      UBRR3H = ( GDB_BAUD_PRESCALE >> 8);
      UCSR3B |= (1 << RXCIE3 ); /* Enable the USART Recieve Complete interrupt ( USART_RXC ) */

    lines 962 to 965:

      while ( !(UCSR3A & (1<<RXC3)) )
      return (uint8_t)UDR3;

    lines 972 to 976:

      while ( !( UCSR3A & (1<<UDRE3)) )
      /* Put data into buffer, sends the data */
      UDR3 = c;
  • And finally CP2102 to Arduino physical connection

    (Arduino) RX3 <–> TX (CP2102)
    (Arduino) TX3 <–> RX (CP2102)
    (Arduino) GND <–> GND (CP2102)
    No connection <–> DTR (CP2102)
    No connection <–> 5V (CP2102)
    No connection <–> 3V3 (CP2102)

    IMPORTANT: I didn’t connect 5V nor 3V3 on CP2102 to Arduino, since Arduino board is already powered by a different USB port (Built in USB)

Thanks again @maxgerhardt!

1 Like

Great work! I hinted at your code at Using Alternate Serial Ports · Issue #25 · jdolinay/avr_debug · GitHub.

1 Like

HI there,

thanks for the information on the solutions on how to allow the Serial.println() estatements while debugging.
Unfortunatelly, I am pretty new to the PlatformIO world, so I am missing a step by step description on how to enable the println estatements.
My use case is that I installed Visual Studio Code on windows, and the PlatformIO extension, because I wanted to debug some simple code for ArduinoUNO, but I found I am not allowed to print the estatements. I have no idea on

  • how to use GIT to update the avr_debug library in windows I assume (is the possibility of downloading the zip from somwwhere and manually exchange the files that are located somewhere?)
  • or where I can find the files that I would need modify for the other solution of using UART3 instead of UART0

So if somebody could invest some time on either of both solutions I would be grateful

Thanks a lot,

No modification of source code necessary, only configuration via macros in the platformio.ini. See

That is not necessary because the library has not been modified for the last two years, so using the lib_deps expression as recommended will work.

I would suggest though you still start with the regular example at Usually you also need no Serial.println() statements because you can see the value of the variables in the debugger directly, as opposed to needing to print them out.