Close
0%
0%

Simple Treadmill Tracker for games

Track a mechanical treadmill behaviour and use it to play games, using Raspberry Pi and a bit of coding.

Similar projects worth following
144 views
0 followers
In this project we're building a small tracker for a mechanical (or broken electric) treadmill. Walk on the treadmill to trigger a distance, time and pulse detection counter. Additionally, override keyboard input to control a character in a game. A physical RESET button is added to clear the counter logs. Thonny plus microPy is used to receive the treadmill info via serial, while a python script handles the GUI. Treadmill behaviour is tracked by a simple magnet/sensor system, which is the most common hardware you will find on a mechanical treadmill.

Step 1, treadmill hardware:

Locating and connecting the treadmill magnet sensor to your raspberry pi. Testing treadmill via Thonny.

Step 2, additional hardware:

Connecting the additional RESET button.

Step 3, python script:

Installing the TreadmillTracker script, debugging any issues and testing the keyboard override.

Step 4, expanding your setup for gaming:

We'll be discussing options to specifically use the treadmill for gaming. 



  • 1 × Mechanical treadmill Any type of mechanical treadmill wll work, all you need is access to one of the front wheels driving the rotation of the belt. You can also use a broken electrical treadmill, however be cautious of handling charged equipment and take all precautions to avoid injuries.
  • 1 × Raspberry Pi pico and its USB cable
  • 1 × Reed switch sensor optional: if your treadmill does not have one
  • 1 × Small multi-use magnet optional: if your treadmill does not have one
  • 1 × Momentary push button

View all 6 components

  • 1
    Prerequisites:

    Thonny: 

    Please download the latest Thonny version from the official website: https://thonny.org/

    MicroPython:

    After installing Thonny, add micropython: https://micropython.org/download/

    Here is a simple tutorial on how to flash micropython to Thonny: https://randomnerdtutorials.com/getting-started-thonny-micropython-python-ide-esp32-esp8266/

    Visual Studio Code:

    Please download the latest VS code version, or any other code editor you like to use: https://code.visualstudio.com/

  • 2
    Download associated files:

    You can find all needed files and scripts on its github repo: https://github.com/ineswisseme/TreadmillTracker

    Please download the repo as a zip and unzip in your project folder.

  • 3
    Step 1: Treadmill to Thonny

    Now that we have all needed components let's start this project!

    If you have a standard mechanical treadmill, locate the reed switch sensor. It is usually wired to a simple LED display screen that shows your steps, distance, and time. The sensor itself should be next to one of the front wheels of your treadmill so it can detect the magnet. If you cannot find your sensor/magnet combo you can add one yourself. Your reed switch should be positioned on the magnet path at a 2-5mm range. Here is a picture of my sensor, the grey circle on the wheel is the embedded magnet:

    The reed sensor uses two simple copper wires. We will now take the time to solder both wire to our raspberry. Since reed sensors are not polarized it does not matter which wire you connect to GPIO. Solder one of the wire to GP15 and the other one to any GND pin.

    Here is a wiring diagram of the final product for reference: 

    Since your iron is hot, let's add the RESET button to GP1 and any GND.

    About the plastic box: I recommend adding that at the end since you might go through a few debugging steps.

    Open Thonny, if you have followed the prerequisites your pico should be detected by thonny and you should be able to light and turn off its led.

    We will now upload the microPython script to your pico. In the github repo you downloaded, open the folder "Install". Copy and paste the main.py script into your raspberry pico, or create it yourself by copy pasting the following script:

    from machine import Pin
    import time
    
    PULSES_PER_KM = 6667  # The amount of pulses you will detect per kilometer. Adjust based on wheel size.
    LOG_FILE = "log.txt"
    
    pulse_pin = Pin(20, Pin.IN, Pin.PULL_UP)  # Reed sensor
    button = Pin(2, Pin.IN, Pin.PULL_UP)      # Physical button
    led = Pin("LED", Pin.OUT)
    
    pulse_count = 0
    distance_km = 0.0
    
    LONG_PRESS_DURATION = 6000  # milliseconds in range [0, ...)
    
    def load_log():
        global pulse_count, distance_km
        try:
            with open(LOG_FILE, "r") as f:
                line = f.readline()
                if "," in line:
                    pulse_count, distance_km = line.strip().split(",")
                    pulse_count = int(pulse_count)
                    distance_km = float(distance_km)
        except:
            pulse_count = 0
            distance_km = 0.0
    
    def save_log():
        try:
            with open(LOG_FILE, "w") as f:
                f.write(f"{pulse_count},{distance_km:.3f}\n")
        except:
            pass
    
    def clear_log():
        global pulse_count, distance_km
        pulse_count = 0
        distance_km = 0.0
        save_log()
        print("RESET")
        print("0,0.000")
    
    def check_long_press():
        if button.value() == 0:
            press_start = time.ticks_ms()
            while button.value() == 0:
                if time.ticks_diff(time.ticks_ms(), press_start) > LONG_PRESS_DURATION:
                    clear_log()
                    while button.value() == 0:
                        time.sleep_ms(10)
                    time.sleep(1)
                    return
    
    # main:
    load_log()
    print(f"{pulse_count},{distance_km:.3f}")
    
    last_state = pulse_pin.value()
    
    while True:
        check_long_press()
    
        current_state = pulse_pin.value()
        if last_state == 1 and current_state == 0:
            pulse_count += 1
            distance_km = pulse_count / PULSES_PER_KM
            save_log()
            print(f"{pulse_count},{distance_km:.3f}")
            led.toggle()
            time.sleep_ms(20)
    
        last_state = current_state
    
    

    This script reads the sensor pulses via the USB port your pico is plugged into. It then uses the diameter of your treadmill's wheel to calculate the distance you walk, saves it into a log.txt on your pico and clears the log when the physical button is pressed down more than 6 seconds.

    To make sure you have the proper values, let's calculate and update the PULSES_PER_KM variable. 

    wheel circumference =  π * diameter

    PULSES_PER_KM =  100 000  ÷ wheel circumference

    Change this value, save the script. Since this script is a main.py it only starts when the pico is plugged, meaning the previous version of it is running. Unplug/restart your pico for the update to take effect.

    To test the script, open and load it again in Thonny, then run it. As you walk on the treadmill, you should be seeing something like this in your shell, starting from zero:

    In the case where you have accidentally triggered the RESET button once, you will see this appear:

    The script is stuck in "RESET" mode since the button has only been pressed once. Press it again and let the reset command go through. I have added this mechanism in case of accidental shutdown from your computer or pico. This way, the log.txt keeps the latest "distance" and "pulses" values and it only clears with the RESET button. 

    Congrat! The first step is one!

    If you are not getting anything from this setup, please troubleshoot your sensor or button first then go through the main.py script test again.

View all 5 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates