Good day!


Last week, I ended up creating a monstrous stand simulating the work of a bike computer that only worked on my desk. Until the end of the contest there were exactly two weeks. I understood that working on the technical part of the project at the same pace (8 hours a week), I will not be able to  finish it for the time allotted to me, but I promised myself and my readers that the project will be completed on time! This summer at work I got a small vacation and I decided to devote it to the Smart exercise bike computer project.


For five days, I have been working on a bike computer and now having the end result, I understand that I did not spend this time in vain!


Now let me return a few days ago and continue my narration from the place where I stopped.


Case of the bike computer


Looking at the mountain of boards and wires lying on my desk, there was one single desire – to design all this bacchanalia in the form of something solid and geometrically correct. First of all, I needed a suitable housing, and there were three ways:

  1. To make a simple but externally nice case made of cardboard;
  2. Make a solid and nice case using 3D printer;
  3. Finish some ready-made housing for the needs of a computer.


The first option I found simply indecent: ten weeks of working on the project and wrapping it in a  cardboard package? It's not even funny!


The second option seemed the best, if not for one thing: recently the hot-end of [my 3D printer] broke and it would take me a long time to repair it, given that time is the most valuable resource in my project.


So my choice fell on option # 3. In one of the commercial projects entitled "Electromagnetic monitoring system" for a number of system units, we use Bopla industrial cases:


In the photo you can see the "Wireless communication unit" of our commercial system. This unit is placed in the [Bopla CombiNorm-Classic CN 45 AK] case. The housing has the following characteristics:

  1. Dimensions: 45x75x109.5 mm;
  2. Material: ABS plastic;
  3. Mount mechanism: DIN rail clip;
  4. Protection class: IP40.


We purchased these cases in abundance and I was able to take one for my hobby project. Here’s a brief list of all the upgrades I had to do:

  1. DIN rail clip removed;
  2. a hole for the HD44780 display was cut out;
  3. one of clips fastening housing cover was cut off (it interfered with display mounting);
  4. a mounting bracket for the housing cover was made from the plate cut in step (2);
  5. stiffeners interfering with installation of boards removed from the housing cover;
  6. holes and threads for mounting the display and boards;
  7. holes for two USB ports and a mini-Jack port.


Of course, before making any changes, I carefully measured everything and made sure that the parts were really placed in my case. To perform all the above modifications of the case, I used following tools:

  1. Wireless drilling kit Makita 8391 DWPETK;
  2. Wireless Dremel 7750 + rigging Dremel 687;
  3. A set of taps and dies Zubr Expert 28129-N32;
  4. A set of files (made in USSR).


The work on the case was titanic: a high-quality thick-walled case Bopla has a large number of stiffeners, so milling holes took quite a long time. The result of my work is presented in the figure below:


Sandwich made of boards


Having installed the LCD into the case I tried to put development boards inside it As it turned out without any modifications you can install in the case only one board either FDRM-K64F or  FRDM-KW41ZFRDM-KW41Z This situation did not suit me because I wanted to use the case for installing two boards at the same time

At that very moment, I had an idea: what if I make a sandwich of boards? The KW41Z has PB sockets on the front side and PLS pins on the back side. So I can put one board on another as a sandwich. I had two options:

  1. The top board will be KW41Z, and the bottom one K64F;
  2. The top board will be KW41Z, and at the bottom will be another KW41Z.


Option (1) was the simplest, but it turned out that such a sandwich does not fit in my case. I tried option (2) and I was disappointed again: PLS pins and battery compartment on the bottom board interfered with the installation of our "duo" in the case.


As a result, I had to go to extreme measures – to bite off all PLS pins from bottom board and also to solder off the battery compartment. As a result, the sandwich finally fit into the case! The solution seems to me to be very aesthetic:


After connecting the boards, I immediately got a problem: the boards did not only start to be powered from one source, they began to be programmed from one programmer, and simultaneously! Thus, during programming, I had to separate the sandwich.


