In this video, we will learn how to add a GPS receiver module to a Raspberry Pi Pico. We will be taking a look at how GPS works, and how to wire up and code your project to get coordinates, as a final step, we will be making a short and sweet GPS project - a small device that tracks a target location and uses our current location to display the distance and direction to that target, allowing you to navigate there from anywhere on the surface of the earth.

Transcript

In this video, we'll be looking at how to use a GPS module with the Raspberry Pi Pico and MicroPython. We'll learn how GPS works, how to wire up and code the Pico to get coordinates, and as a final step, we'll use it to build a device that uses your current position to point you in the direction of any chosen location in the world and also tell you how far away it is. Let's get into it.

GPS is one of the most incredible things that we, as a species, have made. Figuring out where you are used to be a skill that our ancestors spent quite a few years mastering, but now you can do it far more precisely with a module that costs a little more than a cup of coffee. But how does it work? How does such an inexpensive and small module have the ability to locate you anywhere on the face of the Earth?

Above you, there is a system of strategically placed GPS satellites orbiting around the Earth, about 30 of them. These satellites beam out radio messages containing the exact time the signal was sent and where the satellite is. This device listens for those messages and does some calculations to figure out how far away each satellite is, which it calculates using the speed of light. If it can get a signal from four or more satellites at once, it can figure out its latitude, longitude, altitude, and even get a very accurate time reading from the satellite's atomic clock. This module will let your project read the time from an atomic clock in space.

A very simplified example of how this locating with distances works: imagine that I was lost somewhere in Australia, but I somehow knew that I was exactly 2,000 kilometers away from Sydney, and I also knew I was exactly 1,450 kilometers away from Cairns. If I used a little bit of math here, I could precisely find my location and know that I'm in Alice Springs. It gets a little bit more complicated in 3D space and on the face of the Earth, but it's the same idea. You know how far away the satellites are and where they are, and you can find where you are.

Alright, that's how it works. Let's plug it in and start receiving those satellite messages. To follow along, you'll need this GPS module, a Raspberry Pi Pico (we're using the Pico 2 here, but any other Pico should work), a micro USB cable to program it, and also a breadboard and jumper wires to connect it all. You can find links to all of these things in the written guide linked below.

Let's start by placing our Pico into the breadboard, as well as the GPS module, which, as always, we've gone ahead and soldered some headers onto. First things first, let's supply the GPS with power. We'll plug the ground pin of the GPS into the ground of the Pico, and then we'll supply it with 3.3 volts from the Pico's 3.3-volt out pin, and we'll just connect that to VCC of the GPS. This GPS module also has the option to be powered with 5 volts if you want to use another 5-volt power source for it.

Now, this module is going to communicate with our Pico through UART, so we're going to go ahead and connect the TX pin (the transmitter pin) of the module to the RX pin (the receiver pin) of our Pico, which is just going to be pin 1 here. Then we'll make the reverse connection by connecting the RX to the TX, which will be pin 0 on our Pico. And that is all we need. Let's jump into Thonny and code this thing up.

If this is your first time dealing with Thonny and the Pico, or you just need a refresher, you'll find a link below to a quick getting started video, which is also part of an entire comprehensive Pico course if you want to check it out. Alrighty, let's plug in our Pico, select our COM port down below, like so, and we're just going to paste in the demo code, which you'll find on the written guide. This is the most stripped-back and bare-bones bit of code you need to interact with your GPS module.

Starting off, we import UART and pin, very important, and then we go ahead and set up our UART peripheral here. We've gone ahead and connected it to pin 0 and pin 1, which are our TX and RX pins respectively, and that's just going to be assigned to UART peripheral 0. There are two peripherals there; we're just using 0, which is what 0 and 1 are assigned to. Very importantly, we're going to set our baud rate to 9600, which is what the GPS module is operating on as well. This is just the agreed data rate that these two will communicate at, and it needs to be the same.

