Search code examples
serial-portgpsesp32android-gpsarduino-esp32

ESP32 Connected to GPS module. No serial out unless holding down reset button


I'm new to Arduino and I'm having some trouble. I have a 16E TTL GPS module connected to the RX and TX pins on my NodeMCU ESP32 board and have a simple Arduino sketch i wrote to output the data to the serial monitor.

String data = "";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  data = Serial.read();
  Serial.print(data);
  delay(500);
}

I am only getting the GPS data in the serial monitor while I am holding down the RST button on the board and an output of "-1" every cycle otherwise.

Serial Monitor Output

I have tried looking up the problem but I cant seem to find a solution and I have tried figuring out how to use serial in detail but I'm admittedly confused. I expected the data to just be printed every loop.


Solution

  • You're using Serial both to output debugging messages and to talk to the GPS.

    The RX and TX pins that you connected the GPS to are the same serial port as the USB serial chip connects to. Every time you write something Serial it goes to both the USB port and the GPS. So when you read anything from the GPS, you immediately write it back to it.

    You can only use the serial port for one thing at a time. Since it's connected to a USB serial chip, your best bet is to use a second serial port for the GPS.

    For instance:

    #define RX1_PIN 9
    #define TX1_PIN 10
    
    String data = "";
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(9600);
      Serial1.begin(9600, SERIAL_8N1, RX1_PIN, TX1_PIN);
    }
    
    void loop() {
      data = Serial1.read();
      Serial.print(data);
      delay(500);
    }
    

    You should set RX_1PIN and TX1_PIN to be whatever pin numbers are convenient for you; just be sure that they're pins that are available on your board and aren't being used for something else.