Custom Fitness Tracker Tutorial
There are many fitness trackers on the market today, but most of them do not give users the ability to easily add custom software or hardware. In this tutorial, you will learn how to do both! TinyCircuits Wirelings allow you to choose between 30 unique sensors for your projects! This is a new step forward in the **[Quantified Self](https://en.wikipedia.org/wiki/Quantified_self)** movement!
### **Hardware: **
* **[TinyScreen+](https://tinycircuits.com/products/tinyscreenplus) **
* **[MicroSD TinyShield](https://tinycircuits.com/products/microsd-tinyshield)**
* **[150 mAh LiPo Battery](https://tinycircuits.com/products/lithium-ion-polymer-battery-3-7v-150mah) -or- [500 mAh LiPo Battery](https://tinycircuits.com/products/lithium-ion-polymer-battery-3-7v-500mah) **(13 hr vs 45 hr approximate battery life)
* [**Wireling Adapter TinyShield**](https://tinycircuits.com/collections/wirelings/products/wireling-adapter-tinyshield "Wireling Adapter TinyShield product page")
* [**LRA Motor Wireling**](https://tinycircuits.com/collections/wirelings/products/lra-driver-wireling-drv2605 "LRA Motor Product page")
* [**Pulse Oximetry Wireling**](https://tinycircuits.com/collections/wirelings/products/pulse-oximetry-sensor-wireling "pulse sensor product page")
* [**Microphone Wireling**](https://tinycircuits.com/collections/wirelings/products/mems-microphone-wireling-spw2430 "Microphone Wireling")
* [**Accelerometer Wireling**](https://tinycircuits.com/collections/wirelings/products/accelerometer-wireling-bma250)
* **Wireling Cables** (1 for each wireling you plan to use)
* [**MicroUSB Cable**](https://tinycircuits.com/collections/accessories/products/micro-usb-cable-3-feet)
* [**WiFi TinyShield**](https://tinycircuits.com/products/microsd-tinyshield) (optional, details at the end of the article)
* [**Arduino IDE**](https://www.arduino.cc/en/Main/Software)
* **[Fitness Tracker Code](https://cdn.shopify.com/s/files/1/1125/2198/files/FitnessTracker.zip?4037)**
* **[Google Sheets Uploader](https://cdn.shopify.com/s/files/1/1125/2198/files/SheetsFitness.zip?4036)** (optional, advanced)
* [**Blynk Fitness SuperChart Sketch**](https://cdn.shopify.com/s/files/1/1125/2198/files/BlynkFitness.zip?4038) (optional, advanced)
### **Step 1: Choosing the right Wirelings**
For our custom fitness tracker, we chose to use the accelerometer, pulse oximetry, and LRA (tiny vibrating motor) Wirelings. Our goal was to track our steps using the accelerometer and issue notifications using the LRA. Then, using the pulse oximetry Wireling we would be able to measure your heart rate and blood oxygen level.
In addition, we created a sleep tracker functionality! By observing the nightly movement of the user and their heart rate, we were able to measure sleep quality and duration. Based on **[this study](https://www.nature.com/articles/1300146)**, we know that heart rates alone provide a significant clue as to which sleep stage you are in.
Of course, there are many other interesting combinations. Using the general tips provided in this article, you will be able to easily modify our code to match with any Wireling you choose!
### **Step 2: Assembly**
* First, gather your Wirelings and Wireling cables.
* Plug your Wirelings into the Wireling TinyShield using the Wireling cables
* Connect the larger boards using the 32-pin tan connector (TinyScreen+, MicroSD Card TinyShield, Wireling Adapter TinyShield)
* Insert the microSD card to the MicroSD Card TinyShield
* Plug in the battery to the TinyScreen+
Connect all of this with a micro USB cable to your computer to be ready for uploading the program
_Here, the pulse Wireling measures the rhythmic flow of blood into and out of your finger. The small noose keeps a light, steady pressure to ensure the most accurate readings._
### **Step 3: Uploading the code to your processor**
If this is your first time using TinyCircuits products, please refer to the **[TinyScreen+ Setup Tutorial](https://learn.tinycircuits.com/Processors/TinyScreen+_Setup_Tutorial/)**to configure the Arduino IDE correctly.
* Then, download the zip archive provided in the **Software** section above.
* Open the FitnessTracker.ino file with the Arduino IDE or text editor of choice.
When using the Wireling Adapter TinyShield, it is important to understand port selection. You must indicate in your sketch which port each Wireling is using. Note that the port numbers are printed on the reverse side of the TinyShield. The 4 ports are numbered 0-3.
The good news is that our sketch is able to automatically detect which port the Accelerometer, LRA, and Pulse Oximetry Wirelings have been connected to.
If you want the ability to record audio using the MEMS Microphone Wireling, you would need to choose a specific port number and always connect the Microphone Wireling to the same port. Then, in your sketch, you would only need two lines to read data from the Wireling.
_selectPort(1); // Correlates to port numbers labeled on Wireling device
__analogRead(A1); // Same number as port number_
There are a few parameters that you can set at this stage to personalize your fitness tracker. In order to set them, navigate to the line shownin the table and modify the value to the right of the equals sign.
|const int AGE \= 25;
|const int STEP\_TRIGGER \= 250;
const byte seconds \= 10;
const byte minutes \= 52;
const byte hours \= 23;
const byte day \= 16;
const byte month \= 8;
const byte year \= 19;
|const int BEDTIME\_HOUR \= 23;
const int BEDTIME\_MINUTE \= 53;
* Next, you will need to install two libraries from the Arduino library manager, shown below:
_The library manager can be found by going to Tools-> Manage Libraries in the Arduino IDE_
* You did it! Now let's upload the code.
* Make sure that you have the correct port selected. The correct port number will vary, but your processor should appear to the right:
* Then, upload the code!
* After uploading the code, wait a few seconds and try tapping any of the buttons on your TinyScreen+. You should see the fitness display appear.
At this point, the tracker will begin monitoring the time, step count for the current day, percentage of your step goal, pulse, oxygen saturation, and remaining battery percentage. Also, once the tracker has detected that you have fallen asleep it will measure your sleep quality and determine your current sleep stage. All data is recorded to tracker.txt in a CSV format every 30 seconds by default.
Another text file called quartiles.txt will store quartile data of your heart rate at night. It is intended to be read by the sleep quality algorithm. Since everyone's heart rate varies, it is much more accurate to base calculations off of your past heart rate than preprogrammed values. Avoid editing the format of this file or it will not be read properly by the sketch.
Finally, a third file called sleephistory.txt is created. This file contains two columns in a CSV format, the date and the sleep quality for that date. It is updated every morning when you wake up and contains more accurate information after your sleep chronology was analyzed. Sleep chronology contrasts your sleep stage transitions against the optimal configuration of lighter sleep at the beginning and end of the night and deeper sleep in the middle.
This is just the beginning of what you can accomplish using Wireling sensors! See the optional tips below for how to incorporate any Wireling into your own sketches. If you need any help, feel free to email us at email@example.com!
### **Wireless setup tips (advanced, optional)\***
* There are some limitations to WiFi in this application, which is why we recommend using a MicroSD card:
1. WiFi uses about 3x the power (30ma vs 10ma), which means that you will likely need to use a significantly larger battery.
2. During any period of time in which you lose connection to WiFi, data will be lost. There are workarounds that are compatible with some IoT apps, but not others. For example, we have written **[a solution](https://cdn.shopify.com/s/files/1/1125/2198/files/SheetsFitness.zip?4036)** that starts storing data in the TinyScreen's RAM when you lose WiFi connection and then attempts to upload it to Google Sheets once a connection is regained. However, if you remain disconnected for several days, you will run out of RAM. This is recommended for _advanced users only_ as you will need to merge the solution into this sketch.
3. The WiFi module is longer than the rest of the stack by about 0.5''.
I see you're still undeterred! Wireless setup is quite possible with this project and we have created some example code and pointers to help you on your journey.
* In order to interface with Google Sheets, please follow step 2c of the weather station tutorial. We have also included some example code in the software section that was working perfectly with an earlier version of this project. It is up to you to see if you can adapt it to fit your needs.
* If you wish to interface with Blynk, we recommend you use the SuperChart widget, as explained in **[The Internet of Things: Getting Started with Blynk](https://tinycircuits.com/blogs/learn/getting-started-with-blynk) **tutorial. We also have included some example code for this route in the software section.
## Contact Us
If you have any questions or feedback, feel free to email us or make a post on our **forum**
. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.
Thanks for making with us!