Using A Second (Software) Serial USB To Debug Your Arduino

So, you’ve got a working second serial connection all set up and going – now how to use it on the Arduino?

It’s actually really easy, using some code called NewSoftSerial (available from this site, at the ‘Download’ subheading). This software is much like your Serial device you use on the Arduino, but it’s in software (so slower), and it works with your new device (so great for a second debug terminal).

Here’s the steps to test it out:

  • Download the NewSoftSerial library, and unzip it into your Arduino folder (place \NewSoftSerial\ in the \libraries\ folder).
  • Run the Arduino editor, and load up the new example sketch ‘NewSoftSerialTest’ (in the NewSoftSerial Examples menu option).
  • Change the pins in this setup line to yours, such as 8 and 9 – the first entry is for the receive pin and the second is for the transmit pin (but even if it’s mixed up, just reverse them in the code if nothing works right):
    NewSoftSerial mySerial(2, 3);
  • Make sure your Realterm program is running, connected to your port, in half duplex, and set the baud to 4800 (as shown in the sketch for the serial terminal). Also, make sure your normal serial terminal is running at 57600 as shown in the sketch (or change these values – just make sure the two terminals match them in there).
  • At this point, compile/upload the sketch. If everything works, you’ll get two messages, one on your regular terminal, and one on Realterm:
    Arduino term: Goodnight moon!
    Realterm: Hello, world?

    That’s it – you’re communicating! To test further, just type away on one, and you’ll get it sent to the other (don’t forget the hit Enter if nothing appears).

In working with this, I found the biggest issues were forgetting: forgetting the baud rate, forgetting to hit the Change button on Realterm, forgetting which pin was which (and reversing them in code till they worked), and so on. However, once running, the terminals are fun to play with. As the code works now in the test sketch, anything you write on one can be sent to the other. A few things to watch out for, though:

  • On the Arduino terminal, there is a checkbox on the bottom for how to end lines. It doesn’t matter too much which you pick, just take note of it when working with the program. After all, if your code expects newlines, and you are sending breaks, you could have a problem.
  • On Realterm, using it is a bit odd. As long as you stay in half duplex, it will move to the next line, but not until a new character comes in, which can look somewhat weird. And if you want to send a break out, you have to combine it with CTRL, so CTRL-Enter will do the job.
  • There are settings on the Realterm front panel that can give you great diagnostics as to what’s going through the terminal: for example, clicking on ‘hex+ascii’ will display the character and the hexadecimal code, ideal if your characters are not all printable. Likewise, clicking on ‘terminal font’ and selecting ‘Terminal_Ctrl+Hex’ will give you extra characters for normally invisible text, helping you debug a connection.

Put together, you should have a working system for communication. Now for debugging, you can skip having one talk to the other; instead, use the second serial device to do debugging, and let the first hardware one go ahead and do its work – and aside from a little extra (and a bit slower) code, your Arduino code won’t even know it’s got a second line hooked up!

New 5V Eight Channel Relay Module For PIC AVR DSP ARM Arduino
$4.04 (5 Bids)
End Date: Friday Oct-31-2014 16:03:18 PDT
Bid now | Add to watch list
GOOD 5V Eight Channel Relay Module For PIC AVR DSP ARM Arduino
$3.93 (2 Bids)
End Date: Friday Oct-31-2014 15:38:15 PDT
Bid now | Add to watch list
Mini USB Nano V3.0 ATmega328 CH340G 5V 16M Micro-controller board For Arduino
$3.32 (7 Bids)
End Date: Friday Oct-31-2014 15:23:42 PDT
Bid now | Add to watch list

Comments are closed.