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:
- You need two USB to TTL adapters.
- On Arduino, use two digital pins (e.g., pins 10 for RX and 11 for TX for software serial).
- 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.
- 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.
- 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.