Then we're going to go into our infinite while true loop, and we're going to start by checking if UART.any. Essentially, when the GPS sends a message to our Pico, it's going to be held in a buffer, and this if UART.any basically checks if there is a message there waiting for us. If there is, we're going to .read that message and also .decode it, and store it in GPS reading, and then we're just going to print out GPS reading like so.

Alright, let's give that a run, and ah, we are receiving GPS readouts like so. Now, chances are yours might look like mine here with a lot of commas everywhere. This is actually what it looks like when the GPS hasn't got a lock on enough satellites above. We're inside a big metal warehouse, so it's a little difficult to receive those signals, so we aren't getting our position yet. You can confirm this by seeing a V here, which means you're not locked on yet. When your GPS can see enough satellites and it can start finding your location, this will change to an A, just a capital A, and the red light on the module will also start flashing. It may take your module a few minutes to find the satellites above.

If your GPS module hasn't been powered on for a while, and if you're like us and inside a giant metal building, you might need to move closer to a window or head outside to get a lock onto those satellites. We actually found that if we took it outside and powered on the Pico for a few minutes to get a lock, we were able to work on it inside and it kept that lock for a while.

This also brings up a conversation about accuracy and operating conditions. Essentially, the more stuff in between you and the satellites in space, the less accurate this is going to be. When we were outside with a completely clear view of the sky, the GPS module was accurate to about a meter, give or take that much. However, when it was really cloudy outside and we were in between a few buildings and under some trees, it was accurate to about two or three meters. And of course, when we bring it into the giant metal warehouse, that blows it out to a dozen, sometimes maybe even a hundred meters off. So the more things between you and the satellites that might block those signals, the less accurate your GPS will be.

I just took my GPS outside for a walk and now we've got a lock. As you can see, we've got the letter A coming through, the LED is flashing, and we have a lot more information coming out of our GPS. Now hidden in here is a lot of helpful information. This is the current time being read off the atomic clock. This is our latitude, our longitude. We've got our altitude. We've got the number of satellites we're connecting to. There's a whole bunch of information here. If you want a more in-depth breakdown of it all, you'll find it in the written guide link below.

Our information is there, but right now it's quite hard to use in our projects in this format. That's because it's actually in a standard format called NMEA. The hardest part of actually using a GPS in your project is taking it from this format and handling it and manipulating the strings to extract the information that we want. One option here would be to paste this output message into an LLM like ChatGPT, Claude, or DeepSeek and get them to help you extract the information you want. They're pretty familiar with this NMEA format, but we also have gone ahead and written a library to do that as well for you. You'll be able to download the library from, you guessed it, the written guide link below.

Once you've downloaded it, go ahead and open up the file explorer in Thonny, navigate to it in our window, and then upload it to our Pico. Let's close that. You can also find this demo code that I just pasted in the written course as well. It's quite similar to what we had before, just with a few new modifications to use the library. We now have to import GPS parser, which is just the library that we downloaded and uploaded. We're also going to go ahead and set up UART exactly like we did before. But now we have this line here to create an object called GPS, and we're going to feed in UART to tell it which UART peripheral our GPS is connected to. Then all we need to do is call this line GPS.getData, and it's going to store all of that information in this variable here.

The output of this library is actually going to contain a lot of information. If you go ahead and open up that parser library, at the top here, you will find all of that information. We can check if it has a fix. This is literally checking if that letter is a V or an A. If it's a V, it'll be false. If it's an A, it'll be true, and we can get a GPS location. We can also get our latitude, our longitude, our speed in knots (we've left that because that's what it reads out as default). We can also get our time and date. Be aware this isn't for your time zone. This is UTC or central time. We can see how many satellites we're connected to, as well as an estimate of our altitude. We also have these three things called HDOP, PDOP, and VDOP. This number is an overall rating of how accurate your horizontal, vertical, and position is. Essentially, the lower this number is, the better this reading is. You don't need to know much about it. It's just here in case you want to use it.

