Category: Ketosis detector

Ketosense – An Arduino based ketosis detector

In this post I will go through the hardware and software of the instrument I have made. If you are curious to why I have made it, I would recommend that you start reading the other posts I have done about this project and work your way forward. Just click on Ketosis detector under categories.

Introduction

The instrument´s purpose is to detect the amount of acetone gas that is present in a persons breath.

By measuring the amount of breath acetone gas present in a persons breath, it should be possible to determine wether or not a person is in a state called ketosis. Ketosis is, very simplified, when the body runs on fat rather then carbohydrates. If you eat a ketogenic diet, your ketone levels will be elevated. Currently, the only way to monitor your ketone levels are either to take a blood based test or a urine based test, with test strips that changes color depending on the amount of ketons that are present. Both methods work fine but the blood method is very expensive, about 2$ per test, and the urine method is not very precise. Being the curious type of person, and getting inspired by a podcast where Steve Gibson was talking about building an instrument like this, I decided to give it a shot myself.

On a very high level, the instrument consists of an electrochemical gas sensor that is sensitive to various types of gases, a combined hygrometer/thermometer and an Arduino Uno board. The electrochemical gas sensor and hygrometer/thermometer has been placed in a plastic case that serves as a ”gas chamber” that you blow into to collect the gas sample. If there is gas present in the air, the resistance of the sensor will decrease. The decrease of resistance is dependent on three factors; gas concentration, humidity and temperature. If these values are known, it is possible to calculate the gas concentration in PPM (parts per million) using the data sheet provided by the manufacturer.

Does it work?

I don’t normally eat a ketogen diet, but in the name of science and curiosity I gave it a shot. Over the last two weeks I have and taken measurements with the Ketosense and compared them to the results from urine test strips. During the first 10 days, I ate a strict ketogen diet consuming less then 50 grams of carbohydrates a day. I took a reading each morning before breakfast, and one right before going to bed. However, the urine test strips does not have a very granular scale, so it is difficult to say what the exact concentration is  when you go above 1,5 mmol/l. The steps of the Keto-Diastix are 0, 0.5, 1.5, 4, 8 and above 16.

The measurements taken with the Ketosense actually correlates a lot better then I expected to the reference measurements from the Keto-Diastix. This means that the hypothesis that it is possible to calculate ketone levels based on the amount of acetone present in a persons exhalation, seems to be sound. One realization I had was that you have to eat very large amounts of fat to really get the ketones going, it was not until I started to eat a few spoons of 100% coconut fat a day that I reached higher levels of ketones, (witch can be seen around measurement 17 in the graph).

Hardware

Main Unit

Mouthpieceketosense_mouthpiece

Total price for the components is about 30$.

TGS822 – Electro chemical gas sensor

This sensor works like a resistor, the higher the gas concentration is, the lower the resistance gets. It has 6 pins, the two middle pins are the power to the internal heater and the other are the resistor connections. There are two pairs of resistors connectors called ”A” and ”B”, but they are essentially the same so I just soldered them together. It is very important that the power provided to the internal heater is exactly 5 Volts, or else you risk damaging the sensor. I used an LM7805L voltage regulator to provide the power for both the TGS822 and the DHT11 sensor.

This sensor is far from ideal to be applied in an application as this, it is slow to start, sensitive to humidity and temperature, sensitive to many other gasses other than acetone, and almost impossible to calibrate without special equipment. A wisdom I can pass on, is that one of the gasses the TGS822 is sensitive to is ethanol so don’t try to demo or measure your breath acetone during a dinner where there is wine served. The sensor is so sensitive that it goes bananas if you just have a glass of whine next to it.

The TGS822 is connected to the Arduino on analog pin 0 in parallel with a 10k resistor to create a voltage divider circuit.

DHT11 – digital temperature and humidity sensor.

I added the DHT11 sensor because the characteristics of the gas sensor changes depending on temperature and humidity. Since a persons breath is both humid and warm, it was necessary to factor that in when trying to determine the gas concentration. At first I had placed both the gas sensor and the temperature and humidity sensor close to each other. This turned out to be a mistake since the heater in the gas sensor heated the air around it, thus drying out the air and elevating the temperature. To avoid this, I moved the temperature and humidity sensor to sit as far away from the gas sensor as possible.

