Home > Microcontroller >Microcontroller Production > How to use the GPS module of STM32F103C8 to obtain location coordinates

How to use the GPS module of STM32F103C8 to obtain location coordinates

Source: InternetPublisher:奥特man123 Keywords: Microcontroller GPS module LCD display STM32 Updated: 2024/12/27

GPS stands for Global Positioning System and is used to detect the latitude and longitude of any location on Earth with precise UTC time (Coordinated Universal Time). The device receives coordinates from satellites every second, including time and date. GPS provides high accuracy and also provides other data in addition to location coordinates.

We all know that GPS is a very useful device that is very commonly used in mobile phones and other portable devices to track location. It has very wide applications in various fields, from calling a taxi at home to tracking the altitude of an airplane.

In this tutorial, we will interface GPS module with STM32F103C8 microcontroller to find the location coordinates and display it on 16x2 LCD display.

Required Components

STM32F103C8 microcontroller

GPS Module

16x2 LCD display

Breadboard

Connect the wires

GPS Module

This is a GY-NEO6MV2 XM37-1612 GPS module. This GPS module has four pins +5V, GND, TXD and RXD. It uses serial pins for communication and can be easily connected with the serial port of STM32F103C8.

pYYBAGMYVcqAe-2zAAMhfr5X3hQ522.png

The GPS module sends data in NMEA format (see the screenshot below). The NMEA format consists of several sentences, of which we only need one. This sentence starts with $GPGGA and contains coordinates, time, and other useful information. This GPGGA is called Global Positioning System Fix Data.

The following is a sample $GPGGA string and its description:

$GPGGA,104534.000,7791.0381,N,06727.4434,E,1,08,0.9,510.4,M,43.9,M,,*47

$GPGGA, HHMMSS.SSS, latitude, N, longitude, E, FQ, NOS, HDP, altitude, M, height, M, , checksum data

But in this tutorial, we use a TinyGPSPlus GPS library which extracts all the needed information from the NMEA sentences and we only need to write one simple line of code to get the latitude and longitude as we will see later in this tutorial.

STM32F103C8 pinout

The STM32F103C8 (BLUE PILL) USART serial communication ports are shown in the figure below. These are the blue ones (PA9-TX1, PA10-RX1, PA2-TX2, PA3-RX2, PB10-TX3, PB11-RX3). It has three such communication channels.

poYBAGMYVcWAVgqzAAl4ObxQVCs507.png

Circuit Diagram and Connections

pYYBAGMYVcGAPAoZAAIGs_OIIlM872.png

Circuit connection between GPS module and STM32F103C8

pYYBAGMYVb2AQy8gAAAN0rrc-L0227.png

Connection between 16x2 LCD and STM32F103C8

poYBAGMYVbqAf1nkAAA4ZmRyAaU075.png

The whole setup looks like this:

pYYBAGMYVbaAN9kcAAdRX34H6Qs600.png

Programming STM32F103C8 for GPS module interface

The complete program to find the location using GPS module using STM32 is given at the end of this project. STM32F103C8 can be programmed using Arduino IDE by simply connecting it to the PC via USB port. Make sure to remove the pins TX and RX while uploading the code and connect them after uploading.

To interface GPS with STM32, first we have to download a library from GitHub link TinyGPSPlus. After downloading the library, it can be included in Arduino IDE through Sketch -> Include Library -> Add .zip Library. The same library can be used to interface GPS with Arduino.

So first include the necessary library files and define the pins for the 16x2 LCD:

#include

Then create a gps object called TinyGPSPlus class.

TinyGPSPlus Global Positioning System;

Next, in void setup, use Serial1.begin(9600) to start serial communication with the GPS module. Serial1 is used as the Serial 1 port (Pins-PA9, PA10) of the STM32F103C8.

Serial1.start(9600);

Then the welcome message is displayed for a while.

  lcd.start(16,2);
  lcd.print("Circuit Abstract"); 
  lcd.setCurs

Next in void loop(), we receive the latitude and longitude from GPS and check if the received data is valid and display the information in the serial monitor and LCD.

Check if the available location data is valid

  loc_valid = gps.location.isValid();

Receiving latitude data

lat_val = gps.location.lat();

Receiving longitude data

  lng_val = gps.location.lng();

If invalid data is received, the serial monitor displays "*****" and the LCD displays "Waiting".

  if (!loc_valid) 
  { 
    lcd.print("W

If valid data is received, the latitude and longitude will be displayed on the serial monitor as well as on the LCD display.

   lcd.clear(); 
    Serial.println("GPS reading: "); 
    Serial.print("Latitude: "); 
    Serial.println(lat_val, 6); 
    lcd.setCursor(0,0); 
    lcd.print("Latitude:"); 
    lcd.print(lat_val,6); 
    Serial.print("Longitude: "); 
     Serial.println(lng_val, 6); 
     lcd.setCursor(0,1); 
     lcd.print("Length: "); 
     lcd.print(lng_val,6); 
     delay(4000);

The following function provides a delay in reading data. It keeps looking for data on the serial port.

static void GPSDelay(unsigned long ms) 
{ 
  unsigned long start = millis(); 
  do 
  { 
    while (Serial1.available()) 
    gps.encode(Serial1.read()); 
  } while (millis() - start < ms); 
}

Finding Latitude and Longitude using GPS and STM32

After building the setup and uploading the code, make sure to place the GPS module in an open area to receive the signal quickly. Sometimes it takes a few minutes to receive the signal, so wait for a while. When the GPS module starts receiving the signal, the LED will start blinking and the location coordinates will be displayed on the LCD display.

pYYBAGMYVa-AfrS8AAbn5WtRgbw915.png

You can verify the latitude and longitude of the location using Google Maps. Just turn on GPS Go to Google Maps and click on the blue dot. It will show the address with the latitude and longitude as shown in the image below

poYBAGMYVauARECIAAV0zuLNT1Q953.png
#include

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号