The solution to this problem was found with a detailed research of the [circuit] of the KW41Z board. In order for the boards to be programmed separately, it was necessary to remove fuses SH9 and SH10 from one of them. I preferred to perform this procedure on the bottom board.


Another problem caused by the direct connection of the two boards was that the UART interfaces were not connected correctly:

  • Rx line of upper board was connected to Rx of bottom board;
  • Tx line of upper board was connected to the Tx of bottom board.


For normal operation of UART, as it is known, a different connection is required:

  • Rx line of top board must be connected to Tx of bottom board;
  • Tx line of top board must be connected to Rx of bottom board.


Solving this problem turned out to be amazingly simple: it was only necessary to change the location of jumpers J30 and J31.


At this work on the sandwich was completed and until now no new problems arose.


Hardware finishing


Further bike computer assembly required several questions:

  1. LCD control board mounting;
  2. Placing control buttons outside the case;
  3. Placing lines for connecting sensors outside the case;
  4. Placing wires inside the case.


Let's start with the first question: LCD control board. As you remember, to connect the display to the microcontroller I use a [special board] with PCA9675PW chip.


The problem is that this board does not fit between the display and the sandwich of  FRDM-KW41ZFRDM-KW41Z As a result I had to turn it at an angle of 90 degrees and also replace the angle connector PLS with a more compact one and position it(the connector on the other side of the board As a result I got the following layout


The control buttons were to be connected in parallel with SW3 and SW4 buttons of KW41Z boards. The line J1-5 corresponds to the button SW3, and the line J3-1 to the button SW4.


I placed the buttons on an additional board, on which I also placed a mini-Jack connector used to connect the sensors. Two sensors are connected to the mini-Jack: a digital reed sensor (line J2-4) and an analog potentiometer used for [tension measurements] (line J4-2).


The final scheme of the Smart exercise bike computer is as follows:


As for the issue of connecting wiring, I used a little know-how, due to which I managed to save maximum space in the case of the device!


In the finished form, the configuration of the case is as follows:


After all the boards were securely fixed, and the case was assembled, the work on the hardware part of the bike computer was completed. In its final form, Smart exercise bike computer looks like this:


Final firmware adjustment


In the official repository of my project there are three branches:

  1. FRDM_K64F – branch, which for a long time was the main one;
  2. FRDM_KW41Z – branch that was supposed to replace (1) after the arrival of the KW41Z board;
  3. KW41Z_BLE – branch of the second KW41Z board realizing Bluetooth function.


Before I continue, I want to clarify these branches.


1. The branch (1) is frozen for today because The  FRDM-K64FFRDM-K64F board performed its role in my project and is no longer used All the results created in this branch are transferred to the branch (2).


2. The branch (2) according to my plan should completely replace the branch (1). The program is written in Keil MDK v5. The development plan for this branch is as follows:

  • At the first stage, the branch should become a "mirror image" of (1) with the only difference that instead of the K64F board, the KW41Z board is used;
  • At the second stage, the program should be debugged, i.e. fixing all problems identified at the previous stage (this is the latest firmware for the contest);
  • In the third stage, the BLE library should be ported in order to completely abandon the second board (after the end of the contest).


3. The branch (3) is for the second board (the bottom one in our sandwich). This program is written in MCUExpresso IDE and is intended for:

  • receiving data from the top sandwich board;
  • realization of connection with the smartphone;
  • transfer of training data to the smartphone application (GATT CSC protocol).


Now that my readers have a complete idea of the project topology, I continue my story. Porting the firmware from K64F to KW41Z passed without incident, I quickly enough managed to transfer all the functions and libraries I developed, having carried out some settings related to the difference in microcontroller architectures.


I will not cite the code of the software that I developed, but only briefly list all the work that I had to do.


For branch (2) I performed the following improvements:

  • accuracy of speed determination increased (this problem was mentioned [here]);
  • work stop recognition function improved;
  • accuracy of measuring time of training significantly improved;
  • thread of sending data to the second board reworked (about this below);
  • several mathematical formulas refined;
  • recognition of short and long pressing of buttons added (about it below).


As for the second board, the following innovations were introduced into the branch (3):

  • accuracy of turns measurement (W) considerably increased;
  • ability to measure both speed and turns of pedals added;
  • ability to use third-party programs on smartphone added;
  • procedure of buttons operation is reworked.


Let’s consider the algorithm for measuring turns (W), implemented in the protocol [GATT CSC]:


W = (T2 – T1) / (N1 – N2) / 1000 * 60



  • W – pedal rotation angular speed [rpm]
  • T1 – time at moment i [ms];
  • T2 – time at moment i + 1 [ms];
  • N1 – the number of pedal turns at moment i;
  • N2 – the number of pedal turns at moment i + 1.


[Last week], I decided to operate with N1 and N2, taking T2 – T1 = const = 1s. Thus, the measurement error for W was ±60 rpm. I managed to reduce the error by artificially multiplying N2 and N1 by 100, but this led to the fact that W is multiplied by 100. In other words, the way I was going was erroneous.


Looking at the above formula with a fresh look, I realized that if we take N2 – N1 as a constant and operate with variables T1 and T2, the measurement error for W is ±0.06 rpm at W = 60 rpm!


When measuring speed (V) in the same way, the measurement error is ±0.032 km/h for V = 32 km/h or 0.1%.


As for working with third-party applications, it turned out that some programs use 1ms as a step for quantizing time, and some 1.024ms. The possibility of selecting a step is implemented in program from branch (2) as a directive BLE_1024.


The last but not the least important point in the development of my project was the issue of management. It so happened that buttons SW3 and SW4 are connected simultaneously to the two KW41Z boards. How is it possible to determine which board I want to transfer command to?


Everything is very simple! A short pressing of buttons is only perceived by the top board, and long pressing (holding the button down for more than 1 second) – by the bottom board only.


As a result, the buttons SW3 and SW4 perform the following functions:

  • SW3 short press: switching parameter on left side of screen;
  • SW4 short press: switching parameter on right side of screen;
  • SW3 long press: turning on Bluetooth / searching for Bluetooth devices;
  • SW4 long press: disconnecting the connected Bluetooth device (smartphone).


Testing of the bike computer in reality


The hardware platform of the project is developed, the writing of the firmware is completed. It remains only to experience our computer in real training!


I will give a brief list of preliminary preparations that I performed before this crucial step:

  1. Fasten the new bike computer in place of the old one;
  2. Connect sensors to the computer through mini-Jack;
  3. Connect PowerBank to the computer via USB connector.


In the beginning, I had a small problem: PowerBank periodically turned off, the fact that it is designed to charge a mobile phone and if you connect it to a low power consumption device, it simply turns off about every 60s! This problem may be fixed in various ways:

  1. Add to the bike computer an additional ballast resistor;
  2. Connect a mobile phone to the bike computer, so we can not only increase the load on PowerBank but also recharge the phone.
  3. Enable the special PowerBank mode that prohibits automatic shutdown;


I chose way (3): if I press the power button three times on my PowerBank, it switches to a special mode of operation and is no longer disconnected (this function was not documented and was detected experimentally by me).


So, instead of a thousand words I offer you a short video on which I test my creation:


In this video, I tried pairing the computer with the [NXP IoT Toolbox] application. Above, I mentioned that my computer is basically compatible with any applications that support the GATT CSC protocol. To confirm these words, I offer you screenshots of the work of my bike computer in conjunction with another application: [Cadence & Speed Computer].




In conclusion of today's article, I could sum up my project and finish it, but I prefer a different approach., I want to dedicate the final – eleventh article to summing up. It will be something unusual, set out in a manner that is not peculiar to me, so I'm a little worried about how my readers will perceive this work.


As for today's day, I can state the success of my enterprise! Smart exercise bike computer is ready to work: the hardware and software parts of the project are completely finished, including a set of functions for working with Bluetooth. Naturally, many things can be improved, but we'll talk about this and many other things in the next article, and for today, allow me to bow out!


Thanks for reading and have a nice day!