The DHT11 is not very fast, especially when it comes to humidity. It can take about 2-3 minutes before the humidity reaches it’s max value. This presents a bit of a problem when it comes to scaling the reading from the gas sensor, depending of humidity from the DHT11. The gas sensor reaches it’s max value just seconds after you finished blowing into it. So if you read the DHT11 at the same time as the gas sensor reaches it’s max value, the humidity value will be to low because the DHT11 is slow.

After blowing into the instrument a couple of times I noticed that the temperature and humidity of a persons breath was stabile at around 60% humidity and 28-29ºC. After realizing this I hardcoded the temperature and humidity to those values to get the scaling right without having to wait for the DHT11.

The DHT11 sensor is connected to digital input pin 10.

16 x 2 Character LCD Display Module

The display is wired as such:

RS: Pin 2
EN: Pin 3
D4: Pin 4
D5: Pin 5
D6: Pin 6
D7: Pin 7

I used the LCD crystal library by David A. Mellis, Limor Fried,  and Tom Igoe to control it.

Buttons

There are three switches on the Ketosense. One to trigger a reset cycle of the gas sensor, one to reset read max value and one to toggle between the result being displayed as PPM or mmol/l.

resetMaxSwitchPin = 13;
resetSensorSwitchPin = 12;
toggleModeSwitchPin = 11;

Mouthpiece case

When choosing a case to use as a mouthpiece, make sure it is easy to open to be able to ventilate all gas in between readings.

Breadboard view of the whole circuit

Ketosense_0.2_bb

Software

This is my first time doing anything with an Arduino so I learned as I went along, the code is not very pretty and I am sure that there are many better ways to get it done, but this way works good enough for me. The code is available at my Github, it is perhaps not super clean but I will try to explain the concepts in words here to perhaps make it a bit more understandable.

Initiation and reset sequence

As I mentioned in the hardware section, the TGS822 sensor is quite slow. When you power on the Ketosense it can take up to 10 minutes before the sensors resistance is stabile, so to determine if the sensor is stabile, readings are taken continuously every second and compared. If the reading changes less then 5 steps over 20 seconds, the sensor is determined to be stabile otherwise the sequence restart.

After a person has blown into the Ketosense it takes a few minutes for the sensor to reset, so the same method as described above is used to determine if the sensor is able to do another reading. This is triggered by pushing one of the buttons.

On the left you can see the Ketosense while it is in startup mode waiting for the sensor to become stabile. On the top row H = current humidity, T = current temperature and i = what step in the 20 second cycle the algorithm that determines if the sensor should be considered stabile is at. On the bottom row the Max and Min value detected on when reading the voltage from the sensor, if the sensor is to be considered stabile and ready to use to take a measure the difference between these two values has to be less then 5. After the sensor has been considered stabile the user is prompted to start the measurement by blowing into the mouthpeice.

Making sense of the readings from the TGS822

The only info I had about the TGS822 was what was written in the datasheet. I wrote a post a little while back called ”Calibrating a Figaro TGS822 sensor, by drawing…” so take a look at that if you want to know more about my thoughts on how to use the info in the data-sheet.

Since I feed 5V to the sensor and it is connected in a voltage divider circuit with a 10k resistor the voltage going into the A0 port of the Arduino is between 0-5 volts. The Arduino then divide it into 1023 parts so the value I actually read from the sensor is a voltage where every step is a change of 5/1023 = 0,0048V.

To get the resistance of the corresponding to the voltage read use this formula:

Rs_vs_Rl

To find the relation between resistance and gas concentration I did some regression analysis using libre office. I found out that a reasonable approximation to the ppm vs voltage can be achieved with the functions:

if resistance is between 50kΩ & 3600Ω
logPPM = (log10(tempResistance/R0)*-1.5512)+2.5911
PPM = pow(10, logPPM)

if resistance is below 3600Ω
logPPM = (log10(sensorResistanec/R0)*-0.9768)+2.4906
PPM = pow(10, logPPM)

How I went about calculating my sensors R0 is described in the post called ”Calibrating a Figaro TGS822 sensor, by drawing…” I noticed that in the graph for gas vs resistance in the datasheet, it stated that in Air the resistance is ≈ R0 * 19, so I simply divided the startup resistance of the sensor  after it had stabilized with 19 and went with that.

