Connect a GPS to the Raspberry PI

After seeing an article about the Raspberry PI in the atmosphere with the PI in the Sky Project, I’ve decided why not try it ? Since I don’t have access to a telemetry board, I’ve decided to go ahead with off the shelf components. Well, this is the first step

Portuguese version

First, how to track the Raspberry PI with the GPS


Raspberry PI B/B+ (I’ve used both)

1 GPS (I’ve tried with two GPS – both working perfectly):


To connect the GPS to the Raspberry PI we’re going to use the GPIO. The following table shows the GPIO PINs for RPI version B where to connect the GPS Pins.

Raspberry PIN GPS PIN
2 – 5v VIN/VCC
8 – TX RX
10 – RX TX

Note: The  TX and RX switch when connecting to GPS:: RX -> TX e TX -> RX

Here’s RPI B pinout





  GPS GY-NEO6Mv2 to PI wiring


Some images of the GPS wiring to  Raspberry PI version B



To connect the GPS EM-406A I had to buy a  JST SH Jumper 6  cable – with 6 pins to fit in the GPS  – although we only need 4 pins.


GPS EM-406A wiring



I’ve used the RPI version B+ for this GPS. No explanation – just because at this time the B+ was the only one I had available.  Both PINs in RPI B or B+ are the same and the wiring is also the same. The only thing changing is the GPIO  of RPI  B+.


Power on the PI

As soon you have a prompt available, it is necessary to change some configurations.

The UART port, by default, is used for logins. We need to free it for the GPS.

Edit the file /boot/cmdline.txt and change:


After that, change the /etc/inittab file and comment a line so it do not spawn a login console:

Comment the following line (It should be the last one):

After this changes, reboot the PI

After the reboot, install the GPS applications

Execute gpsd (The GPS daemon)

Now, let’s try the GPS (place it outside so it can work better)

And after some seconds you should get some signal

Now you should have GPS in your PI. But lets not stop here and create a map of the RPI locations.

Update a map with the current RPi location

It wont do us any good if the have a GPS in the RPI and not create some fancy map with it.

Create a map with the GPS coordinates

By installing the GPS applications, we also get some python modules. Let’s use those to get some info from the GPS.

My solution was with a Web server, the python app reporting the coordinates to a text file and a Web application in another computer (or in the PI – more on that later) getting them and creating a map with PHP and Ajax.

In the PI, install a Web server. I’m going for nginx

Configure nginx

This is the first time I’m working with nginx. I decided to use it because is much less resource hungry than apache.

For security concerns, lets change nginx listening port. Instead of 80, lets use 2121 (for example).

Change the line


Change document_root

In the line that says root, change to

Note: Don’t forget to create the directories gps/www

Since nginx runs with the  www-data group, lets add the PI (or your user) to the group because of permissions – we want nginx to be able to read and create files.

Note: You need to logout and login again

Change the permissions so that any file created by nginx can be read by the group – any created file by nginx will belong to the www-data group – (setgid)

Restart nginx

Note: Do not forget that, as configured, to connect to nginx, you must use the 2121 port:



Install Python module gpxpy

All the code is in my  bitbucket account (with Portuguese comments).

The directory contains a file named .This file is to keep in the RPI, wherever you want, but you must later create a www directory (accessible by nginx) – where the this script will put the file coords.txt that will contain the current coordinates.

To run it, just:

Before running it, please make sure the GPS is connected and working. You can use the above commands to test it: cgps -s

For informations about gpsd Python modules, visit the page.

Computer (the one that’s going to display the map)

In this computer is necessary to install a Web server (with PHP support) and Internet access so it can display the maps.

In the bitbuck account you can find all the necessary files.

It’s only necessary to change 2 lines, one on each of the following files::

– getcoords.php : change the line $rpi_host to your RPI IP

– pingPHP.php :  change the line $host again, to match your RPI IP

Update: With my last version, there’s no need to change any file. Just put your RPI IP in the input box and press “Track”

Auxiliary files:



Everything is configured now.

IMPORTANT NOTE: It may seem that the map is not updating, but it is. Just zoom out with the mouse and center the map at your location. Since it is necessary to put some coordinates when creating the map, I’ve placed the map at my home town (Castelo Branco – Portugal). I will update this in a new version. (Thank you Nuno Oliveira)

In a browser, execute the file RPITracking.html and wait for the markers to start appearing with your RPI position.

In this version I used markers instead of a line. I get it that after a while it gets messy and confused… In a newer version I’ll use a line.

Here’s the result I’ve tried this morning (Image from the previous version)

GPS Track

Use the RPI to show the map (in update)

Instead of using another computer to get the RPI coordinates and show them on the map, we can use the RPI to do this job – and access it with a browser –

Change nginx

Install  PHP

Next, edit the ficheiro /etc/nginx/sites-available/default

and uncomment the following lines:

Save and restart nginx

To work, the files you would put in the computer, put them in the RPI and you should get the map working.  As you have configured the nginx document_root, that’s were you need  to put the files. A side note – and since everything is in the same place, there was no need to use CURL – on PHP – to get the coordinates, but it will do no warm and it will work.

PI Mobility

For all this to work, the RPI is needed to work on batteries.

In my case, I’m using LIPO batteries  – with 2h and 30m duration – which is not bad…for now is enough .

My material was (Portuguese stores):

Lithium Ion Polymer Battery – 3.7v 1200mAh

