Tag Archives: Hometrainer

Hometrainer 2.0 – Research

Out of curiosity, I’ve studied the inner working of my home-trainer. Without dismantling the bike, or disabling the functionality of the bike, I have succesfully connected the home-trainer to my Macbook, and can now calculate the cycling speed on my computer.

The home-trainer has a computer with a screen that displays all the information. This computer has two sensors connected to it: A heart rate sensor and a flywheel sensor. The flywheel sensor has a 3.5mm jack plug, which gave me the idea to connect it to the line-input on my soundcard (because a line input is nothing more than a A/D converter). This way it was not necessary to solder, or to create an interface.


After starting the audio recording, it struck me immediately that with every stroke of the pedals a small pulse was visible. Based on this I could calculate the distance for each stroke that is made, then I ultimately calculated the speed of the hometrainer on my computer.

Perhaps I will come around to make a GUI for my home-trainer, so I can automatically tweet the results of my workouts. Small detail: Java Sound is a really hard study. A quote I found:

There is no getPitch() method. You need to implement a spectrum analyzer for this. You might find a library online somewhere OR you can go to college, get an engineering degree, learn some DSP and still just throw your hands up in frustration and copy the implementation from a book

Hometrainer 2.0 – Application

After wrestling through the Java Sound libraries, I finally succeeded in writing a small hometrainer application. If you are cycling while watching a movie (something I do often), then this application will pause the video if you are riding too slow. Also, the current speed is constantly displayed.

The application constantly monitors the line-input, waiting for a pulse from the hometrainers’ flywheel. When the sinus reaches a (pre-defined) threshold, it gets registered, and I can calculate the speed I’m cycling at. For extra precision, I calculate with audio-samples instead of using the computer-time. This is because of the time it takes to fill the buffer. So far, I’ve been able to measure the speed fairly accurate.

A small disadvantage is that the computer on the hometrainer calculates the speed exponentially. This was apparently done to make the impression that the user’s riding very fast. For the sake of honesty, I wrote my own program in a way so that it works linear.

Hometrainer 2.0 – Source code

I’ve recently had a request to publish the source code of the hometrainer application I made a while back. The application was a continuation of the research I did on connecting a hometrainer to a computer using the sound card and calculating the bike’s speed by processing the audio signal.

The source code can be found at the GitHub repository.

I’ve rewritten most of the code to make it easier to use (notable by the use of tons of constants to tweak the application to your needs), but I also removed the part that controlled my media player. I couldn’t find a nice cross-platform method of doing this without resorting to gigantic APIs or messy hacks. The previous code used the Robot class in Java to simulate pressing the spacebar, but this is a rather messy method that can even complicate using your computer.

The code consists of three classes: Hometrainer, View and Main. The Hometrainer class does the work of monitoring the audio port and making sense of the incoming data. It extends the Observable class so it is easy to subscribe to updates by making your own class implement the Observer class (it this is unclear, you can read up on the Observer pattern). This is exactly what the View class does, it receives updates from the Hometrainer class and display the current speed and distance in a nice JFrame using JLabels. You can use this class as an example of how to grab data from the Hometrainer class. This leaves us with the Main class, which is a small class that initializes both classes.

Feel free to use the code however you want. If you have any questions, don’t hesitate to use the comments box below.