calculated value vs datasheet graph

 

I applied the same reasoning for temperature and humidity graph and there the function is:

scalingFactor = (((Temperature * -0.02573)+1.898)+((Humidity*-0.011)+0.3966))

The last thing to do is to convert from PPM to mmol/l since that is the most common unit when talking about ketone levels. Im not entirely sure that I got it right but it seems to give reasonable results.

float ppmInmmol = (((float) PPM / 1000) / 58.08);
ppmInmmol = ppmInmmol * 1000;

58.08 is the molar weight of acetone.

Fine tuning the scaling function

After doing the 14 day journey into ketosis I had enough data so tune my original scaling functions to fit the reference of the Keto-Diastix.

logPPM = (log10(sensorResistance/R0)*-2,6)+2.7
PPM = pow(10, logPPM)

I also updated the R0 from 3000 to 4500.

new_old_scaling_function

Collecting the measurement

After the Ketosense has started and is stabile, the display prompts to the user to blow into the mouthpiece to start. Actually the measuring has already started, even before that point, however the screen is only updated once the sensor is reading changes and thus indicating that someone is blowing into the mouthpiece. The samples are taken three at a time with 5ms between them and compared to each other. For a measurement to be considered valid, all three samples has to have the same value. This is to minimize the risk for an unexpected voltage rise that would give a false indication as a max value. If a sample is considered valid it is then scaled based on humidity and temperature. As I wrote in the hardware part, the humidity is hardcoded to 60 and the temperature to 28 due to the DHT11 sensor being much slower then the gas sensor, thus causing the scaling to be wrong if both sensors are being read at the same time.

Ketosense_measuringEvery time the collected value is valid, (3 samples after each other that are the same), the screen is updated. The current reading is displayed on the bottom row as ”Now:” If the current value is greater then the last one, the ”Max:” is also updated. Typical behavior is that during the first 30 seconds after someone has blown into the Ketosense mouthpiece the max value keeps increasing, therefor the gas collection chamber is crucial to give the gas sensor time to react. It is possible to tell when the actual max value has been reached since the ”now value” is decreasing and is lower than ”max value”.

To reset the instrument to make it ready to use for another measurement, remove the cover of the mouthpiece to ventilate the current sample and then push the reset button to trigger the same sequence as during startup to detect when the sensor has been reset. The reset button is actually the little nut (button) in the middle beneath the dispalay, (the microswitch was not tall enough to reach out of the case, so I extended it by gluing a nut on top of the switch). The nut on the left is to switch between displaying the result as PPM or mmol/l and the one to the right to reset the current max value.

Discussion

I would not go as far as to claim that my instrument works, It is tricky to decide if it does since I don’t have a reliable way to get a reference other then the keto-diastix, (and those are not exact enough). But I will go so far as to say it looks promising.

Improvements

If I had access to an instrument reading blood ketone levels, it would be possible to further improve the scaling functions relative to that. Take one measurement of blood ketone levels, and then blow into the Ketosense and write down the raw voltage from the TGS822 rather than the scaled value. Do this for a couple of different values and do some regression analysis on the results and I would have a function that was entirely adapted to my sensor and to displaying ketone levels. However I am not prepared to spend money on a blood ketone level measuring device, but perhaps someone else will after having being inspired by this post.

Another application for this device is actually a breath alcohol detector, since the sensor is actually sensitive to ethanol as well as acetone.

Sources and additional information

I found three different data sheets for the sensor while searching:

Annonser

Calibrating a Figaro TGS822 sensor, by drawing…

After consulting with the boys and girls at chemicalforums.com about how to produce an ethanol gas with a 300 ppm without having to buy a lot of fancy gear and thus finding out that it was more difficult then I initially thought I have reluctantly decided that I don’t think I will be able to pull it of. There for I have decided to work with what I got. What I got is air and a datasheet.

The data sheet provided by Figaro for the TGS822 only goes down to 50ppm, however the graph looks pretty logarithmic linear to me so I decided to add the 10-50ppm part my self making a bold assumption that it will be logarithmic linear in that interval as well.

From the data sheet we get the relation between RL and RS which is a voltage divider circuit.

Rs_vs_RlFrom the graph in the data sheet we can also see that the resistance of the sensor in air is RS (air) = R0 * 19.