– PowerBoost 500 Basic – 5V USB Boost @ 500mA from 1.8V+ (increasing 3.7v from the battery to 5v for the PI)

– USB/DC Lithium Polymer battery charger 5-12V (3.7/4.2v cells) (to recharge the batteries)

With the mini-usb cable, you connect all to the RPI. I believe is not the most efficient way to power the RPI, but after a few days reading, it’s all I came up. I’m not an electronics expert and if there’s some one out there with a better idea, please come forward !


Before I’ve settled with this solution, I’ve tried with AA batteries – 1.5v – a 6 AA batteries adapter and a Pololu 5v Step-up/Step-down voltage regulator 5v to control the voltage (6 * 1.5v = 9v). But, don’t know what went wrong, and only got 40m…

RPI alimentado com pilhas

Saving power

We can save about 20mA (more or less) turning off the PAL/HDMI output (assuming we’re not going to use it) .

To do so, just add the following line to the RPI boot (Usually, adding the following line to  /etc/rc.local)

tip via


This project (for now) as the downside that, in the computer used to view the maps, an Internet connection is necessary – because of the maps. There’s another reason that, both computers must be in the same network. I’m exploring XBees. The models I’ve purchased have 1.5km range – updates after some testing.

Hope you like it. Errors or mistakes, please advise !

21 thoughts on “Connect a GPS to the Raspberry PI

  1. Dear,

    Could we use a offline google map for this project ?

    Could you please tell me about the accurate rate of both GPS device you have mention ?

    1. Hi Le Toi !
      I’ve used OpenStreetMaps for this project to run away from Google Maps. I’ve tried hard to search for a solution to Offline maps, but I couldn’t find any that I could make it work. I’m due to review this project and try again for a offline maps solution.

      For the accuracy of these GPS, I believe they are in the order of 3-4m . Can’t be sure right now, but is within these figures.

      Cheers and thank you !

    1. Hi Sergio.

      Did you restart ngynx after you change the port ?

      Try the nginx default port:
      nc localhost 80

      Do you have a firewall active ? Please check

        1. Hi !

          What problems are those ?
          nginx runs as www-data. You need to add your user to that group:

          gpasswd -a www-data pi

          logout and login again.

          Those directories where you have the files need to belong (or at least the www-data group) to the group:

          chgrp www-data -R
          chmod g+w -R

          Hope it helps

    1. Hi Dhani !
      Can you get GPS fix ? You can check that with the small utility cgps .

      Where are you stuck ?

  2. hello.

    when i running code sudo python

    it’s tell that no such file ‘/coords.text’. how to fix this problem.


    1. Hi man4118.
      It’s a file that should be available in the directory, but it should have been created if it not exists.

      To fix it, just change the line:

      coords = open (“www/coords.txt”,”w”)


      coords = open (“www/coords.txt”,”w+”)

      (just add the plus sign after the w)
      This is in the file (on the RPi).

      Hope it helps. Let me know if you need anything more !

      1. hi FEITICEIR0.

        thank you for your reply.

        i already add the plus sign and its worked.

        after i running the sudo python , its show an error
        ” Traceback (most recent call last):
        File “”, line 27, in
        AttributeError: dictwrapper instance has no attribute ‘lat’ ”

        help me to fix this problem.

        thank you.

        1. Hi MAN4118

          That appears to me is a problem with the Python libraries for the gpsd.
          Are you getting a lock for the gps ?

          Try another version of the gpsd daemon


    Yes, gps already lock and i get gps data when i run code cgps -s.

    what do you mean another version of the gpsd daemon?


    1. Hi MAN4118.

      Like I said, after a few searches, that appears to me to be a problem with the gpsd python libraries, hence I recommended you to try another version of the gpsd, but I guess that’s the latest version.
      Have you manage to sort that problem ?


  4. when I run code sudo python
    it’s tell that no such file ‘/coords.text’

    I have tried: “To fix it, just change the line:
    coords = open (“www/coords.txt”,”w”)
    coords = open (“www/coords.txt”,”w+”)”

    In addition: previously, chmod g+s www returned a result chmod: No such file or directory.

    Please help, thanks.

    1. Hi Piuser !

      Have you created the www directory ? In the post I wrote:

      “Note: Don’t forget to create the directories gps/www”


      “The directory contains a file named .This file is to keep in the RPI, wherever you want, but you must later create a www directory (accessible by nginx) – where the this script will put the file coords.txt that will contain the current coordinates.”

      The code in the bitbucket account is changed – there is no more w, just w+ ! I’ll change the code in the post.

      Check if you have the www directory created

  5. Hi there, so I’ve got the file as well as the php and html scripts in the /home/pi/gps/www folder on the Raspberry Pi. My coords.txt file is getting updated with my co-ordinates of my location. However, when I navigate to the Pi IP address:2121, a map is displayed, however, the button “track” after entering the pi’s IP seems to be greyed out. In addition, no markers are appearing at my location.

    What could be the problem?

    1. Hi Bivek !

      As soon I have time, I’ll look into it and try to figure why it’s grayed out. Nonetheless, you have a developer console in any browser that you can invoke and see any errors that may be happening and if you want, post them here.
      In the code there’s nothing to gray . The colors are just like that – a pale blue

  6. Hello,
    I need some helps. I have followed the tutorial but I cant find the inittab file on /etc directory. What should I do ?

Leave a Reply

Your email address will not be published. Required fields are marked *