Library 27- Read GPS data on STM32F4 devices

Recently I got a cheap GPS module from ebay, NEO-6M. They said that this is flight controller, but position fix is good only on really flat area. When I made a library, I’ve connected my SDcard to STM32F4 to save data, result are here.

To communicate with STM32F4 device, USART is in use. Most GPS modules have by default 9600 baudrate and 1Hz refresh rate of sending data. I got my GPS configured in 115200 baudrate and 5Hz refresh rate, so when you try example, make sure that you change your baudrate to correct value.

When you plug your GPS to power, it can take some time to get first valid signal from satellites. This time can be up to one minute, according to where you are trying to get signal.

If you are interested in HAL version of the same library, click here.

GPS Library


  • Parse GPS data
  • Supported NMEA 0183 standard
  • Works with GPGGA, GPRMC, GPGSA, GPGSV NMEA statements
  • Version 1.1 – August 22, 2014
    • Calculate distance between 2 coordinates
    • Calculate bearing between 2 coordinates
  • Allows you to select custom NMEA statements


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx USART
    • MISC
  • TM
    • TM USART
    • TM GPIO
    • attributes.h
    • defines.h
GPS Default pinout to STM32F4 device
GPS module STM32F4 Description
RX PB6 Receive line for GPS, actually not needed
TX PB7 Transmit line for GPS, this is needed

This pins are for USART1, if you want your custom USART, add this line below in your defines.h file and edit them:

My library basically checks to get all data that are need to display all possible data which library supports. For that, it needs 4 NMEA statements to be returned from GPS module:

  • GPGGA: Global Positioning System Fix Data
  • GPRMC: Recommended minimum specific GPS/Transit data
  • GPGSA: GPS DOP and Active Satellites
  • GPGSV: GPS Satellites in view

GPGGA and GPRMC should be returned from all GPS receivers, but others 2 are maybe not. Everything depends on GPS configuration. There are some tools on net, to change GPS module’s settings, like one here.

If for some reason your GPS module does not provide all of your statements, you will not be able to get all possible data. To disable any of your statements, copy statements below in your defines.h file and uncomment line you don’t want to use:

When you enable these defines, my library will ignore statements, which are disabled and response like we have new data.

Distance and bearing

These two options were added in version 1.1.

Distance between 2 points can not be provided from GPS receiver, so we have to calculate it. This is useful, if you have let’s say quadcopter and you want to know, how far you are from quad. FIrst coordinate will be quad’s current position from GPS and second point will be set before you start with your flight.

If you want to make let’s say “Return home” function on quadcopter, bearing comes to be very useful. GPS module tells you your direction (included in library) where you are currently moving. Still, first coordinate is from quad’s GPS receiver, and second is where quad started with flight. When you activate “Return home” function, quad will know how far he is from “home” and in which direction (bearing) he has to move according to the north. Then, you just have to compare your calculated bearing with actual direction provided from GPS. And you will know, if he needs to go more left, right, etc.

Custom GPS statements

Library supports by default 4 statements. It assumes that every GPS module should output these 4 statements. But it may happen, that your module outputs data that you need, but are not available with my library by default.

Here is why custom GPS statements come handy. It is possible to define custom statements which will be parsed as strings from GPS receiver directly to user.

For example, let’s say that your module outputs $GPRMB sentence (navigation). From that statement, you want to get “Steering value”, which can be “L” or “R” for Left or Right. Steering value is part 3 inside GPRMB sentence. If you want to include this into your output, you can do this:

This statement is now added into main GPS_Data structure and also, pointer to small GPRMB.3 structure is returned.

You just have to make sure, that in this case, GPRMB statement is really returned from module. If module will not return this statement, you will not be able to read anything from library because it will never happen that everything will be updated before new data are available.

How to handle that data, you should look at bottom example about custom GPS statements.

Functions, structs and enumerations


  • GPS data are displayed to to user on USART3 at 115200 bauds
    • TX pin is PB10

Example below produces something similar to:

Example 2

  • Works on STM32F429-Discovery with included LCD
  • Default baudrate for GPS module is 115200
    • PA10 is used for receive data

Example 3

  • Shows how to use custom NMEA statements for your GPS module

GPS Custom result output

GPS Custom result output

View projects at Github, download library below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...