If we combine these two facts we can express R0 as a relation of RS (air) and the value of RS (air) can be deduced by reading the voltage of the sensor and using the voltage divider formula.

RS (air) / 19 = R0 in my case RS (air) = 78kΩ. => R0 = 4105Ω

When we have R0 we can make a table to relate resistance (RS) to ppm by reading the scaling factor of RS/R0 from the graph for different gas concentrations.

Rs in air = 78000 Ro = 4105,26315789474
ppm Scaling factor Rs = Ro * Scaling factor
0 19 78000
10 15 61578,947368421
10 10 41052,6315789474
20 9 36947,3684210526
20 7 28736,8421052632
30 6 24631,5789473684
30 5,7 23400
40 4,7 19294,7368421053
50 4 16421,0526315789
60 3,5 14368,4210526316
70 3,2 13136,8421052632
80 3 12315,7894736842
90 2,7 11084,2105263158
100 2,5 10263,1578947368
150 2 8210,5263157895
200 1,6 6568,4210526316
300 1,2 4926,3157894737
400 0,9 3694,7368421053
500 0,75 3078,9473684211
600 0,67 2750,5263157895
700 0,58 2381,052631579
800 0,52 2134,7368421053
900 0,47 1929,4736842105
1000 0,4 1642,1052631579
2000 0,2 821,0526315789
3000 0,15 615,7894736842
4000 0,1 410,5263157895

The TGS822 sensor is affected by both temperatures and humidity and it should be complemented with a thermistor and hygrometer so that it is possible to compensate for temperature and humidity. I don’t have any thermistor or hygrometer yet but if we use the ”calculate R0 from RS (air)” every time we start the sensor then perhaps we will also compensate for temperature and humidity, this is something further experimenting will tell.

Hi, I’m Ketosense

Im_ketosense

During the weekend I have managed to put together a first very early prototype of my ”Ketosense”. It does register when a person is blowing in to the ”gas chamber” where the sensor is located and something that feels encouraging is that my wife gets significantly higher readings then me. Since I’m still a carbohydrate junky and she is not that is exactly what we want. However we do have some hurdles to cross before this thing is useful.

To-do-list

  • Calibrate the sensor
  • Determine sensor characteristics for gas concentrations between 0-50 ppm
  • Build a better moth piece

Calibrating the sensor

Calibrating the sensor is the major thing that needs to be done. In the sensor data sheet there is stated exactly how the resistance of the sensor behaves at different gas concentrations but everything relates to one calibration point, the sensor resistance Rs = Ro at 300 ppm of ethanol. Based on this value we can calculate the relation between sensor resistance and gas concentration for all other gas concentrations. Further in the data sheet it is stated that Rs at 300 ppm of ethanol is between 1-10 kΩ and that is quite a wide range and I don’t want to make a generalization of 5k before I even tried to calibrate it. Right now I don’t rely have a clue how to create a 300 ppm ethanol gas mix but hey, thats just another problem to solve.

Sensor characteristics for gas concentrations between 0-50 ppm

The datasheet for the TGS822 doesn’t have any data for how the sensor behaves at low gas concentrations between 0-50 ppm. In the graph displaying the Rs/Ro relation it looks like the function for the sensor resistance follows a Log-linear model so if I can determine the Ro (Rs at 300 ppm ethanol) of my sensor and also have the Rs for < 10 ppm of gas, which I presume is the ethanol content of normal room air, then I should be able to deduce some info for how the sensor should behave in that range as well.

Moth piece

I quickly understood that to get a good reading you needed to give the sensor some time to take the reading. This means we have to trap the gas around the sensor in some sort of chamber for a while to get a good reading. Right now I have a plastic cup with a tube and some tape over the opening and it does the job. However it is actually to air tight and I have to remove the tape from the opening to vent out the gas after getting a reading for the sensor to be able to reset it self. Another issue with the cup design is the condensation. Breath has quite a high moisture level and after just a few readings with this moth piece you start so see condensation on the inside of the cup and readings from the same person are different from time to time which they obviously shouldn’t be. Both humidity and temperature affects the sensor resistance so I am thinking of getting a humidity/temperature and add that so I can compensate for those factors but I believe that a better design of the moth piece can be just as or even more effective.

Measured sensor characteristics MQ-3 and TGS822

