Introduction: Interfacing GPS Module With Raspberry Pi
Hey Guys!! Are you wishing to interface a GPS module with Raspberry Pi? But facing some difficulty to do it? “Don’t Worry, I am here to help you out! You can start by using the following parts:
Supplies
Step 1: Connect Raspberry Pi With PC
First of all, connect your Raspberry Pi Board with a PC. You can visit https://www.raspberrypi.org/blog/getting-started-raspberry-pi/ for the information regarding the installation of Raspberry Pi Imager.
Step 2: A Brief Info. About UBlox NEO-M8N GPS Module
This is an UBlox NEO-M8N GPS Module with Ceramic Active Antenna. This GPS Module has a 72-channel Ublox M8 engine in the receiver. The module has 4 pins: VCC (Supply Voltage), GND (Ground), Tx (Transmitter), and Rx (Receiver).
This module provides nonstop NMEA (National Marine Electronics Association) data strings to the TX pin resulting in GPS information. To know more about this module, you can download its datasheet here.
Step 3: Interface GPS Module With Raspberry Pi
For interfacing, make the connections as follows:
- Connect Vcc of GPS module to Power Supply Pin No.2 (5V) of Raspberry Pi.
- Connect Tx (Transmitter Pin) of GPS module to Pin No.10 of Raspberry Pi.
- Connect GND (Ground Pin) of GPS module to Pin No.6 Raspberry Pi.
You can also choose other Raspberry Pi boards, but be sure to check out for the appropriate pin numbers while making connections.
Step 4: Set Up the UART in Raspberry Pi
The first thing we will do under this is to edit the /boot/config.txt file. To do this, run the commands below:
sudo nano /boot/config.txt
At the bottom of the config.txt file, add the following lines
dtparam=spi=on
dtoverlay=pi3-disable-bt
core_freq=250
enable_uart=1
force_turbo=1
ctrl+x to exit and press y and enter to save.
The second step under this UART setup section is to edit the boot/cmdline.txt
I will suggest you make a copy of the cmdline.txt and save first before editing so you can revert back to it later if needed. This can be done using;
sudo cp boot/cmdline.txt boot/cmdline_backup.txt
sudo nano /boot.cmdline.txt
Replace the content with;
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
Press ctrl+x to exit and press y and enter to save.
Now reboot pi to see the changes
Step 5: Disable the Raspberry Pi Serial Getty Service
a. If in your output, Serial0 is linked with ttyAMA0, then to disable it use the below command,
sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service
b. If in your output Serial0 is linked with ttys0, then to disable it use the below command,
sudo systemctl stop serial-getty@ttys0.service
sudo systemctl disable serial-getty@ttys0.service
Step 6: Activate Ttys0
To enable the ttyso use following command,
sudo systemctl enable serial-getty@ttys0.service
Step 7: Install Minicom and Pynmea2
Use minicom python library to connect with the GPS module and make sense of the data.
sudo apt-get install minicom
Use pynmea2 python library to parse the received NMEA data.
sudo pip install pynmea2
Step 8: Test Output
To test the GPS run the command sudo cat /dev/ttyAMA0, You'll get the output as shown above.
Step 9: Write Python Code
Now, write the python code for the interfacing of the GPS module with Raspberry pi.
import serial
Import time
import string import pynmea2
while True: port=“/dev/ttyAMAO”
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)
Step 10: Final Output
The window shown above is the final output. It provides the data of your exact position in terms of Latitude and Longitude.
This project is based on an article GPS Module with Arduino and Raspberry Pi - By Priyanka Dixit. Visit this article to know more about GPS, how it works, explanation of key terms longitude & latitude, the difference between GPS chip & GPS module, and much more!
2 Comments
Tip 9 days ago
Hi,
Nice tutorial, helped me so much!
I have some trouble with python code, so I need to make some adjustments. For anyone that is having trouble with the code, attachments is code with modifications for working with Python3.
Question 2 years ago
Hi
The fixes for cmdline.txt, cause me to reinstall Rsp. Many suggest these fixes, but they don't work. Fine with a backup you can't get hold of. I use an Rsp 4 and will initially just have some data on the TX from my GPS. Not even a Loopback on Rsp doesn't work.
So what do I have to do ?
It's really frustrating that it has to be so complicated to make serial communication work.
Yours sincerely
Kent
kentoa@hotmail.dk