GPS Modules with Arduino and Raspberry Pi

Rek XIX
40 followers
4 Video Views·Oct 31, 2022
GPS Modules with Arduino and Raspberry Pi
Today in the workshop we're working with GPS modules.
We'll see how satellite positioning systems work and how we can use a
variety of modules with an Arduino and Raspberry Pi.
We'll also build a GPS logger to find out where we've traveled.
We're in the right position today so welcome to the workshop.
Well hello and welcome to the workshop and today we are going to be working
with a number of GPS modules. In actual fact what we're really
working with is satellite positioning systems because GPS
is not the only positioning system. It's just the most well-known one.
But we can take advantage of signals from a number of different satellites
in order to find out exactly where we are.
Now GPS itself really doesn't need any introduction. It started off as a
military only system, then it branched into avionics and
marine applications, but now it is used as a consumer
application for virtually everything. We have GPS devices in our vehicles,
we've got them in our phones and in our tablets, and
model aircraft and quadcopter enthusiasts have them in their devices
as well. And we can use them in our own projects.
So let's start off by finding out how satellite positioning systems work
and then we'll see how we can work with these amazing little modules.
These systems provide geospatial positioning using data from multiple
satellites. The satellites broadcast precision time
references. These time references are provided by
onboard cesium atomic clocks for incredible accuracy.
Most positioning satellites reside in medium earth orbit,
meaning that they orbit the earth twice per day.
There are six current positioning systems, four of them global
and two regional ones. The American Global Positioning System or GPS
is by far the most used and most well known of the positioning systems.
The first GPS launch occurred in 1978, but the system was restricted for
military use only then. Although the system was opened up to
maritime and aviation use by President Ronald Reagan,
commercial use didn't really start until 1994.
The GPS system employs up to 32 satellites, 31 currently are in use.
Satellites are often replaced in order to improve their technology.
GPS satellites orbit at an altitude of 20,180 kilometers.
The second most popular positioning system is the Russian Global Navigation
Satellite System. The first launch occurred in 1982
and it was available for global use in 1995.
GLONASS uses 24 satellites and they orbit at an altitude of 19,130 kilometers.
The Beidou Navigational Satellite System is a product of the People's
Republic of China. The first launch occurred in the year 2000.
Global operation was achieved in 2018 and the final deployment of Beidou 2,
which replaced the older Beidou 1, occurred in 2020.
This is a very advanced system that consists of 30 medium orbit satellites
plus five geostationary satellites. The medium orbit satellites orbit at an
altitude of 21,150 kilometers.
The most recent global positioning system is the European Space Agency's
Galileo Positioning System. The first launch of Galileo occurred in
2011 and global operation was achieved in 2014.
Galileo consists of 30 satellites and they orbit at an altitude of 23,222
kilometers. Galileo was designed to be
completely compatible with GPS so the two systems can enhance each other.
There are also two regional positioning systems.
NAVIC is the Navigation with Indian Constellation System.
It currently consists of seven satellites but will eventually consist
of 11. NAVIC was launched in 2018 and the
Indian government has indicated that they will eventually turn this into a
global positioning system. The Japanese Quasi-Zenith Satellite System
operates in a slightly different principle
in that it uses both ground stations and satellites.
Currently it consists of only one satellite but eventually will be
expanded to seven. The satellite was launched in 2010 but
did not become operational until 2018. Positioning systems work on the
principle of triangulation which you're probably familiar with.
If we have a person at point A and two known reference points at B
and C, we can determine the person's position knowing the distance between
B and C, A and B, and A and C. If our person were to move those distances
would change and their position could be easily
calculated. However we live in a three-dimensional world and simple
triangulation is not enough to determine our position.
We also need to be able to determine altitude.
For this we need signals from multiple satellites which allow us to determine
position, altitude, and also velocity.
We require a minimum of four satellites for accuracy.
Three of the satellites will be used to provide the x, y,
and z coordinates. An additional satellite is used to provide a clock
reference. It should be noted that some GPS
systems used for maritime and aviation use
also make use of ground-based stations as well.
Two types of systems that do this are assisted GPS
or differential GPS. A GPS receiver first consists of an antenna.
This can be a very sophisticated and large device
or it can be etched onto a printed circuit board.
The output of the antenna is passed to a filter to filter out extraneous signals
and just get the satellite signals. The multiple satellite signals are combined
into a decoder which will determine the actual position.
The decoder is then fed into an output device
to display the position. Most GPS systems use a comma delimited ASCII text
standard called NEMA 0183. The GPS data is generally updated at a
rate of once per second but some advanced systems can operate
at up to 20 times per second. Each comma delimited line is called a
sentence and you can break down the sentence in
order to get the GPS data that you're interested in.
You can get the time, the longitude, the latitude, the number of satellites
that are visible, and the altitude from the NEMA 0183
serial data stream. Because they use comma delimited serial
data, GPS units can easily be connected to a computer for data processing.
They can also of course be connected to a microcontroller
which is what we'll be doing today. So let's take a look at a few GPS units now.
So here are a couple of GPS units some of which we're going to be using today.
The first one over here is the BTANe BN-180 GPS.
This is a very inexpensive and very basic GPS but if all you're looking for
is simple positioning function then this one will certainly do.
The BN-220 that is next to it has pretty well the same performance specs
except this also has a built-in flash memory.
And the BN-880 over here has the flash memory, it has the GPS, it's a more
sensitive GPS, and it also has an electronic compass
or magnetometer inside it that you can use. So you'll
notice it has more output pins than these two over here.
Now this is the Adafruit Ultimate GPS. It's a very high quality GPS. It can get
a lot of different satellite channels. It has provisions for an external antenna
as well as its built-in antenna. And finally this SparkFun unit on the
end I just wanted to show you. We won't really be using it today
but I will be using it in a future video. This is a very advanced GPS unit
that can also do RTK which is real-time kinetics.
And that allows you to use a second source
to get some incredible accuracy. To give you an idea of the accuracy
all of these other GPS units, the standard ones,
will get you accurate between one and three meters.
By using this with what is called an NTRIP source, which gives you
a positioning reference source, you can actually get accuracy
down to one centimeter on these. And you can get accuracy down to one
tenth of a millimeter of positioning. So this is an incredibly accurate GPS.
Now let's give you an idea of some of the cost differences. Talking in US
This unit over here is about a $10 unit.
And this is about a $12 unit. The BN880 goes for about $20 US dollars.
The Adafruit Ultimate GPS goes for about $40 US dollars.
And this SparkFun unit over here goes for $275 US dollars.
And while that may seem a lot, GPSs with RTK capability
were rarely seen under $1,000. So this is actually quite a bargain.
But we won't be focusing on this device today,
but we will be using the other devices that you see here.
So now that we understand how satellite positioning systems work,
and that we've looked at a couple of GPS modules,
it's time of course to start using those modules.
Now I'm going to be using the BN220, but you could use pretty well
any GPS module that outputs serial data.
And so let's go ahead and get out an Arduino Uno,
and start working with these modules.
For our first GPS experiments, we'll need an Arduino.
I'm using an Arduino Uno, but any Arduino should suffice.
And the GPS unit with a serial output. Now I'm showing a BTAN BN-180,
but pretty well any GPS unit will work.
We'll start by connecting digital IO pin 3 to the GPS's TX or transmit pin.
IO pin 4 on the Arduino will be connected to the GPS's RX or receive input.
The 5 volt output from the Arduino will be connected to the VCC on the GPS module.
And the ground from the Arduino will be connected to the GPS ground.
And that's it for the wiring.
Now let's take a look at some code we can use to make use of it.
Now when we use our GPS units with an Arduino Uno,
we are going to be using a software serial port.
This is because the Uno only has one serial connection,
and we need that for the USB port.
The software serial library is built into your Arduino IDE,
so you don't need to install it.
And it allows you to use any two data pins as a transmit and receive,
and it emulates a serial port in software.
Now this first sketch that we're going to look at is actually a general purpose sketch
that you could use with other devices aside from GPS's,
anything that uses the software serial.
And it simply reflects the data on the software serial port
to the serial port so we can read it on the serial monitor.
Now the sketch is very simple.
We'll start off by defining the connections
that we're using for transmit and receive from the GPS.
So the transmit from the GPS is to receive on the Arduino,
and vice versa, and that's on pins 3 and 4.
And you could move those to other pins if you wanted to.
The next thing we have is the baud rate on the software serial port.
Now most GPS's have a range of baud rates,
but the two most common ones are 4800 and 9600.
We're going to be using 9600 baud.
We're also setting up the baud for our serial monitor,
and notice that we're using 115200 and not the slower 96.
This is so we don't drop off any data when we're reading it.
Then we go and include the software serial library,
and we set that software serial library up.
We create an object called GPSSerial to represent that software serial port,
and we pass it the two pins we're using for the receive and transmit.
In the setup, we'll set up our serial monitor,
and we'll also start the software serial.
And the loop is very simple.
As long as there is data available in the software serial,
we will just write that to the serial port.
So it's a very simple sketch.
Now I've got my GPS hooked up, so let's open the serial monitor
and take a look at the data that we're getting.
Now remember, I'm indoors right now, so this data is going to be garbage data,
but as you can see, we are receiving data from our GPS.
And if you look at the GPS unit, it is flashing,
and it flashes every time that it transmits data.
Now if you don't get anything back from this,
you'll have to check your connections,
and the most likely connection error will be reversing transmit and receive.
So just try swapping those two lines, and you should get data.
Now as I said, this is garbage data, but I'm going to take this outside now
and run it where I can pick up some satellites,
and we'll take a look at the data we get there.
And so I've taken the unit outside, and as you can see,
I'm getting a bunch of NEMA sentences out of it,
which is what I would expect, so it's locked onto some satellites.
Now to decode these sentences, you can use the chart that I've got over here.
It tells you what type of different sentences we have,
and you can certainly use an online utility to go and break down the sentences
and get the data you need out of it.
But there is a simpler way of doing that,
and the simpler way of doing that is just simply to use a library.
There are a number of libraries for the Arduino
that will allow you to take NEMA sentences
and decode them and extract the data that you actually want to get,
such as longitude, latitude, and time, etc.
So we're going to take a look now at a couple of libraries
that will let you go ahead and do exactly that.
Now in order to make some sense out of that GPS data,
there are a number of libraries that we can use
to parse the data and turn it into more usable results.
Now one of the more common ones is called the TinyGPS library,
and if you open up your library manager,
you can go and grab that directly from there.
So just go in and filter by TinyGPS,
and there it is right now, TinyGPS,
and we can just hit the installation button over here,
and it's installed.
Now TinyGPS is a nice library,
and I'll show you a few of the things that it has.
We can go down to the examples,
and scroll down to the examples from custom libraries
where we should be seeing TinyGPS.
And we've got a simple test, a static test,
which basically doesn't require a GPS.
It just has some preset data in it,
and a test with a GPS device.
And this basic library can be used for a lot of applications.
But I found another library that I think is nicer,
and it's called TinyGPS++.
And you can't get it on the library manager yet,
excuse me, it's right over here.
Now this is on GitHub.
You can go and grab this.
So grab the source code in a zip file,
and then go and download that.
And once you've done that,
you'll need to install that into your Arduino IDE.
So go under sketch, go into include library,
and then go into add zip library.
And go into where you've downloaded it,
in my case, my downloads folder.
And there it is, TinyGPS+.
And we'll just install that.
And now we can go and take a look at the examples
that we get in the TinyGPS++ library,
which are much more extensive.
As you can see, there are quite a few libraries,
sorry, examples in this library.
The basic example, device example,
full example, kitchen sink.
Let's go and take a look at the full example.
Now the full example is pretty cool.
It prints out pretty well every piece of data
from the GPS that you'd want to see.
Plus it also amusingly prints out your distance to London.
And so this shows you what you're going to start getting,
the degree and longitude, latitude,
the date, the time, the altitude, et cetera,
the course speed if you happen to be moving
and not stationary.
And also the distance from London.
And here's just a look at the code inside here.
As you can see, it pretty well,
the library takes care of everything.
So you can get the number of satellites
from satellite values.
You can get the location, age.
You can get the date and time right from the GPS object.
So the library pretty well does everything for you.
The rest of the code essentially is functions
that format the numbers.
So down over here, for example, a print of the float.
Down here, print integers, print date time.
It just formats the data so that it looks nice.
And up over here, the amusing print distance
and kilometers to London.
And it actually gives you the longitude and latitude
for London in order to do that.
So I'm going to run this,
but of course, if I run it right now on my GPS,
we're probably not going to get some wonderful results.
Just let me make certain that I've actually got
my port set up in my board.
I do indeed.
So let's go and upload this.
And we're done uploading.
And I can open up my serial monitor.
And as you can see, I'm getting absolutely no data.
Let's just make that a little wider
so we can see all the different things we have.
Because of course my GPS,
although it's blinking here merrily away,
is indoors and it's not picking anything up.
So let's move this outside and see what the results are
using the tiny GPS++ library.
And so we've taken it back outside.
And as you can see, the data is a lot easier to read.
It's separated out longitude, latitude,
the date and the time,
and even such statistics as the distance
to my place from London.
So by using a library,
we can interpret those NEMA sentences
and get much more meaningful data for our GPS projects.
The Betane BN880 module has not only a GPS sensor in it,
but also a magnetometer or a compass.
And this module is intended for use primarily
in things like model airplanes and quadcopters,
where you don't only want to know where you are,
but you also want to know what direction you are pointed in.
And of course, we can use that capabilities
in our experiments as well.
So let's go and take a look at how we get the compass data
out of the Betane BN880 module.
Now for this experiment,
we're going to require an Arduino Uno
and a Betane BN880 GPS unit.
Now, when I do the hookup diagram,
please do not look at the colors of the wiring I'm using.
Instead, look at the order of the connections
on the Betane BN880.
The B10880 will probably come
with a connection cable you can use,
but the colors on those cables vary dramatically.
So starting from left to right on the Betane unit,
we'll connect the SDA connection
to analog input A4 on the Arduino.
The next connection is the ground,
and that will be connected to the Arduino's ground.
The third connection is the TX from the BN880,
and that will be connected to Arduino Data Pin 4.
The RX from the BN880 will be connected
to Arduino Data Pin 3.
The next connection is the VCC,
and that will be connected to the Arduino's 5V output.
And the final connection on the right
is the SCL connection,
and that will be connected to the Arduino's A5 connection.
Note that I didn't use any pull-up resistors in this,
as I found that the internal 30k resistors
that the Arduino has for I2C were sufficient.
However, if you have difficulty getting the compass readings,
you might want to throw a couple of 4.7k pull-up resistors
on lines A4 and A5.
And this completes our wiring.
Let's go and take a look at some code we can use
with the BN880 GPS.
Now the BN880 GPS unit
also has a built-in magnetometer in it.
Now you can use the GPS section
exactly as you did with the previous GPS modules.
So either the TinyGPS or TinyGPS++
or another GPS library
can be used to get the results for it.
For the magnetometer,
you're going to need to install another library.
Now the magnetometer is an HMC5883.
I've already installed the library,
but let me show you where it is.
You just go into Manage Libraries,
and then type in HMC5883.
And you will get this Adafruit library,
the HMC5883 Unified Library,
which I've already installed.
Now when you go to install this library,
you may be prompted to install
some dependent libraries as well,
because this is part of the Adafruit
Unified Sensor series of libraries,
and they rely on a master library
to be existing as well as this library.
So go ahead and tell it to install
all of the libraries.
And once you do,
you can go into the examples,
and there only is one example
called MagSensor.
And this is an example
that works with a magnetometer.
Now you can run this pretty well as it is.
The actual code again is made simple
because the library does pretty well everything.
So it gives you the X, Y, and Z coordinates
of the magnetometer.
But one thing you'll want to do
if you want accurate readings
is you'll need to calibrate it
for your location.
The magnetic north pole of course
is not the same as the true north pole,
and so there'll be a magnetic deviation
or declination in your area,
and that will be unique to where you live.
Now they have an example
of 0.22 radians over here,
and you can calculate yours
by going down to a site
in which you first of all measure
the actual magnetic declination.
And I've got mine over here.
It's negative 14.2,
which is very close
to where they used the example.
And that makes a lot of sense
because Adafruit is based in New York City,
and their magnetic declination
is very similar to ours here in Montreal.
Now you'll need to convert that unit to...
That's in degrees and minutes,
and you're going to need to convert it
to a decimal form.
So we need to just change the minutes
into a decimal,
which I've done over here.
So I've got negative 14.03392, etc, etc.
And it comes out to negative 024 radians.
And so as you can see,
that's very close to the example
they have here of 2.2.
I'm just going to leave this alone right now
because I'm not looking
for particularly precision results.
But what I'm going to do is load this
and observe the results
in the serial monitor
as I move the unit around.
OK, I've got it running right now.
As you can see, I'm getting readings
through the X, Y, and Z axis
and the heading in degrees.
And if I move this around,
you can see that I can change
all of those figures,
move it on to different angles,
and you can watch the different axes
change value as well as the degree heading
when I've got it facing up like this.
And so it does indeed seem
to give different readings
as I'm moving around.
And demonstrates the use of the magnetometer
or compass function
of the BN880 GPS module.
Now, the module that we're going
to take a look at next
is the Adafruit Ultimate GPS module.
And this module is actually available
in two different configurations.
It's available in a configuration
with a USB output,
which is the module that I have,
or it is also available
with a discrete output
that you could use in a similar fashion
to the modules we already looked at.
Because it has a USB output,
it is more suited for things
like computers or microcomputers.
And so instead of using an Arduino
for these experiments,
we're going to be using
a Raspberry Pi for them.
So let's go and take a look
at the Adafruit Ultimate GPS module
and how we can use it
with a Raspberry Pi.
Now, this is the Adafruit Ultimate GPS,
the USB version.
And as you can see,
it's got a USB connector
right on the end over here,
a micro USB.
It's also got a place
for four pins over here,
but those are just a repeat
of the four connections on the USB.
So this is not as easy
to directly connect to an Arduino,
but for a microcomputer
or a computer, this is ideal.
Another neat thing about this
is that you can use it two ways.
You can use it just standalone
or you can connect
an external antenna to it,
which is what I'm doing now.
It's got a connection down over here
called the UFL connection,
and I've got a little adapter cable
on it to adapt it to an SMA connection.
And this is going to allow me
to use this inside the workshop
with an antenna outdoors.
Now, because my workshop
is a great distance
from the window in the basement,
in fact, it's the opposite corner,
I need a number of things to do this.
So first of all,
this is my antenna over here.
It's a very basic antenna.
You'll find these things
relatively inexpensive.
I got this one at SparkFun
and it's actually an amplified device
and it comes with about
three meters of SMA cable
and you can see the SMA adapter
I have over here.
Now, this antenna needs to be placed down
onto a metallic surface
and it's actually magnetic.
So I've got this plate over here
that also came from SparkFun
and the plate has on the bottom of it
a thread that can go onto a tripod thread.
So that's what I'll be using
in the backyard.
Now, in order to get it to the workbench,
I've created a couple of things here.
Now, this is another SMA extension
and I've placed this
onto a piece of Coroplast,
which I've cut to fit
into the basement window
so that I can run the antenna outside
and not let all the insects inside.
But even that and this
are not long enough
to get to where I want to go.
So what I've done is
I've got another extension cable over here
and this is a three meter extension.
This is a two meter one here.
This is about two meters of cable,
but that's going to run in my backyard
and that will bring the Adafruit GPS
pretty well down my hallway.
So in order to complete the rest
of the distance to the workbench,
I've also got this cable over here,
a micro USB cable
that's about four meters long.
So we're really going on
a long distance extension run
in order to work on this in the workbench,
but it actually works.
I was rather surprised.
I was concerned that the attenuation
in these cables
might knock the signal down too much,
but it doesn't really
and it seems to work out just fine.
So now we'll see how we can use this
with a Raspberry Pi
here in the comfort of the workshop.
And so I have my ultimate GPS
hooked up to my Raspberry Pi
with an extremely long extension cord
on both the USB
and on the external antenna
so that I can actually
keep my antenna in the backyard
and work on this in the workshop.
So this is real remote control work over here.
Now I've got a cheat sheet over here
and you can get a copy of this cheat sheet
on the article that accompanies this video
on the dronebotworkshop.com website.
It'll make it a lot easier
to enter all these commands in.
And the first one I've got over here,
this lists the devices
that it sees on the USB port.
And what you're looking for over here
is this one over here,
this Signal Integrated Products Incorporated.
This is actually the GPS unit
and so you'll want to do that
just to make certain
that you've got a connection to it.
Now the next line we're going to do
is going to actually install GPSD
which is the client
that we're going to use
to display GPS data.
So I'll copy that over here
and I'll do a paste into my terminal
and I'll just hit enter
and we'll do the installation
and I'll say yes I want to do this.
Now after we've installed it
we have to disable
and get rid of one of the services it installs
because it's actually going to interfere
with the operation on the Raspberry Pi.
So we do it with these two commands.
First of all this one will stop the service
and the next command over here
will actually disable the service
in its entirety
but you have to stop it first
before you can do that.
Okay now we're going to actually
manually start the GPSD service
with this long command line over here
and finally we can use this final one
to display and see
if we're getting any results.
And bear in mind
it may take a little while
for the results to come in.
And there we go
we're actually getting some results right now
from a number of satellites
which is pretty impressive
when you consider again
that this antenna is in the backyard
and the antenna itself
has about a three meter cable on it
and I've got a total of eight meters
of additional SMA extension cable
running to the GPS
and then I've got a 12 foot USB connection
going to that
and nonetheless we're getting good data
here in our Raspberry Pi
so this is one way
that you can use the Adafruit Ultimate GPS.
Now the experiments we've done so far
has made use of GPS data
from a stationary position
and that position being my backyard
but a lot of times
we like to use this kind of data
in order to track where we have gone
so for our last experiment
we're going to build a simple GPS tracker
we're just going to take the GPS data
and format it
and put it into a CSV
or comma separated value file
now you can open up a CSV file
in pretty well any spreadsheet application
in Microsoft Excel
in Google Sheets
or LibreOffice Calc
but you can also use a CSV file
and open it up in Google Maps
and by doing that
you'll be able to track
exactly where you have been
with your GPS unit
so let's go and take a look
at the hookup for that
and then we'll take a look
at the code we need
in order to make our own GPS tracker
now here's what you're going to need
in order to hook up
our position logging circuit
first of all you'll need an Arduino Uno
and you'll need a micro SD card module
the module I'm using
is a very common module
and it has a built-in 5 volt regulator
and level converter
so it runs on 5 volts
and is 5 volt logic compatible
of course you'll also need a GPS module
although I'm showing a BN180 GPS
I'm actually using a BN220 in my circuit
and any GPS module
that outputs serial data will work
you'll need an LED
any color will do
and the dropping resistor for that LED
I'm using 220 ohms
but any value from 150 to 470 ohms will suffice
we'll begin by connecting the SD module
we'll connect the ground from the Arduino
to the SD module's ground connection
we'll connect the Arduino's 5 volt output
to the VCC input on the SD module
pin 12 of the Arduino
is connected to the MISO
which is master in slave out
on the SD module
pin 11 on the Arduino
is connected to MOSI
master out slave in
pin 13 is connected to the clock signal
on the SD module
on my module this is labeled SCK
and pin 5 from the Arduino
is connected to the chip selector CS pin
on the micro SD module
now we'll hook up the GPS
in the same fashion that we did
in our previous experiment
we'll connect the Arduino's ground
to the GPS ground
pin 4 of the Arduino
will be connected to the GPS receive line
Arduino pin 3 will be connected
to the GPS transmit line
and the Arduino's 5 volt outputs
will be connected to the VCC
on the GPS module
finally we'll connect pin 7 of the Arduino
to one side of the dropping resistor
the other side of the resistor
will be connected to the anode of the LED
and we'll connect the cathode of the LED
to the ground connection
and this completes our wiring
now let's go and take a look at some code
we can use to build our GPS position logger
okay this is the sketch that we're going
to be using for the position logger
and it's based around the tiny GPS library
that you saw earlier
so if you haven't installed that library
remember that's the one we need
to install from a zip file
it's not up in your library manager
at least not yet
after that we just use the software serial library
so we can talk to the GPS
the SPI library
so we can talk to the SD card
and the SD card library itself
so we can exchange data with it
and these last three libraries
are all part of your Arduino IDE
we define the GPS connections
and we also define the baud rate to the GPS
so if you're using a module
that has a 4800 baud rate instead of 9600
or a different one you can change that
now this is the chip select pin
for the SD or micro SD card
and here's the pin that we're using
to drive our LED
now we got a string we define over here
called GPS text
and this is going to hold one line of GPS data
which in our case is just going to be
longitude, latitude, and a timestamp
then we're going to have a couple of variables
for a counter
because I found that
if you took every single GPS reading
you got too many readings
and you have to remember
that if you're going to try to plot this
onto Google Maps
you have a limit of 2000 plots
that they'll accept
and that's the maximum
so you'll want to keep it down under that
I found for walking a value over here
for GPS count was pretty good at 30
now if you're cycling
or if you're going to use this on a motorcycle
or something
you might want to reduce this value
if you want to get more waypoints
if you want to get them spaced closer together
you can reduce that value
or you may even want to increase it
so experiment a bit with that
anyway we create an object for our GPS
call it GPS
and we set up the software serial
at the pins that we defined
then we go into the setup
we'll define our LED as an output
and we'll turn on our serial monitor
and the serial monitor is just there
for debugging purposes
when you're getting this all to work
so it could be handy then
but of course when you're out in the field
you're not going to be able to look at it
unless you want to bring a computer with you
after that we're going to get the software serial port
going at the baud rate that we defined
and then we initialize our SD or micro SD card
and so if it fails
we're not going to go anywhere past this point
but if the card initializes
we'll print out to our serial monitor
and then because we're going to be out in the field
and probably won't have a serial monitor
I blink the LED on and off three times
is to let me know I'm gone through the setup
and everything is good to go
now before I show you the loop
I want to show you the function
that I call in the loop
the one that's written over here display info
so let's go down to display info
which is down over here
and this is the function
that's going to get a string of data
to write out to our files
so as long as we have GPS data that's valid
it'll look for it
so if the location data is valid
then it is going to go get a string
with the latitude and longitude
and it's going to build it up into this string
that we define over here called GPS data
and then this function will return the string
assuming it's completely valid
now on this string line
you'll notice that I have a six at the end over here
and the reason for that is
I'm converting the longitude and latitude
which are longs into a string
and if you don't put a value back over here
which is an optional value you can put in
it's only going to return back two decimal points
and I want all six decimal points
so you need to do that
and I'm putting a comma between each
because I'm making a comma delimited string
if the value isn't any good over here
I'm going to end by returning a zero
and that's true for all of the time and date values as well
we return a zero if it isn't valid
so we don't end up getting any invalid data
right to the SD card
assuming the date is valid
I go and get the year the month and the day
and you'll notice that if the month and day
are a value that is under 10
I pad it with a preceding zero
so that everything formats nicely
and then I put a space between the date and the time
and then after that I go and get my time
I look to see if the time is valid
I do the same thing over here
if the value is under 10
I pad it with zero
I send back the hour minute and second
and I put a colon in between them to format at like time
and assuming we got to the end
without having to return a zero
because of invalid data
we just return the completed string
and that's a whole string of our GPS data
so now let's go back up into the loop
and see how we use that
we'll start off the loop by writing to our LED
and sending it low
so we turn it off
and then we see if there's data actually available
and if there is good data available
we go and look and see if we got a zero back
if we didn't get a zero back
then we go into here
and we get the GPS string
and assign it to the GPS text
so we call our function
and it returns a string of data
that we assigned to this variable
we defined earlier as a string
then we go and check our counter
now I am going to get every 30th count
the way that I set up my GPS count earlier in the program
and so we look to see if we've arrived at that total
and if we have
then we go into here
we turn the LED on high
and say we're about to write to the SD card
and we open up a file called gpslog.csv
and we open it for writing
now if the file doesn't exist
it will create the file
if it does exist
it will append to the file
and that's something you should make note of
because if you turn this off and on
it won't overwrite the old file
it'll append to it and keep on going
and you could redesign that if you wanted to
a neat way to do it
might be to make another function
just to get the GPS date stamp
and put it on the file name
so that you can put a time date stamp
and make unique file names
but I'll leave that up to you
right now we're just creating a file called gpslog.csv
that we're going to continually write to
so if the file is actually open
then we'll do a data file println
which is like a serial monitor println
but in this case it actually prints out to the file
and we'll print the content of GPS text
which is the line of data we want
and then we will close that file
we'll also print it out to the serial monitor for debugging
and also tell the serial monitor
if we had an error over here
assuming everything went okay
we're going to increment the GPS count
but make sure that we haven't exceeded the total count
and if we have we'll set everything back to zero
and then go back to the top
and run the loop again
and so that will get all of the GPS data
spaced out onto every 30th reading in my case
for our entire trip
so let's go and take our GPS logger out for a walk
and see what it looks like
so here's my little GPS logger
and I just put it onto a sodless breadboard as you can see
now you might notice this yellow wire going over here
it actually has nothing to do with the circuit
it's literally holding the Arduino down to the breadboard
another way of doing that
might have been to use double-sided tape
but I figured if I use double-sided tape
then I'd probably ruin the breadboard
otherwise you can see the SD card module back over here
and the GPS unit itself
and you probably can't see it
but I've got a couple of little pieces of wire over here
just holding down the cable for the GPS unit
you'll also notice I've mounted it
so the antenna side is up
and you'll see my LED over here
and over here I've got a couple of different ways I can power it
this is the way I'll probably use out in the field
and it's just a 9-volt battery
that can plug into the Arduino's power plug
but I've also got this little 5-volt adapter
this USB power bank
that can plug into the USB connector on the Arduino
so I could power this up either way
when I'm taking it out into the field
which is what I'll do right now
I'll go outside and walk around
and we'll grab some readings
all right I've taken my GPS logger out for a stroll
and I have collected some data in this CSV file
and I've just moved it onto my computer
into a folder in my documents folder
now before we work with it on Google Maps
let's just take a look at the inside of this file
so we can go open
and it'll open in a spreadsheet
which in my case is LibreOffice
I'm going to open it for display
and it wants to know how it should format it
but it already sees the correct format
it's using a comma as a delimiter
and it sees three different columns
which is exactly what we've recorded
so we can say okay
and take a peek at this file
and over here we have the latitude and longitude
and the date stamp that we made for every entry
and as you can see I started off at 1521
now remember that's GMT
so because of daylight savings
I'm actually about six hours earlier than that
and I went and I saved 398 lines of data till 1547
so it looks like I spent about 26 minutes or so
on my little stroll
so I've got all the different waypoints
now you may see occasionally
where it doesn't look like it lines up
but that's only because the last character
would have been a zero
so you'll see that occasionally
and LibreOffice just isn't going to print the zero
after a decimal point
it doesn't see the point in it
so I'm going to take that off
and now I'm going to go into my Google Maps
okay so I'm in Google Maps
and I'm going to go to the menu over here
I'm going to your places
and in your places I'm going over to maps
and I'm going to go to see all your maps
and there's a button that you'll see right up here
that says create a new map
so that's what we want to do
and we can give our map a title if we want to
and this is my test map
and you can add a description or something
I won't bother to do that right now
and I'm going to import a layer
and so I hit import
I have to select the file
and I'm going to my CSV file
which I've got in my documents folder here
I'm going to open the CSV file
it's going to upload it
now it wants to know what these columns mean
so the first one over here is my latitude
and the second one over here is my longitude
and then we hit continue
and then it needs a label
and we're going to use a time stamp as the label
and hit finish
and let it load
and here we go
and here's my walk
and I went to a park called Centennial Park
that has a fake lake
or it's actually really a fake big pond
it's not really that large
and I can zoom in over here
you can see where I started my walk over here
and so for every one of these points over here
I'll get the longitude, latitude, and a time stamp
and it followed the path pretty well
there's a couple places where it's off
now that could be that the map is off
it could also be that this area over here was quite treed
and there are a lot of treed areas over here
but otherwise it definitely stayed within the three meters
that the accuracy is supposed to be
and as I said some of the areas were quite heavily wooded
I also went off the path over here
up a bit of a hill to a lookout
and you can see that I got a lot of mapping points over here
sometimes you can see one going up and down
like here's one that was at 1530
and the one next to it was at 1533
so one of them was up and one of them was going down the hill
so it tracked me pretty well around this park over here
I was fairly pleased that I got this kind of resolution
and it tracked me when I was coming back past the chalet
I actually did cut across the grass over here
but after all it is a park you're allowed to do that
and I got back to my car over here
and that's a pretty accurate representation of where I parked as well
so I would say that our GPS logger is working pretty well
and this is a way we can view our data on Google Maps
so that wraps up our look at satellite positioning systems for today
but it's certainly not the last time
that you're going to be seeing GPS units here in the Dronebot workshop
in fact I've got two projects coming up
that make use of GPS units
including that very expensive and very sophisticated Sparkfun unit
that I showed you at the beginning of the video
now of course the best way to find out about those videos
is to be subscribed to the channel
so if you aren't a subscriber yet
please do me the honor of subscribing
all you'll need to do of course is click on the subscribe button
and also click on that little bell notification
and as long as you've got notifications enabled on your YouTube
you will get notified every time that I make a video
now if you want to grab some of the code that I used today
or learn a little bit more about satellite positioning systems
just head over to the complimentary article
on the dronebotworkshop.com website
you'll find the link to that article right below this video
when you're on the website of course
you could subscribe to the newsletter if you haven't
it's just my occasional way of letting you know what's going on
I've been a little bit slack about sending them out
but I'm trying to do better
and finally if you want to discuss today's video
the absolute best place on the planet to do that
is on the DroneBot Workshop forums
we've got a bunch of great people there
who can help you out with your projects
and who love to talk about this stuff
and there's a link below the video
so that you can sign up for the forum as well
so until we meet next time
please take care of yourself
and please stay safe
and we will see you again very soon
here in the DroneBot Workshop
goodbye for now