The 48 hour burn in of both sensors has now been completed and I have been able to do some initial measurements of the sensors characteristics. So far both the sensors does seem to react to acetone but that is not rely surprising. The sensor resistance range (Rs) does vary a lot between them, the TGS822 has a span of 300Ω – 78 kΩ while the MQ-3 has a more narrow resistance span of 22.6 – 1.5 kΩ. Since I am interested in low concentrations of gas I want to have as wide range as possible and have therefor selected to go on working with the TGS822 sensor first.

One aspect of the sensors that makes the them a bit annoying to work with is that they have a warmup period of 3-5 minutes before the resistance has stabilized it self and they also take quite a long time to return back to the initial value after a measurement has been done. The time it takes for the sensor to reset is related to how high the gas concentration was.IMG_3363

Figaro TGS822

  • Rs = 78 kΩ, 22 degrees C, 20% Humidity, normal air.
  • Rs = 300 kΩ when blowing into the sensor.
  • Rs = 300 kΩ after ail polish remover puff.

A good value for voltage divider resistor with the TGS822 should be 10k. A 10k resistor would give an output to the Arduino of just above 0.5 V at no gas detection up to a full 5 V for high gas concentrations.

Reset time, Resistance in kΩ/time after acetone puff.

  • 32 kΩ after 7 minutes
  • 41 kΩ after 10 minutes
  • 51 kΩ after 13 minutes
  • 54 kΩ after 15 minutes
  • 58.4 kΩ after 18 minutes
  • 62 kΩ after 21 minutes

MQ-3 sensor

  • Rs = 22.6 kΩ, 22 degrees C, 20% Humidity, normal air.
  • Rs = 15 kΩ when blowing into the sensor.
  • Rs = 1.05 kΩ after ail polish remover puff.

Reset time, Resistance in kΩ/time after acetone puff.

  • 10.5 kΩ after 8 minutes
  • 14.5 kΩ after 16 minutes
  • 17.9 kΩ after 26 minutes
  • 18.9 kΩ after 31 minutes

Blowing at the sensor with clean air did not seem to have any effect on the reset time.

The sensors has arrived

Yesterday the sensors I had ordered arrived, I had ordered the Figaro TGS822 and the MQ-3 from Hanwei Electronics. Since this type of sensors require a burn-in of 24-48 hours I wired them and applied some power. I had a suspicion that the sensors were actually going to be identical in function but perhaps I was wrong since they do actually use different amounts of current. The TGS822 sensor draws 117 mA while the MQ-3 draws 102 mA. It might just be that different individuals use different amount of current, further experimenting will tell if they also measure differently.

One thing that I didn’t expect was that the pins of the sensors are placed along the side of the round sensor following the curvature. This makes it impossible to stick them into a breadboard, since the pins will not line up with the holes in the breadboard, so I had to solder some wires to the connectors.

Sensors

There are a bunch of different sensors out there, based on price and sensitivity I have chosen to buy two different sensors the TGS822 from Figaro sensors and the MQ-3 from HANWEI ELETRONICS. If i wasn’t on a budget I would also have ordered a WSP2110 –  air polution detection since I believe that a combination with on of the TGS882 or MQ-3 with the WSP2110 could be a good match where the WSP2110 is has a sensitivity range between 1-50 ppm and the other two has a range between 10-1000 ppm. So the combined result of both could give us a more granular scale at lower concentration levels.

Figaro Sensors

TGS822 – Alcohol (ethanol) gas sensor, gas sensors is a tin dioxide (SnO2) semiconductor.

From datasheet for TGS 822

From datasheet for TGS 822

HANWEI ELECTRONICS CO.,LTD

MQ-3 – Alcohol Gas Sensor, is also a SnO2 sensor so it should have similar detection abilities as the TGS822 but acetone is not mentioned in the data sheet. According to one manufacturer this sensor has a detection range between 10-1000 ppm for alcohol and then about the same apply for acetone.

MQ-3 Sensor

From data sheet for MQ-3 sensor

MQ303A – Alcohol Gas Sensor, is essentially the same sensor as the MQ-3 however it works on a lower voltage. One serious disadvantage with this one is that a manufacturer states that the sensitivity of this sensor is 20-1000 ppm.

