Skip to main content

Building A GPS Data Logger For An Electric Bike

Having recently converted my folding bike to electric, I have spent a great deal of time exploring the freedom that electric transport and cycling have to offer. In this regard, I have been thinking of ways to try and share this experience with a wider community, and aside from the obligatory Instagram posts and some shaky GoPro footage, I determined it would be best to start tracking my routes using a GPS datalogger and sharing them online in an attempt to illustrate the versatility of green technology.

In this article, we will explore the MicroPython scripting language alongside the Pycom development environment to start recording our cycling routes using GPS, an SD card and the Google Maps service.

Parts List

  • Pycom WiPy Development Board (172-0647)
  • Pycom PyTrack Expansion board (136-8853)
  • USB Power Bank
  • Micro USB Cable

Building the Datalogger

For this project, we will be using the WiPy development board as our main microcontroller and script interpreter alongside the PyTrack expansion board which features both the GPS transceiver and the SD card socket needed to record our location data. We will be programming the board using the PyMakr plugin for Visual Studio Code which also includes a REPL terminal window for easy debugging.

Pycom WiPy connected to PyTrack expansion board and USB power bank

Pycom WiPy connected to PyTrack expansion board and USB power bank

First, we need to setup the GPS transceiver using the constructor function and create a reference from which to read our location data. Once the GPS establishes a link to the satellite network, we can then return our longitudinal and latitudinal location using the coordinate function. Nesting this function in a loop we can read and process this data periodically before thinking about saving it to our SD card.

GPS = L76GNSS(Pycoproc(Pycoproc.PYTRACK), timeout=30)

while True:

    location = GPS.coordinates() 

    latitude = location[0]
    longitude = location[1]

Initialise GPS to read latitude and longitude

As Google Maps limits the number of positional points we can upload, it would be prudent to filter some of this location data before saving it to our SD card. To this end, we can remove any potential redundancy by keeping track of the previous reading and comparing it against the current. Using an if statement we can compare the two values and skip the save process if they match.

GPS_Store = ''

while True:

    location = GPS.coordinates()

    if location == GPS_Store:
        time.sleep(5)
        continue

    latitude = location[0]
    longitude = location[1]

Filtering duplicate data

Once we have removed any duplicate readings, we can insert our SD card into the socket and use the mount function to initialise our connection to the file system. As we will be using the Google Maps service to display our cycle route, we need to format our location data into the standard comma-separated variable (CSV) file format so it can be imported later.

sd = SD()
os.mount(sd, '/sd')

Mounting the SD card to the filesystem 

The device will be configured to create a new data file on boot so we don’t overwrite any existing data while making recording sessions easier to differentiate. To do this, we will use a conditional loop and the open function to find and allocate a uniquely numbered filename followed by the csv file extension.

FileNo = 0
FileName = ''

while True:

    try:
        FileName = '/sd/' + str(FileNo) + '.csv'
        FileNo = FileNo + 1
        open(FileName, 'r')
    except:
        break

f = open(FileName, 'a')
f.write('Name,' + 'Latitude,' + 'Longitude' + '\r\n')
f.close()

Creating a unique filename using a sequence of numbers

Saving our location data is then a simple matter of creating a string containing our longitude and latitude separated by a comma and delimited by a carriage return. We can then open the file in write mode, write our data and close the file to save. We should then be ready to upload our route data once we have finished recording.

GPS_Store = ''

while True:

    location = GPS.coordinates()

    if location == GPS_Store:
        time.sleep(5)
        continue

    GPS_Store = location

    latitude = location[0]
    longitude = location[1]
    
    f = open(FileName, 'a')
    f.write('Fix,' + str(latitude) + ',' + str(longitude) + '\r\n')
    f.close()

    time.sleep(5)

The full tracking loop including filtering and CSV data formatting

The full MicroPython code for this project can be found on GitHub.

Testing and Results

With the hardware and software operational, I could then test the system using my e-bike to measure the GPS accuracy and data logging performance as I progressed along a known route. To do this, I mounted the hardware in a handlebar bag and powered it using a high-capacity USB power bank.

Mounting the Pycom hardware to my bike using a handlebar bag

Mounting the Pycom hardware to my bike using a handlebar bag

Having chosen an appropriate cycle route, I set out cross-country on my intrepid folding bike, making sure to reset the board at the beginning of the journey to start a new data logging session. Fortunately, it was a sunny day with clear skies which meant test conditions were perfect, a rarity in the UK!

Testing the limits of my bike and the data logger

Testing the limits of my bike and the data logger

Despite there being a lot of firm, uneven terrain, the hardware worked brilliantly, which meant I was free to take some great shots of the testing process. Towards the end of the route, I finished the recording session by resetting the board once more and returned home. I then checked the data for recording quality and was very happy with how well it had been transposed into CSV file format.

 

Checking the quality of the CSV data file... Perfect!

Once the cycle route data had been recorded it was then a simple matter of importing it into the Google Maps service. Using my Google account, I could use the “MyMaps” feature to create a user-defined map and import the route as a series of GPS location data points. As the data was correctly formatted in a CSV file, this was as easy as clicking import and choosing my data file.

Importing the data into Google Maps worked beautifully

Importing the data into Google Maps worked beautifully

Success! My cycle route was recorded accurately and consistently by the Pycom hardware which in turn was illustrated excellently by the Google Maps service. It is my hope that the novel application of technologies as seen in this article can be used to help build an engaging narrative around sustainable alternatives and I am certainly interested to see how far you could take a project like this.

A keen maker and electronic engineer with a passion for the environment, renewables, alternative transport and anything off-grid. Man with a van and founder of the Kickstart Kamper sustainable campervan project. Grassroots Education Sustainability Ambassador. BrightSpark 2017. BEng. KickstartKamper.co.uk
DesignSpark Electrical Logolinkedin