Hey everyone, it’s Frenoy and in this video,
we are going to learn how to interface a GPS
module to the Raspeberry Pi. We will then
use it to obtain information and
display this as a text overlay on
the video recording, so let’s begin.
This is part 2 of the dashcam series. Before
you proceed, please watch part 1 from last
week. The description will also contain links
to written posts on instructables, hackster
and our website itself, which will help you
build the projects.
A GPS module communicates with satellites
and it can give you information about your
location along with some other parameters.
The signals they receive contain location
and time information. GPS receivers use a
process called trilateration to determine
their location.
Here’s a very simple explanation of how
it works. When the GPS receiver receives the
signal from one satellite, it can use it to
determine the distance between it and the
satellite. This means that the GPS receiver
could be anywhere on this circle which is
at a fixed distance from the satellite based
on the information that the GPS module received.
Now, if we receive a signal from a second
satellite, then it can repeat the process
and by using both the signals, it can determine
its position to be at either one of these
two points where the two circles intersect.
When the GPS module receives a signal from
a 3rd satellite, it can repeat the process
and determine which of these points is the
correct location depending on where all three
circles intersect.
This is also knowns as a GPS fix. What we
have here is a 2-dimensional representation
of the process, and even though the GPS receiver
can obtain its location using 3 satellites,
this will not be very accurate. Here we have
some GPS data and even though it has received
signals from 3 satellites, it does not register
it as a fix and hence does not provide the
position information. A GPS receiver needs
signals from at least 4 satellites to provide
accurate location information.
Here are two commonly available GPS modules.
You have the antenna at the top and the receiver
at the bottom which are both identical in
this case. You also have a status LED which
helps indicate if the module has a GPS fix.
For these module, the LED will be a solid
blue when there is no fix and it will blink
when it has obtained a fix. The output is
sent over a serial port with a default baud
rate of 9600bps. These modules also have a
battery which is used to save some satellite
information in memory and this allows them
to obtain a fix much quicker. This module
was damaged while being used in a previous
project, so I will solder the battery from
this other module and use it for this project
as it is slimmer. Let’s now start interfacing
the module.
The first thing we need to do is SSH into
the board. We then need to enable the serial
port and that can be done by running the raspi-config
command, navigating to the interfacing options
and selecting Serial. Select No for the login
shell, Yes to the hardware serial port and
then select no when it asks you if you want
to reboot the board. We do not want to reboot
the board but we want to shut it down so that
we can connect the GPS module. Run the following
command to shutdown the board. Once done,
simply wire the GPS module using this reference
diagram. Please make sure that you use a GPS
module that operates on 3.3V as using a higher
voltage can damage the raspberry PI. Once
this is done, you simply need to ssh back
into the board and then run this command to
test the GPS. You should obtain the serial
output from the GPS module which looks something
like this. Press CTRL+Z to stop.
This data uses the NMEA format and the initial
characters determine the sentence or data
type. There is numerous documentation available
that explains this format. We are mainly interested
in the GGA sentence which gives us the time,
latitude, longitude, fix and so on. GPS uses
universal time or UTC. You can also obtain
something called HDOP which gives you the
relative position accuracy.
Before we can write the script for this, we
need to disable the getty service so that
we can reliably communicate with the GPS module.
The getty service is enabled by default and
whenever a connection is detected, it prompts
for the username and runs the login program
to try and authenticate the user. Since we
have only connected a GPS module to the serial
port, we do not need this service and we can
disable it. This can be done by stopping the
service first and then running this command
to disable it.
It is now time to work on the script. We will
be using python to access the serial port
and we first need to install that by running
the following command. In order to make sense
of the GPS data, we will be using the pynmea2
module and that can be installed by running
the following command. We can then create
a new script file with the name gps-test.
I would recommend copying the script contents
from the website post instead of typing it
all. Simply right-clicking in the terminal
window pastes the copied text for me.
The script simply imports the serial and pynmea2
modules. We then specify the serial port name
and create a function to parse the GGA sentence.
After the data is parsed, the function prints
the timestamp, position, altitude, fix and
satellite information to the terminal. In
the main script, we simply define the serial
port and use the default baudrate of 9600.
We then create an infinite loop where we read
the serial data, one line at a time and send
it to the parsing function which will print
the data to the terminal.
You can save the script by pressing CTRL+X,
Y and then Enter. The script can be executed
by running the following command. Valid data
will only be displayed once the GPS module
has a fix. Make sure the antenna is facing
the sky, ideally outdoors though you can also
sometimes get a fix by placing the module
near a window. Here’s what the output will
look like once the module has a fix. You can
press CTRL+Z to stop the script.
We now need to update the dashcam script to
use this information. I would recommend create
a new file with the name dashcam2 and then
copying the script file directly from the
website. We have imported 4 additional modules
as seen here. Serial and pynmea2 are used
to communicate with the GPS module. The time
module allows us to add some delays, while
the Color module allows us to change the text
background. We then have the serial port location,
parsing function and serial port definition
like before.
In the script that we wrote in part 1, we
used the wait_recording function as a delay
but by using it, we could not execute any
other functions and that is the reason why
we needed to update this section. We simply
use a while loop to keep track of time and
in that while loop, we read the GPS data,
parse it and use the following camera functions
to display it on screen. The script also displays
the CPU usage from the psutil library. We
then sleep for 100ms and repeat this until
the recording duration has elapsed. The recording
duration has now been updated to use seconds
which makes it much more flexible. This script
also checks the storage while the video is
being recorded. Everything else is pretty
much the same.
You can go ahead and save the file and then
try to run it by using the following command.
This should give you an error which indicates
that psutil is not installed. We actually
installed this in part one but we did not
install it for the root user and the reason
we get the error is because we are using the
sudo command to run the script. Sudo represents
the root user or admin if you like and we
need to use sudo in order to access the serial
port. So simply run sudo pip install psutil
to install it. You will then be able to run
the script using the following command and
it should work as normal.
You can manually stop the script using CTRL+Z
and use FileZilla to obtain the video files.
They can be opened using VLC and as you can
see, the GPS data has been added to it as
an overlay. The video seems to be playing
back a little faster than it should and this
could be due to a framerate mismatch but we
will look at this in a later video before
finalizing everything.
That’s it for this video, please do consider
subscribing to this channel if you like videos
like these. Thank you for watching and I will
see you in the next one!
