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.
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).
- Arduino Uno, 9$
- LM7805L 5V Voltage Regulator IC, 1$
- 16 x 2 Character LCD Display Module with Blue Backlight, 4.70$
- 3 Micro switches less then 1$
- Plastic case 70x50x50, 4.8$
- Prototype circuit board
- Some wires
- Electrochemical gas sensor, TGS822 produced by Figaro, 3.20$
- Digital Temperature Humidity Sensor Module, DHT11, 3.10$
- Plastic case, 3.10$
- 4 stranded telephone wire to connect it to the Arduino
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.
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;
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
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:
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.
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.
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.
Every 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.
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.
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:
- Some additional info on working with arduino and an electrochemical gas sensor: Arduino Breathalyzer: Calibrating the MQ-3 Alcohol Sensor
- If you want more info about how to wire the LCD take a look at Jeremy Blum’s excellent tutorial about it on youtube.
Based on Steves posts it looks like it is actually possible to detect if a person is in a ketose state or not by analyzing that persons breath. But what are the signs of ketosis?
After doing some researching on Wikipedia I concluded that what we are looking for is acetone.
|Molar mass||58.08 g mol−1|
|Odor||Pungent, irritating, floral|
|Density||0.791 g cm−3|
|Melting point||-95–93 °C, 178-180 K, -139–136 °F|
|Boiling point||56-57 °C, 329-330 K, 133-134 °F|
|Vapor pressure||24.46–24.60 kPa (at 20 °C)|
|Refractive index (nD)||1.35900|
From the Wiki page about Ketone Bodies
Ketone bodies are three water-soluble compounds that are produced as by-products when fatty acids are broken down for energy in the liver. Two of the three are used as a source of energy in the heart and brain while the third (acetone) is a waste product excreted from the body. In the brain, they are a vital source of energy during fasting. Although termed ”bodies”, they are dissolved substances, not particles.
The three endogenous ketone bodies are acetone, acetoacetic acid, and beta-hydroxybutyric acid, although beta-hydroxybutyric acid is not technically a ketone but a carboxylic acid. Other ketone bodies such as beta-ketopentanoate and beta-hydroxypentanoate may be created as a result of the metabolism of synthetic triglycerides such as triheptanoin.d
Individuals who follow a low-carbohydrate diet will also develop ketosis, sometimes called nutritional ketosis, but the level of ketone body concentrations are on the order of 0.5-5 mM whereas the pathological ketoacidosis is 15-25 mM.
From the Wiki page about Ketosis
Steve also published a post at a newsgroup were he presented some more details about his ”KetoFlute”. He also included a picture of his creation.
Date: Jun 12 2012 21:12:02
I've been silent for the past week while I've been pushing to
get a prototype ketone breathalyzer finished
I've never worked with USB at the hardware/software level
before. So I didn't appreciate that the USB HID (Human Interface
Device) was inherently low-bandwidth, or at least low polling
rate... fixed at a minimum of 16 milliseconds per packet
Since my whole effort has been toward determining as quickly as
possible whether I CAN detect exhaled ketones, and differentiate
their effect on the gas sensor from the extreme effects of
temperature and humidity, I have wanted to race to reach that
conclusion as fast as possible. That meant NOT placing any
processor out on the data collection end... just using a "dumb"
solution that could talk to ultra-high-resolution analog to
digital converters. But THAT meant that the USB HID polling
rate severely limited my possible transactions with the ADC's.
Consequently, I was forced to scrap my first iteration base upon
the USB HID spec and switch to mainstream USB data exchange.
THAT second-generation solution is now FINISHED and WORKING:
In the photo, you can see the mouthpiece at the lower right,
the gas collection chamber, and its connection to the pair of
22-bit ultra-high-resolution SPI-interface surface mount ADC's.
The large green button allows the "user" to signal to the
software, though I'm unsure that it will be necessary since
there is NO DOUBT when someone is blowing into the gas
The two large power-transistor-looking things near the top are
a P-Channel Power MOSFET which I use to switch the USB's 5 volt
supply to the pair of gas sensors, and an adjustable voltage
regulator which I use to drop the USB's 5 volt supply down to
3 volts for the gas sensors. (If this should ever evolve into
a limited production run build of KetoFlutes, it would run on
a pair of AA cells, so 3 volts is my operating target.)
The circuit board at the far left is an FTDI USB-to-Serial
protocol bridge which, among other things, supports the SPI
protocol used by the pair of Microchip MCP3553 ADC's.
The left-most trimpot adjusts the sensor voltage to 3vdc, and
the other four trimpots adjust the gain and offset for each of
the two data acquisition channels.
It's all working and able to collect a pair of high-precision
22-bit samples at about 22.5 samples per second, which is more
than adequate for my purposes.
And I have a prototype console app that reads and displays the
data from both channels, but I don't have anyone handy who is
NOT in ketosis. For ME, I'm seeing a DEFINITE "common-mode"
signal difference between the "signal" sensor that IS supposed
to respond to volatile gasses and the "control" sensor that
should NOT respond. But I don't yet know that I might not
just be seeing a difference in, for example, the channel gain.
(Though I don't really think I am, since I swapped channels and
the response moved with the sensor.)
I have the podcast tomorrow that I need to switch over to now.
So it will be for another day or two before I'm able to bug my
NON Ketogenic friends and have them blow into the mouthpiece
while I collect and log the data for subsequent analysis.
I may well remain in a fat-burning ketogenic state for the rest
of my life. So I would LOVE to have a handy ketone breathalyzer
that can yield both qualitative and quantitative realtime
appraisals of my body's ketone status. I've been poking my
fingers and drawing drops of blood for test-strips several times
per day, but at kr31.73 () per test it's not really affordable over the
long term, and my fingers are getting a bit chewed up.
So... If I can make this work, I will DEFINITELY build at least
one standalone AA battery-powered "KetoFlute" for myself. And
if there appears to be sufficient interest among our podcast
listeners, then I'll likely do a single production run of these
devices to equip everyone who wants one.
This all began when I heard of Steve Gibsons work of building a ”KetoFlute” and I decided to look in to if I could build one as well. I use this blog as a note book for my initial research. From what I have found Steve talked about his ”KetoFlute” twice at the Security now! podcast.
From the transcript of theSecurity now! podcast ep 356, June 6, 2012.
STEVE: That's the little prototype for the ketone breathalyzer. LEO: [Laughing] You madman. You've done it. Does it work? STEVE: It's on its way. LEO: He's breadboarding a ketone analyzer. Well, it's about time. STEVE: Yeah, exactly. LEO: Wow. STEVE: Because I don't have enough things on... LEO: What chip do you use to detect the presence of ketones? Is there a sensor? STEVE: There are volatile gas sensors which will detect ethanol and also acetone. The problem is that they all - they're very sensitive to temperature and humidity, and our breath is both hot and moist. So the signal I'm looking for is minuscule compared to the noise, which is temperature and humidity. So I have a second sensor which is exactly the same technology, but designed to detect methane instead. And so the idea is that the common mode response will be humidity and temperature, and the differential response will be the content of gases that differ between the two sensors. So anyway, I'm just at the beginning of... LEO: What a fun challenge. STEVE: ...of experimenting. It may be that I cannot find - it may be that breath is just too hostile because of its temperature and humidity. But I'm going to - I'm working to very quickly determine, one way or the other, because I am just so tired of - my hands are just raw from poking them in order to take blood several times a day, which I have been doing. LEO: Several times a day? STEVE: Oh, yeah, yeah, because I'm spending serious money on these ketone blood tests in order to monitor my ketones and get a sense for where they are. I would - I can't wait to be able to, you know, to blow into something. And if it works, we'll, I mean, I'm not going to go into production. People don't have to worry about me disappearing...
From the transcript of theSecurity now! podcast ep 358, June 20, 2012.
LEO: Ho ho ho, he's building it, ladies and gentlemen. STEVE: Yeah. LEO: You put that on your head? What the hell? STEVE: You blow in there. LEO: For those listening, it is breadboarded. Lots of - I see six, five pots on there. I see a bunch of circuitry, wires. And there is a little - there's a USB interface. Oh, that's cool, so it's USB. And a thing, I see a thing, I guess that you could buy that off the shelf, that you blow into. STEVE: Oh, no, that's a made-from-scratch... LEO: You made that, too. STEVE: It's a chamber that has the sensor located inside... LEO: Dude, you rock. STEVE: ...which is measuring - anyway. So the point is... LEO: You're calling that the Ketoflute, right, as I remember. STEVE: The Ketoflute, yeah. And I've invested heavily. I've had friends blowing in it. I mean, it's working. But I still don't know... LEO: Hey, come here. Can I get you to blow into this? STEVE: I still don't know if I'm going to have anything. LEO: Right, this is research. STEVE: Yeah, it's pure R&D. LEO: Steve, this is why we love you. I'm getting tears in my eyes. This is amazing. STEVE: I need to answer the question. Maybe it'll work; maybe not. But again, if I have to go, well, okay, now I know, at least I've satisfied my curiosity without needing it to be something it isn't. So... LEO: You see? STEVE: It's important to be a pure researcher. LEO: And everybody needs to have that. If people could develop that mentality, it'd be so great. If you don't know, investigate. And you probably don't know. That's the problem. Smart people think they know. STEVE: Well, and that was the lesson from the Portable Dog Killer. I just encourage people to go build something because, oh, the act of doing that teaches you so much.