When you compare the MQ303A with the MQ-3 sensor’s range of 10-1000 ppm I think I will go with the MQ-3 sensor instead of the MQ303A since the concentrations we want to measure is between 0-200 ppm.

WSP2110 –  air polution detection, this sensor has a different ceramic substrate of subminiature Al2O instead of th SnO2 used in the other sensors and is more sensitive but also has a smaller detection range then the others, 1-50 ppm. it is also a lot more expensive as the other sensors.

ME3A – C2H5OH – This sensor works with a completely different type of chemical process.

”Detects gas concentration by measuring current based on the electrochemical principle, which utilizes the electrochemical oxidation process of target gas on the working electrode inside the electrolytic cell, the current produced in electrochemical reaction of the target gas are in direct proportion with its concentration while following Faraday law”

It seems to be very sensitive and detects between 0-1.000mg/L alcohol per liter, however it also have a price of /piece. Since this sensor works with a completely different technique I am not even sure if it detects acetone as well as ethanol and it is also way to expensive so just forget about this one.

Expected aceton concentration

After doing some Google searching I found several different studies on the concentration of acetone in a persons breath and here I will look in to some of them.

Study 1

I found a Study published in the paper ”The American Journal of clinical nutrition” where the concentration of breath acetone was measured after every hour for persons while eating a ketogen diet for 12 hours which states. ”Changes in breath acetone, plasma acetoacetate, plasma β-hydroxybutyrate, and urinary acetoacetate over the 12-h dietary study period are illustrated in Figure 1. By the end of the study, breath acetone increased 3.5-fold (from 33 ± 13 nmol/L at 0 h to 116 ± 19 nmol/L at 12 h).”

Breath Acetone

Breath Acetone

This gives an indication for what concentration of acetone that can be expected, however the persons in the study had not been eating a ketogen diet before the study so I don’t know what levels a person that has been eating a ketogen diet for a longer while will have but I suspect it will be higher then the 116 ± 19 nmol/L the participants in the study showed. Since most sensors give the sensitivity to different gasses in their data sheets as ppm i need to convert the 116 ± 19 nmol/L to ppm.

Found concentration in breath = 116 ± 19 nmol/L, nmole = (10-9) of a mole.

To convert nmol/L to ppmv we need to know the volume of 116 ± 19 nmol/L of acetone. This can be done by using the Ideal gas law.

V = nRT/P

P = atm = 1

V = Liters

R = 0.08206 L·atm·mol−1·K−1

n = measured in moles

T  = in kelvin (273.15 Kelvin = 0 C)

At 23 degrees and at 1 atm that amounts to: ((116 * 10-9) * 0.08206 * 296) / 1 = 2.81761×10^-6 liter = 2.8176 µL (microliters) parts per million in a gas system is equal to µL/L So the concentraion for the subjects in the study was 2.8176 ppmv.

Study 2

In this study a prototype of a ”acetone breach detector has been built and the engineers tested it by fasting for 17 hours and then blowing in to it.

”Results indicate acetone concentrations of 2.5ppm and 0.7ppm. Notably, the author (‘Steve’) had fasted for 17 hours and recorded a slightly high breath acetone value. When the sensor is recently calibrated and has been optimized properly, acetone sensitivity for breath measurements is conservatively estimated at several tenths ppmv, and it is appropriate for breath acetone measurements of healthy, metabolically stressed, and diseased individuals.”

Study 4

”The daily average acetone concentration of the dieters during this period was 290 nmoI/L (SD 8.1, range 280-300 nmol/L). The control subjects showed a daily average breath acetone concentration of 15 nmoIJL (SD 11 nmol/L) .”

(((300 * (10^(-9))) * 0.08206 * 296) / 1) * liters = 7.286928 microliter = 7.286928 ppmv

Study 4

Some rats on a ketogenic diet reached 500 nmol/(L*kg)

Conclusions

So all studies I have looked at indicated that the breath acetone concentration should be around 2-7 ppm. When it comes to sensor sensitivity that i very low and it will be difficult to find a sensor that has that level of sensitivity.

However I think the concentration of a acetone in a persons breath that has been on a ketose diet is significantly higher. Since many people report that the smell of aceton is noticeable and the required concentration of acetone for it to be detected by smell is 200 ppm according to the CDC I have decided to go set as a hypothesis that the sensor range should be around 0-200 ppm.