Getting GPS to work on a Raspberry Pi 3 Model B
“My GPIO-connected UART device is broken on Pi 3, why?
The mini-uart is now routed to GPIO14/15 as the PL011 UART is now used for bluetooth communications. The mini-uart doesn’t have a separate clock divisor and uses the core clock frequency. Changes in core clock (e.g. through throttling or idle/load frequency changes) will result in arbitrary modification of the effective baud rate. There’s no easy way around this, but as a workaround there is a pi3-disable-bt devicetree overlay in latest rpi-update firmware which reverts this change.”
We will show you the operations in the next steps.
Enable the UART
First we need to download and install a new device tree overlay.
The Raspberry Pi Engineer PhillE (forum username) has kindly made a custom overlay called pi3-miniuart-bt-overlay.dtb to remap the UART ports and this needs to be copied into the /boot/overlays folder on the SD card and we also copied it into the root / pi3-miniuart-bt-overlay.dtb of the card as well.
We have made a copy of the new boot overlay available to download from here and it will also be included in a future Raspbian Linux release. We have archived the overlay in zip format and it will need to be uncompressed and copied to your SD card.
Next we need to edit the /boot/config.txt file, open the file either on your desktop computer or using the Raspberry Pi via SSH
sudo nano /boot/config.txt
You need to add the following lines:
dtparam=spi=on dtoverlay=pi3-disable-bt-overlay core_freq=250 enable_uart=1 force_turbo=1
Then use Ctrl+O to save changes and use Ctrl+X to exit.
Now edit /boot/cmdline.txt.
sudo nano /boot/cmdline.txt
Change the file to the following:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Exit and save your changes
If you have edited the cmdline.txt and config.txt files on your desktop put the SD card back in your Raspberry Pi 3 and boot to either a network SSH session or desktop and run the following in a shell window.
If you want to disable the built in bluetooth you need to stop hciattach trying to use the modem via uart0 which will disable the relevant systemd service
sudo systemctl disable hciuart
Now edit /lib/systemd/system/hciuart.server and replace ttyAMA0 with ttyS0 .
sudo nano /lib/systemd/system/hciuart.service
Replace “After=dev-serial1.device” with “After=dev-ttyS0.device”
Exit and save your changes
You need to update the operating system with the latest patches with:
sudo apt-get update sudo apt-get upgrade sudo reboot
Once your Raspberry Pi 3 has rebooted you should now have access to the serial console via the GPIO header at 9600 baud.
If you just want to do a quick check to see what data is coming out of the GPS, you can enter the following command, following by CTRL+C to quit:
sudo cat /dev/ttyS0
Run the following two commands to stop and disable the tty service:
sudo systemctl stop serial-getty@ttyS0.service sudo systemctl disable serial-getty@ttyS0.service
sudo shutdown -r now
You can always just read that raw data, but its much nicer if you can have some Linux software prettify it. We’ll try out gpsd which is a GPS-handling Daemon (background-helper)
To install gpsd, make sure your Pi has an Internet connection and run the following commands from the console:
sudo apt-get install gpsd gpsd-clients python-gps
Raspbian Jessie systemd service fix
Note if you’re using the Raspbian Jessie or later release you’ll need to disable a systemd service that gpsd installs. This service has systemd listen on a local socket and run gpsd when clients connect to it, however it will also interfere with other gpsd instances that are manually run (like in this guide). You will need to disable the gpsd systemd service by running the following commands:
sudo systemctl stop gpsd.socket sudo systemctl disable gpsd.socket
Should you ever want to enable the default gpsd systemd service you can run these commands to restore it (but remember the rest of the steps in this guide won’t work!):
sudo systemctl enable gpsd.socket sudo systemctl start gpsd.socket
After disabling the gpsd systemd service above you’re ready to try running gpsd manually.
GPSD needs to be started up, using the following command:
sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock
Now GPS doesn’t work indoors – as it needs a clear view of the sky so for this I’ve placed the PI on the window sill. Next I ssh into the pi and run cgps. There is a simple GPS client which you can run to test everything is working:
The -s flag is there to tell the command not to write raw data to the screen as well as the processed data.
It may take a few seconds for data to come through, but you should see a screen like this:
If you have any problems and cgps always displays ‘NO FIX’ under status and then aborts after a few seconds, you may need to restart the gpsd service. You can do that via the following commands:
sudo killall gpsd sudo gpsd /dev/ttyS0 -F /var/run/gpsd.sock
If here still shows ‘GPS timeout’ or ‘NO FIX’,edit /etc/default/gpsd as below:
sudo nano /etc/default/gpsd
change it to look like this
# Default settings for gpsd. # Please do not edit this file directly - use `dpkg-reconfigure gpsd' to # change the options. START_DAEMON="true" GPSD_OPTIONS="-n" DEVICES="/dev/ttyS0" USBAUTO="false" GPSD_SOCKET="/var/run/gpsd.sock"
Then reboot. CGPS should work then.
NOTE:If the GPS receiver is new, or has not been used for some time, it may need a few minutes or so to receive a current almanac.You need 3 GPS satellites for a 2D fix (i.e. no height) or 4 satellites for a 3D fix. Once fixed,the LED ‘3D_FIX’ will blink.
You can view http://www.catb.org/gpsd/ to get more info about gpsd.You can also try to use the following command:
cgps and gpspipe should both just show curated data in the same way as your cat command did.
Try running gpsmon to get a live-streaming update of GPS data!
The gpsmon real-time packet monitor and diagnostic tool. (This replaces the sirfmon tool in older versions.)
# Draw a Koch snowflake from turtle import * def koch(a, order): if order > 0: for t in [60, -120, 60, 0]: forward(a/3) left(t) else: forward(a) # Test koch(100, 0) pensize(3) koch(100, 1)
Now, we make a small change to the function koch:
for t in [60, -120, 60, 0]: koch(a/3, order-1) left(t)
# Choose colours and size color("sky blue", "white") bgcolor("black") size = 400 order = 0 # Ensure snowflake is centred penup() backward(size/1.732) left(30) pendown() # Make it fast tracer(100) hideturtle() begin_fill() # Three Koch curves for i in range(3): koch(size, order) right(120) end_fill() # Make the last parts appear update()