1) Oxygen saturation and Covid-19
In this incredible period of our life we've learned a lot of things about viruses, lungs, surgical masks, soap and washing hands. Everybody read about symptoms like coughing, fever and breathing difficulties. We've also known that one way to measure this breathing difficulty is reading the amount of oxygen in our blood.
This measure can be read indirectly with a medical device called oximeter. You have probably already seen it, it's a non invasive device that is placed on a finger with some pulsating lights that do the work.
You can find it on Amazon, for example this one here:
Normally, when you're ok, you have a percentage of oxygen saturation near or grater than 95%. When saturation goes down below 90% and you have cough and fever, it's a problem.
So we will try to build an oximeter!
2) How does an heartbeat sensor work
To measure oxygen saturation, we first need to learn how an heartbeat sensor work. I've got an heartbeat sensor named KY-0039 which is made with an infrared led and a photodiode. (it should be this OP550A or LTR-301)
I've found it in a kit with many sensors, you can see it in this pic (from internet):
As you can see in the circuit, it's just a infrared led that lights a photodiode. There are also two resistors to protect the led and read the small signal from the sensor. The finger is placed between the photodiode and the infrared led like this photo (from this site):
The light emitted by the infrared led is partially absorbed by the nail, the skin, and all the other parts of your finger, but it's not constant because it changes following the changes of the blood running in your veins. When your heart makes a beat the blood is pushed in your veins and the light absorption changes. With the S pin of KY-039 we can measure the current generated by the light absorbed by the photodiode.
3) Measuring heartbeats by finding the peaks of a signal from a sensor
Reading a good value from a variable signal it's not so easy, th signal is very low and there is a lot of noise, so we need to make some math to find values that mean something useful.
I've got to thank this useful post from Johan Ha, which explains how to plot the average of a small number of samples from the signal and it also explains how to remove the noise made by an home lamp (that light has a small noise). But I've also find that my heartbeat sensor records good signal with good ambient light, if I cover the sensor with a black cloth the signal has too much noise.
In its work Johan Ha makes an array in which push a value and drop a value to mantain the average of the last X values read from the sensor.
He has also described a way to find the rising of the signal, when there are N values growing (when a value is grater than the preceding value for N times), it is a peak.
Using SerialPlot I've seen better the number of measures that allow us to define a correct N (rise_threshold constant in the code). In this chart below if you define a number grater than 7, for example, you will miss some beats.
Once you've find the peaks, just count them, or calculate the time between a small serie of beats to determine the BPM rate (Beats Per Minute).
With these tricks, counting the number of peaks and BPM is quite easy.
4) How to hack the KY-039 sensor to find oxygen saturation
Our blood absorbs light in a different way with the change of the wave-length of the light. The red light is absorbed better by the blood with more oxygen, so we can compare the measures and find the percentage of oxygen in our blood. That value is called Sp02%.
SpO2% stands for peripheral capillary oxygen saturation, an estimate of the amount of oxygen in the blood. More specifically, it is the percentage of oxygenated haemoglobin (haemoglobin containing oxygen) compared to the total amount of haemoglobin in the blood.
Our KY-039...Read more »