All of this information is going to be stored in this GPS data variable. As you can see here, to print it out or to use it, we just have to use GPSData.hasFix, GPSData.latitude, and this dot something is just whatever is at the top here. You can copy and paste time, whack it in there, and call GPSData.time to get your time.

Let's go ahead and run this code. It takes a little bit to boot up. We're false; we don't have a fix yet. And now we are true, and we're getting our latitude and longitude out. Something worth mentioning is that there are a few different ways of representing GPS coordinates. By default, the GPS module will output a method using north, south, east, west notation, but the library converts it to this positive-negative number system. If you want to read up about it more, it's in the written guide. But long story short, it's easy to do math and other things when it's this positive-negative method. With that, you are now able to get GPS coordinates as well as a whole bunch of helpful information and add it to your project to use it however you need.

Before we finish, though, let's do something cool with it. We are going to add this magnetometer, essentially a compass module, to figure out which direction we're pointing in, as well as this OLED screen to display all of that information, and this AA battery pack so we can power it anywhere we want. With this, we're going to create something that takes our current coordinates and lets us navigate to any other coordinate on Earth, showing us both which direction we need to head and how far away it is. We're going to go through this quite quickly, so if you want to follow along or need a hand with anything here, we'll cover it in greater detail in the written guide. Sorry to sound like a broken record here.

To use the screen and the magnetometer, you'll need to upload their libraries to the Pico exactly like before, and you'll find them in the written guide. If you've just pulled the magnetometer out of the box for the first time and you haven't used it, you'll need to calibrate it. There are also instructions there on how to do that. Of course, you'll find the project code which we are going to paste in.

We import all the stuff we need, and then we start by declaring the latitude and the longitude of the point that we want to navigate to. The easiest way to get this is probably to go to Google Maps, click on a place like so, and you can see your coordinates down here. Then we go ahead and set up our UART and our library exactly like we did before, and then we set up the display and the compass.

We have a few functions, and this first one here just calculates the distance between latitude longitude 1 and latitude longitude 2. That's one of the cool things about coordinates. If you know the radius of the Earth, you can figure out the exact distance between those two with not too much work. Then we have another function, calculate bearing, that does the exact same thing, but it figures out the direction. Is it north, east, south, west, or which direction is it from point A to point B? Then we have a function, and all this does is it helps us draw an arrow on our display, not too vital to anything.

Then we come into our main while true loop. We start by framing up the next thing we're going to show on the display. We get our compass reading from the magnetometer, and then we get our GPS reading from our GPS module using the library. Then we say, if we have a fix, as in we've locked on to satellites and we're getting coordinates, we're going to calculate distance with the calculate distance function. Same thing with the bearing function. Then we're going to use a little bit of math to compare what the magnetometer is seeing and the direction we need to have to figure out which direction we need to point towards. Then we use that direction and draw an arrow on the screen.

The rest of this is basically just showing stuff on the screen. We're drawing lines to get a bit of a cool UI. We're displaying things like the distance. The distance is in meters, and if it's more than a thousand, we'll write kilometers. If it's less than a thousand, we'll go to meters. We'll display the altitude, the longitude, the latitude, and the destination latitude and longitude.

If we go ahead and save this code to the Raspberry Pi Pico, and we call it main.py, whenever the Pico powers on, it will run this code automatically without the computer. So we should be able to plug in our batteries and switch it on. Hey, look at that! We can now navigate to anywhere we want on the face of the Earth. The arrow points us in the direction we need to go. We can see how far away it is below it. In the bottom right is our current altitude. The number in the top right is our current coordinate, and the one below it is the target coordinate that we punched into the code.

With this, we can navigate to anywhere we want on the face of the Earth, which is incredible if you think about it. With some inexpensive off-the-shelf components and a few hundred lines of code, we can now navigate with more precision than any of our ancestors have ever done so throughout all of history. All of that is thanks to the humble GPS receiver.

Well, that about wraps that up. If you made anything cool with this, or you need a hand with anything we covered in this video, feel free to head on over to our community forums. We're all makers over there, and we're happy to help. Until next time though, happy making.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.