I am trying to use a Leonardo to read data from a GPS chip and display it on the Serial Monitor on the PC. The sketch (not mine - a tested one available on line) uses the SoftwareSerial library to communicate with the GPS chip via a couple of the digital pins (I have tried pin pairs like D2 &D3, D11&D12), but don't get anything displaying on the Serial Monitor. I'm trying to decide if the problem is me, or some limitation of the Leonardo which I understand has some differences compared to other Arduino's like the Uno. However, I'm not clear on the details of those 'serial differences'.
Should the Leonardo be able to communicate with both the Serial Monitor and the GPS chip using the SoftwareSerial library?
Thanks,
Al
The Arduino Leonardo uses also a microcontroller of the AVR family, so it is similar to the Arduino Uno in many ways. As an extra, it has a built-in USB-serial CDC device.
The serial monitor uses that internal USB-serial device, and the serial port at pin 0 and 1 is a spare hardware serial port that you can use for the GPS.
SoftwareSerial also works, so it should work with those pins.
The table shows the serial ports: https://www.arduino.cc/reference/en/language/functions/communication/serial/.
The USB-serial port is called "Serial", but I often use the name "SerialUSB" which is also allowed.
The serial port at pin 0 and 1 is called "Serial1".
Thanks for the response.
I can't get the serial port at pins 0 and 1, or SoftwareSerial at other pin pairs working. SoftwareSerial works fine on the Uno, but I can't get it working on the Leonardo for some reason.
Al
Please show your sketch.
I suppose - if it's written for an UNO - that the line
while(!Serial);
after Serial.begin
is missing. You must wait until the USB connection is established before you can send anything on the USB line. If you try to send something too early than Serial...
may fail completely.
From other projects I am aware of the need for the while(!Serial) line when using the Leonardo, but that is a good point -- and easy to forget.
Below is the little GPS test sketch which is from
https://lastminuteengineers.com/neo6m-gps-arduino-tutorial/ . I use the same pin connections on the Leonardo and the Uno, but only the Uno shows some output on the serial monitor.
Thanks for the help,
Al
#include <SoftwareSerial.h>
// Choose two Arduino pins to use for software serial
int RXPin = 2;
int TXPin = 3;
//Default baud of NEO-6M is 9600
int GPSBaud = 9600;
// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);
void setup()
{
// Start the Arduino hardware serial port at 9600 baud
Serial.begin(9600);
while (!Serial); //for Leonardo, wait for connection to become live
// Start the software serial port at the GPS's default baud
gpsSerial.begin(GPSBaud);
}
void loop()
{
// Displays information when new sentence is available.
while (gpsSerial.available() > 0)
Serial.write(gpsSerial.read());
}
Oops, sorry, I forgot something.
It is written here in the "Limitations" paragraph : https://www.arduino.cc/en/Reference/softwareSerial
The SoftwareSerial does not work with every pin.
If you don't use SoftwareSerial, but the real hardware Serial1 at pin 0 and 1, then it should also work.
Finally got both serial options working. Serial1, and also SoftwareSerial using pin 11 (for Rx) and 12 (for Tx).
It took me a while to realize the setup and commands with Serial1 are essentially the same as for Serial. And the key for SoftwareSerial was using the correct pins for Rx and Tx.
Thanks for your help, and patience!
Al
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.