MATLAB Answers

NEO-6M GPS connected via Arduino to Matlab

21 views (last 30 days)
Joppe Buntsma
Joppe Buntsma on 13 Jan 2017
Edited: Amir Mehdi Yazdani on 8 May 2018
Dear all,
I have connected an NEO-6M GPS device on my Arduino UNO and I was able to retrieve the GPS data to the Serial Monitor via the arduino code ( see code ).
Now I would like to access the GPS data in Matlab. I've added the Arduino Hardware Support Package and got it working through various basic examples. But how do I access devices, like my GPS sensor, which are connected to the Arduino with a virtual serial connection?
For example; how do I set the baud rate, start the connection and how can I pull up the data with is set available by the GPS device?
The TX of the NEO-6M GPS is connected to digital port 11, while the RX is connected to digital port 10 on the Arduino.
Thanks in advance.

Answers (1)

Maitreyee Mordekar
Maitreyee Mordekar on 23 Jan 2017
Edited: Maitreyee Mordekar on 25 Jan 2017
Hi Joppe,
The SoftwareSerial library is used to send the GPS data to the Arduino board and the Arduino board streams the same data to the PC (via the COM port). This data on the system's COM port is the one that is displayed when the Serial Monitor window is opened using the Arduino IDE.
Thus, to fetch the required data from Arduino, one will have to just read from the COM port to which the Arduino is connected. You can follow the procedure mentioned below fetch the data from the GPS sensor using Arduino-
  1. You will, thus, have to declare a serial port using the serial command and set the specified properties.
  2. Also, you can change the properties of the serial port using the set(serial) function.
  3. The following step will be to connect the serial port object to the device using fopen(serial) function.
  4. The below function fgets(serial) will help you fetch the file string, i.e., the NMEA strings that are obtained from the GPS sensor.
  5. The next step will be to disconnect the serial port object from the device using the fclose(serial) function.
  6. The final step will be to delete the serial object from the workspace using the delete(serial) function.
An example code snippet that will yield the NMEA strings on MATLAB is as below.
s=serial('COM4', 'InputBufferSize', 600, 'BaudRate', 9600); % Create a serial object with specified properties
set(s,'Terminator','CR/LF') ; % Set the Terminator Character
fopen(s); % Open the serial port
data = cell(0);
while(i<=17) % Read 17 lines
if (s.BytesAvailable) % Check if bytes are available in the buffer
data{i}=char(fgets(s)); % Read the data and convert ASCII to character
i=i+1; % Increment the counter
fclose(s); % Disconnect the serial port object from the device
delete(s); % Delete the serial object
Note that, the COM port that was allocated to the Arduino was 'COM4' on my machine. This parameter value will change depending on the COM port that Arduino is assigned on the user's system.
Since, from the link provided, the number of lines at the output of the Serial Monitor is seventeen, the counter has been set to 17 (This includes the blank lines). However, this code can be customized depending on the requirement.
Thus, the 'data' cell array will hold the strings that are fetched by reading each line from the 'COM4' port.
Amir Mehdi Yazdani
Amir Mehdi Yazdani on 8 May 2018
Dear Maitreyee Mordekar
Thanks for your useful post. Can you offer some direction to read NMEA GPS data on serial port of Raspberry Pi 3?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!