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 !

47 Replies to “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 ?

  7. hello dude ! thank you for sharing this . can i ask where will i put the “” ? i have my directory gps/www
    but i cant run the “sudo python” coords.txt no file or directory thank you for your help d

    1. hey i have an update its already running but it i dont see any markers indicating that im here or any line . but i have my map when i put my ip then press track nothing happen

      1. Hi kuchi !
        Thank you for the comments.
        There’s sort of a bug in the map that I need to fix – a long time it is to fix . Try to center the map at YOUR location. When starting, the map is at Castelo Branco@Portugal.

  8. Hi friend
    when i am open the /etc/inittab it’s created new file there is content on the file. Can you please send data to me
    what data inside the file.
    And i am not getting gps data when i am typing cgps -s

  9. Hi,
    is it possible to use this GPS also as a precise time source?
    Can you please also add this part in your tutorial?

  10. Hi feiticeir0
    I follow your tutorial but I get the trouble that when I open RPITracking.html on raspi I get the red button and cannot click Track
    The cgps OK
    I cat the file coords.txt have the “lat lon” value
    Realoading nginginx.server OK
    ping OK too

    So why I cannot track my raspi

          1. In the box next to the red button, put the Raspberry PI IP address and press track (the box comment is in Portuguese… I need to change that !

          2. I try and put the Pi IP to that “box” already then click “track” but no luck It’s still red and I cant see the marker !_!

          3. I see you’re using the Raspberry PI to display the map. Can you ping the RPi IP ?
            i.e: ping ?

          4. Yeah indeed I can ping because I connect from my laptop to my RPi via VNC

  11. i did type in the ip addres and pressed track The light is green beside the ip address.
    But no markers visible in the map. I change the original position in portugal to my position, but no gps markers in the map.
    cgps – s is working
    coord.txt is updating with coordinates from the python file
    What else can this be?

    using raspberry pi 3

    1. Hello Samuel ! Thank you for the comment.

      Everything seems fine. Can you please, see the browser console if any errors are displayed ? Ctrl+Shift+j (or F12 and choose console) for Chrome or F12 in Firefox

      Thank you

  12. Hi,
    Thanks for this great tutorial. I am using the following GPS, similar to the one used in the tutorial.

    How do I “change the /etc/inittab file” ?
    I have followed all the other steps in the tutorial and also installed the GPS applications.
    When I run the command “cgps -s”
    I get the table with all the rows, Time, Latitude, Longitude, Speed, Heading etc. But next to each heading it states n/a.

    Could you please tell me where I might be going wrong
    Thanks in advance.

    1. Hi Ikram!

      Thank you !
      Now, some questions so I can understand a little more of your problem

      First: are you wiring it correctly ? RX from GPS goes to TX from RPi and TX from GPS goes to RX from RPi.
      Second: are you outside so your GPS can pick up satellites ?
      Third: About the /etc/inittab file – can you locate the file ? You just need to comment a line, as explained in the tutorial. Is the file there? What distribution are you using ? This was done with Raspbian available in 2014 – Wheezy – have to check now !

      1. Hi,
        Sorry for the late reply. I have been following your tutorial.
        I have connected the GPS module correctly. When you state “After that, change the /etc/inittab file and comment a line so it do not spawn a login console:”

        How do I change this file or where is it located?
        When I run the /etc/inittab command, I get a error message stating ” No such file or directory”.

        Could you tell me what I may be doing wrong? I think since I am not able to change this file, the GPS is giving me no reading s for values. How else can I change or create this file and in which folder should it be located?


        1. Hi Mohammed!

          inittab is not a command, is a file. You have to edit it. The /etc/inittab is just the path. The file is located in etc directory.
          The full command should be (without the quotes) “sudo vi /etc/inittab” or “sudo nano /etc/inittab” .

          Reminder: This was for a older version of raspbian and using sysVinit. Newer versions (like sketch) use systemd, hence the missing of the file.
          You can use raspi-config, go for option 5 (interfacing options) and then option P6 (Serial) and disable the login shell over serial and see if it works.

          Thank you

  13. Hi sir!
    I’m currently doing this command
    ” Execute gpsd (The GPS daemon)
    gsudo gpsd /dev/ttyAMA0 -F /var/run/gpsd.sock ”
    but i seems to have problem said
    ” -bash: gsudo : command not found ”
    can u help me solve the problem? it would be very helpful

    1. Hi Ridhwan !

      gsudo is not installed by default . What you should be using is sudo and not gsudo.. It was a typo.. My mistake..

  14. Hey eiticeir,

    First of all i like to congratulate for this wonderful project its great!

    Now i like to share my problem here
    1. gps coordinates are getting saved in file perfectly
    2. map is getting loaded successfully
    But when i am entering ip address of raspberry pi then there is no change in the status , I am not able to see any icon showing my current position

    I am using raspbian jessie and same devices as mention in this tutorial. I know i am little away from achieving this. if you can help me .It would be great thank you


    1. Hi David !

      Everything should be fine.. This has a little bug (to revisit in the future) that is – the map, when displayed, it shows my home town. You need to zoom out and place it in your part of the world were you are trying the code.. You probably will see the marker there…

      But, if you’re already at your position and don’t see nothing, please check the error logs for the websever and the javascript console – F12 on Chrome and Firefox – if there’s any errors… Please reply to me and I’ll try to help you ! Sorry for the delay in the answer.. This stopped sending emails and I don’t know why !

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.