I have Arduino Nano 33 BLE Sense Rev2 with RFD 868x modem connected trough TX and RX pins. I need to connect GPS module, but most of them uses the same TX and RX pins. What would be the simplest way to connect GPS module? My goal is to get GPS data and send it trough RFD 868x modem.
You need at least two serial ports. If a second hardware serial port is not available, then try SoftwareSerial (which on many older MCUs does not support serial Baud rates above 38400).
There is not need to use Software Serial on the Nano33BLEsense as there are multiple hardware serial ports. There is Serial1 on the RX/TX pins, and you can add additional ports as well.
You can play with this sketch.
I am testing with a usb to ttl converter connected to pins 3 and 4 and Cool Term as the terminal program on my PC. You can also verify Serial1 by connecting the converter to those pins.
Serial is the usb connection to the ide monitor.
Serial1 is the ttl output on 0/1
mySerial is the ttl output on 3/4
//pin4 is Tx and pin3 is Rx
//cross connect to receiver 3 to receiver Tx and 4 to receiver Rx
UART mySerial(digitalPinToPinName(4), digitalPinToPinName(3), NC, NC);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
mySerial.begin(115200);
Serial.begin(115200);
Serial1.begin(115200);
while(!Serial);
for (int i = 10; i > 0; i--) {
Serial.print(' ');
Serial.print(i);
delay(500);
}
Serial.println();
}
void loop() {
if (mySerial.available())
{
char val = char(mySerial.read());
Serial.print(val);
Serial1.print(val);
mySerial.print(val);
}
if (Serial.available())
{
char valX = char(Serial.read());
Serial.print(valX);
Serial1.print(valX);
mySerial.print(valX);
}
}
Using your answer and Chat GPT I have managed to make a code which works for me.
Here's the wiring guidance for connecting your Arduino Nano 33 BLE Sense Rev2, GPS module, and RFD 868x modem to work with the code provided.
Wiring Overview:
GPS Module will be connected to the custom hardware serial port (mySerial), which uses pins 3 (RX) and 4 (TX) on the Arduino Nano 33 BLE Sense Rev2.
RFD 868x Modem will be connected to the default hardware serial port (Serial1), which uses pins 0 (RX) and 1 (TX) on the Arduino Nano 33 BLE Sense Rev2.
Wiring Details:
1. GPS Module (connected to mySerial)
GPS TX (Transmit) → Arduino Pin 3 (RX)
The Arduino reads the data from the GPS, so the GPS’s TX pin must connect to the Arduino’s RX pin.
GPS RX (Receive) → Arduino Pin 4 (TX)
The Arduino can optionally send data to the GPS (e.g., for configuration), but this is not always needed. This connects the GPS’s RX to the Arduino’s TX.
GPS VCC → Arduino 3.3V (or 5V depending on your GPS module, but 3.3V is safer for Nano 33 BLE Sense)
GPS GND → Arduino GND
2. RFD 868x Modem (connected to Serial1)
Modem TX (Transmit) → Arduino Pin 0 (RX)
The modem sends data to the Arduino, so the modem’s TX connects to the Arduino’s RX.
Modem RX (Receive) → Arduino Pin 1 (TX)
The Arduino sends data to the modem, so the modem’s RX connects to the Arduino’s TX.
Modem VCC → Arduino 3.3V (or an appropriate external power supply, if required)
Modem GND → Arduino GND
Code for Arduino
#include <TinyGPS++.h>
#include <Arduino.h>
// Define the GPS Serial on pins 4 (TX) and 3 (RX)
UART mySerial(digitalPinToPinName(4), digitalPinToPinName(3), NC, NC);
// Create an instance of the TinyGPS++ object
TinyGPSPlus gps;
void setup() {
// Start Serial for monitoring in Serial Monitor, but only if USB is connected
Serial.begin(9600);
// Start mySerial (GPS)
mySerial.begin(9600); // Set baud rate for your GPS module (usually 9600)
// Start Serial1 (Modem)
Serial1.begin(57600); // Set baud rate for the modem (adjust if needed)
// Check if Serial is connected, but do not block if not
if (Serial) {
Serial.println("GPS to Modem Communication Started");
}
}
void loop() {
// Check if data is available from the GPS module
while (mySerial.available() > 0) {
char c = mySerial.read();
gps.encode(c); // Feed data into the GPS parser
// Print raw data to Serial Monitor for debugging (only if Serial is available)
if (Serial) {
Serial.write(c);
}
// If a full GPS sentence has been parsed, send the data over the modem
if (gps.location.isUpdated()) {
if (Serial) {
Serial.println("\nGPS Data Parsed:");
}
// Get latitude and longitude
double latitude = gps.location.lat();
double longitude = gps.location.lng();
// Print to Serial Monitor if available
if (Serial) {
Serial.print("Latitude: ");
Serial.println(latitude, 6);
Serial.print("Longitude: ");
Serial.println(longitude, 6);
}
// Send the GPS data over the modem via Serial1
Serial1.print("Latitude: ");
Serial1.print(latitude, 6);
Serial1.print(", Longitude: ");
Serial1.println(longitude, 6);
}
// Optional: Send any other GPS data (date, time, altitude, etc.)
if (gps.date.isUpdated()) {
if (Serial) {
Serial.print("Date: ");
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print("/");
Serial.println(gps.date.year());
}
Serial1.print("Date: ");
Serial1.print(gps.date.month());
Serial1.print("/");
Serial1.print(gps.date.day());
Serial1.print("/");
Serial1.println(gps.date.year());
}
if (gps.time.isUpdated()) {
if (Serial) {
Serial.print("Time: ");
Serial.print(gps.time.hour());
Serial.print(":");
Serial.print(gps.time.minute());
Serial.print(":");
Serial.println(gps.time.second());
}
Serial1.print("Time: ");
Serial1.print(gps.time.hour());
Serial1.print(":");
Serial1.print(gps.time.minute());
Serial1.print(":");
Serial1.println(gps.time.second());
}
if (gps.altitude.isUpdated()) {
if (Serial) {
Serial.print("Altitude (meters): ");
Serial.println(gps.altitude.meters());
}
Serial1.print("Altitude (meters): ");
Serial1.println(gps.altitude.meters());
}
}
}