How to debug with Arduino


If you need to use the hardware UART of Arduino to control some other module, e.g., the Telit GM862, you may wonder how you can use the hardware UART and use Serial print for your debugging. Well you cannot. Your options are to use Software Serial. But then you should not use software serial for communicating with GM862. There are too many issues, especially if you plan to read from GM862. The default SoftwareSerial is not interrupt based and hence you may lose input. The NewSoftSerial does not support 8MHz systems, so that means you wont be able to use it with Arduino Mini Pro 8MHz. In the past I had used hardware serial for debugging and software serial for communicating with GM862. But given the issues that I just mentioned this was not a satisfactory solution.

Matthew Elias, suggested that I use a second USB TTL for debugging. This is a brilliant solution. It allows me to use hardware UART for communication with GM862 and the out of the box SoftwareSerial for debugging. Here is what you need to do:

  1. You need two USB to TTL adapters
  2. On Arduino, use two digital pins (e.g., pins 10 for RX and 11 for TX for software serial).
  3. Connect the 1st adapter to Arduino and use that to Upload your program to the board. You’ll need to add a switch to the TX line of the USB adapter. The switch must be on when you want to upload a program to Arduino, and you must turn it off when you want to run/debug the program. If you don’t add a switch then the USB voltage will interfere with the device that you’re managing, e.g., GM862.
  4. Connect the 2nd adapter with three wires to the Arduino: (a) connect adapter’s TX pin to Arduino pin 10 (software serial RX) ; (b) connect  adapter’s RX pin to Arduino pin 11 (software serial TX); (c) connect adapter’s GND pin to Arduino’s GND pin.
  5. Open the Terminal application on the Mac (or Hyper-Terminal on PC) and connect to the second USB port (due to the second adapter). 

Now you can use the Terminal application for IO with SoftwareSerial. Here is a simple program to test this setup (note that on the Mac, you need to use 2400 baud rate for the SoftwareSerial and for when you connect to the USB port via Terminal).

#include <SoftwareSerial.h>

int rxPin = 10;
int txPin = 11;

// set up a new serial port
SoftwareSerial debug = SoftwareSerial(rxPin, txPin);

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  Serial.begin(4800);
  debug.begin(2400);

  debug.println("SoftwareSerial...");

  Serial.println("hardware UART");
}

void loop() {
  int c;
  char cmd;
   if ((c = debug.read()) != -1) {
     Serial.println("I'm here");
     cmd = char(c);
     debug.println(cmd);
     if (cmd == 'a') {
       debug.println("A");
     } else if (cmd == 'b') {
       debug.println("B");
     }
   }
}

From the Terminal you can type ‘a’ and the main loop will print ‘A’ back at the Terminal. All IO from hardware UART will be directed to GM862 (or whatever module that you need to communicate with).

Thank you Matt for providing this solution.

13 Responses to “How to debug with Arduino”

  1. Shawn Says:

    What WordPress plugin are you using to display your code with? I need something like this for my blog. Thank you for your time.

  2. sj Says:

    Hi, I’m not sure of the name of the plugin, but here is further info. on this.

  3. Shawn Says:

    Thank you. This helped a lot!

  4. pic.micro23 Says:

    Hello SJ – One question about your experience using NewSoftSerial and the GM862. You said that you have experience problems using the library.
    Right now the project that i’m working on needs the use of the software serial library to interface with GM862 to only send and receive SMS messages (not planning to run on 8Mhz), no GPS or GPRS.

    Do you think it will be a problem based on your experience?

    Thx

    • sj Says:

      Hello, sending SMS should be fine as long as you don’t plan to read the reply to the AT commands. That is where I run into a problem, reading the AT commands responses. If you’re OK with sending an SMS and not reading the response, then you should be fine.

      • pic.micro23 Says:

        wow. That might be a problem then. I need to read received SMS messages too. Darn it!

        I guess I need to find another AVR with dual hardware UART. The atmega 168 processing power and # of IO pins are enough so i need to upgrade only because the UART 😦

        Any recommendation of an AVR with dual hardware UART, the 128 is overkill? I will google it but maybe you have a recommendation.

        Thx for your time.

  5. sj Says:

    Really there is no need to use the software serial for communicating with the modem (unless your application demands it). I recommend using hardware serial for control of the modem and software serial for debugging. Good luck. BTW. I would like to start an open source project for creating an Arduino C library for the Telit AT commands. Let me know if you’re interested?

  6. sj Says:

    I think Sanguino has dual hardware UART.

  7. Matt E Says:

    I’m using the Sanguino, mostly for the larger memory but the dual UART is nice. With a single UART I’d try what SJ describes in the blog entry we are commented on.

  8. Peter Says:

    Hi, thanks for the suggestion. I don’t really understand why two USB adapters are necessary. Is it because you are using the 1st one just to power the project? Couldn’t you use a single USB adapter to upload your firmware, then remove it and plug it into the SoftwareSerial ports, then power the project off a 9V or something?

    • Shahram Javey Says:

      Simply for convenience. During development you really don’t want to keep unplugging the debug monitor.
      BTW. The GSM module cannot be powered by USB ; your powersupply needs to handle 3A surges.

  9. Peter Says:

    Thanks for clearing that up; makes sense. I have another question related to your point about putting a switch on the TX line. I am trying to design a board where a UART peripheral is permanently connected to the Arduino RX/TX lines, but it is still possible to upload sketches via USB.

    I tried to design a circuit using an optocoupler that will allow the board to automatically disconnect the peripheral’s TX line when the USB is plugged in, so that the upload process doesn’t damage the peripheral and the peripheral doesn’t interfere with upload. Details here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1284939455

    Would love to get your feedback on this. Is this a reasonable approach? Is there a better way? Is disconnecting the TX correct, or should I simply depower the peripheral or disconnect the RX? Some combination? Many thanks!

  10. Visual Micro Says:

    Nice simple example thanks.

    Another option that is worth exploring is the free Visual Studio Arduino plugin. Aside from a host of useful code explorer and intellisense features it clearly shows code as enabled or disabled based upon #defines. This makes it much easier to see source code that is used only when debug is enabled. http://www.visualmicro.com

    There are also some useful arduino source code debug trace and/or step features to be released June 2012. http://www.visualmicro.com/post/2012/05/05/Debug-Arduino-Overview.aspx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: