barnamah
barnamah

Reputation: 59

Reading serial data from an input pin in Arduino

I have a GPS serial signal coming from Dout of XBee. I checked it with oscilloscope and signal exists.

How can I read serial signal with Arduino (I assume I should feed it to an input pin)?

And then print it to Serial Monitor.

Upvotes: 0

Views: 1611

Answers (1)

barnamah
barnamah

Reputation: 59

I found the solution to read the GPS signal sent to the RX pin.

It is in the Arduino IDE under menu FileExamplesCommunicationsSerialEvent

String inputString = "";         // A string to hold incoming data
boolean stringComplete = false;  // Whether the string is complete

void setup() {
  // Initialize serial:
  Serial.begin(9600);

  // Reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // Print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);

    // Clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
  hardware serial RX.  This routine is run between each
  time loop() runs, so using delay inside loop can delay
  response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // Get the new byte:
    char inChar = (char)Serial.read();

    // Add it to the inputString:
    inputString += inChar;

    // If the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

Now connect the wire that has serial signal to the RX (pin 0) and run Serial Monitor and you will see something like this:

$GPGLL,5652.36437,N,08901.52702,W,225356.00,A,D*73
$GPGSV,4,1,15,07,42,306,33,08,16,110,33,09,53,253,37,10,05,281,23*72
$GPRMC,225357.00,A,5652.36445,N,08901.52698,W,0.145,,180315,,,D*62

Upvotes: 0

Related Questions