How to Use NEO 6M GPS with RPI (Begginer Friendly)

Today you will learn how to use the Neo 6m GPS module with the raspberry pi. Now you can create your own GPS service to use on your travels.

Parts Required

  • Raspberry Pi
  • NEO 6M GPS Module
  • Male to male jumper wires
  • Breadboard

(If this is your first time using the raspberry pi and you want to learn how to ssh access you can do it: here.)

So let’s start with the connections:

Through this connection, the raspberry can receive information from the GPS module and can also supply it with power.

To configure the microcontroller you will need to execute the following code:

sudo nano /boot/config.txt

At the bottom of the file you will add the following configs (copy exactly as written here):

dtparam=spi=on
dtoverlay=pi3-disable-bt
core_freq=250
enable_uart=1
force_turbo=1

Now you just have to press ctrl+x and y to return to the command line.

RaspberryOS uses UART (universal asynchronous receiver-transmitter). To work with the GPS module we have to change this default characteristic.

To change it, execute the following code on the command line:

sudo nano /boot/cmdline.txt

And replace the code with the following code:

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles

Now you just have to save with the journey: ctrl+x and y (don’t forget this keyboard combination that you will use a lot).

To apply the changes, let’s reboot:

sudo reboot

(Sudo will run commands with elevated privileges. It allows you to became a superuser).

After the reboot, our GPS should start working.

To understand if the GPS module is working, you should see the red light blinking!
It is important that you are in a place with a GPS range (ideally outdoors or very close to the window).
Depending on the source of the module, it may take up to 6/8min to receive GPS Data.

When it gets the light blinks, go to the raspberry’s command line again and run the following code:

sudo cat /dev/ttyAMA0

Now your screen will look like the matrix movie intro, with several lines similar to this one:

GPS signal in the command line.

(Before you continue, check if there are too many empty spaces on the lines. Each space corresponds to data received from different satellites.

If most of them are missing, it means that you don’t have good GPS coverage in the current space. Move for an outdoor space for the first setup).

We will have to make a new configuration now.

Originally, raspberry uses a serial port for this “login console” If we want to use the serial port to receive GPS data, we need to disable the login console.

There are 2 serial ports on the raspberry: 1 and 0.

Now let’s use the following code to see where the serial0 port is on your raspberry.

ls -l /dev

If the result is this:

crw-rw-r-- 1 root netdev 20, 38 Jul 10 12:00 rfkill
lrwxrwxrwx 1 root root 7 Jul 10 12:01 serial0 -> ttyAM0
lrwxrwxrwx 1 root root 7 Jul 10 12:01 serial1 -> ttyS0

Your serial0 is linked with ttyAMA0. To disable this port you will need to use the following command:

sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service

If your output is this:

crw-rw-r-- 1 root netdev 20, 38 Jul 10 12:00 rfkill
lrwxrwxrwx 1 root root 7 Jul 10 12:01 serial0 -> ttyS0
lrwxrwxrwx 1 root root 7 Jul 10 12:01 serial1 -> ttyAM0

This already means that serial0 is linked to ttyS0. You need the code:

sudo systemctl stop serial-getty@ttyS0.service
sudo systemctl disable serial-getty@ttyS0.service

The configuration is done, let’s move on to the program.

Python To Decode GPS

To get started you will need the pynmea2 library.

This library is responsible for parsing and is compatible with python 2.7 and python 3.3 (library link: https://pypi.org/project/pynmea2/1.8.0/).

Now that you have the library you need, you are ready to start programming.

To do this you will use the following command:

sudo nano GPS.py

Then you paste the following code inside:

import serial
import time
import string
import pynmea2
while True:
port="/dev/ttyAMA0"
ser=serial.Serial(port, baudrate=9600, timeout=0.5)
dataout = pynmea2.NMEAStreamReader()
newdata=ser.readline()
if newdata[0:6] == "$GPRMC":
newmsg=pynmea2.parse(newdata)
lat=newmsg.latitude
lng=newmsg.longitude
gps = "Latitude=" + str(lat) + "and Longitude=" + str(lng)
print(gps)

Now you run the program through:

sudo python GPS.py

If everything is going as expected, you should have a latitude value and a longitude value.

From here you are ready to develop any script with your GPS location!

In the next tutorial, we will look at a practical application with a distance counter.

References

[1] – https://sparklers-the-makers.github.io/blog/robotics/use-neo-6m-module-with-raspberry-pi/

[2] – https://sparklers-the-makers.github.io/blog/robotics/use-neo-6m-module-with-raspberry-pi/

[3] – https://www.instructables.com/Raspberry-Pi-the-Neo-6M-GPS/

Leave a Reply

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!

Hello! We have noticed you are using an ad blocker. Our website is funded by advertising which allows you to access all our content for free. By disabling your ad blocker, you are contributing to the sustainability of our project and ensuring we continue to provide high-quality, useful tutorials. We appreciate your support!