Internet Of Smells: Giving A Machine The Job Of Sniffing Out Spoiled Food

Has the food in your pantry turned? Sometimes it’s the sickening smell of rot that tells you there’s something amiss. But is there a way to catch this before it makes life unpleasant? If only there were machines that could smell spoiled food before it stinks up the whole place.

In early May, I was lucky enough to attend the fourth FabLab Asia Network Conference (Fan4). The theme of their event this year was ‘Co-Create a Better World’. One of the major features of the conference was that there were a number of projects featured, often from rural areas, that were requesting assistance throughout the course of the conference.

Overall there were many bright people tackling difficult problems with limited resources. This is how I met [Yogesh Kulkarni] who runs a FabLab in Pabal, a farming community not far from Pune, India. [Yogesh] has also appeared on TED Talks (video here). He explained to me that in his area, vendors sell milk-based desserts. These are not exactly refrigerated, and sometimes people become ill from eating them. It would be nice if there was a way for the vendors to avoid selling the occasional harmful product.

I’ve had similar concerns with food safety in my area (Vietnam), and while it has been fine nearly all of the time, a few years ago I nearly died from a preventable food-borne illness. I had sufficient motivation to do a little research.

Chemically, Has This Food Turned?

We can smell rotten food, but can we smell food that’s very recently become unsafe to eat? There is a huge gulf between the two, and to make things more difficult we’re on the hunt for a mechanism that will act as a detector.

This place for example is fantastic: the ribs in the upper right approximate glazed and roasted smoked ham. They are to die for — but not literally.
Sensor with multiple types of metal-oxide gas detectors. Source: Fraunhofer IPM

What I found was there was a pre-existing body of research in India on using volatile organic compound (VOC) sensors to identify spoiled milk (PDF). More generally, these sensors are used in commercial devices to detect spoilage and adulteration in a wide variety of food products (PDF). These devices are sometimes called an ‘electric nose’.

Before we continue, let’s get one thing out of the way. ‘Electric nose’ isn’t a great name, neither is ‘field olfactometer’. Smelloscope is an objectively better term, so let’s go with that. Fans of a certain cartoon might consider that good news as well.

Characterizing VOC Sensors

Moving on, there were two analog VOC sensors readily available to me at a reasonable price: the MQ-2 flammable gas sensor (PDF datasheet ) and the MQ-135 gas sensor (PDF datasheet). I wasn’t sure which would be most useful for detecting spoiled milk, so I picked both up to connect them to a WeMos Mini D1 running NodeMCU.

The general concept here is to use one or both of these sensors to determine a normal range for specific fresh and spoiled foods, and see if it can differentiate the two at some degree of reliability. The MQ-135 sensor is particularly interesting for this application as it is sensitive to a more relevant range of gases including carbon dioxide.

Effect of Temperature (-20 to +50 Celcius) and Humidity (top to bottom: 30%, 60%, and 85% RH) on sensor resistance. Source: MQ-135 PDF Datasheet

These sensors require relatively large amounts of stable power (about 750 mW each at 5V), so both can be powered off a 7805 linear regulator, although I found it gets rather warm when powered with 9V and overheats at 12v, so keep that in mind if powering both sensors. They also vary their output not only with the gases of interest, but also temperature and humidity, so that needs to be tracked as well. In this case I’ve used the popular DHT-22 combined temperature/humidity sensor as we only need rough values.

The next issue is that there’s only one analog input on my Mini-D1, so borrowing a trick from some oscilloscopes, a small 5v SPDT relay connects only one of the sensors to the analog input at a time. A transistor triggers the relay to switch sensors. In hindsight, a simpler way to do this would have been with a single-pole double-throw (SPTD) switch to connect the output of the sensor of interest to the analog input. I wanted to leave the possibility of electronic control though, so this is fine for now.

Once assembled, the code for this device was very simple, just reading the ADC and temperature/humidity sensor every second :

pin = 7
function amount()
    x =
    status, temp, humi, temp_dec, humi_dec =


Finally I added a small screen (an SSD1306 128×32 OLED) and a barrel jack for 7.4v battery power, because there isn’t much street food for sale at my workbench. I’ve covered using this screen before with NodeMCU if you’re interested in more detail.

pin = 7
function amount()
    x =
    status, temp, humi, temp_dec, humi_dec =
    print("DHT Temperature:"..temp..";".."Humidity:"..humi)
    print (x)

-- Setup screen
sda = 2
scl = 1
sla = 0x3C
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_128x32_i2c(sla)
-- choose font
-- display data
disp:drawStr(2, 12, "smell / temp / humi")
disp:drawStr(2, 26, x.." / "..temp.." / "..humi)

until disp:nextPage() == false



What Did We Learn About Sensor Performance?

The initial results were mixed. First off, the MQ-2 flammable gas sensor did not prove to be particularly sensitive – when I sprayed butane nearby, the output only increased from 8mV to 10mV. A quick test demonstrated it was correctly connected to the ADC – I suspect a defective sensor. I’ve used this sensor before, and recorded a baseline reading of about 70mV, increasing to about 500mV in the presence of a little butane, and to about 350mV in the presence of slightly fermenting natural honey. Adjusting the output gain did not fix the issue, so I put this sensor aside for the moment.

The MQ-135 sensor worked quite well on the other hand. A quick test showed an increase in voltage when I breathed on it, more than can be accounted for by the increased humidity, therefore it was probably correctly detecting carbon dioxide. It was also very sensitive to butane, with the output approaching 1V near a butane source.

The next step was to check the trash bin. I had just emptied it, but it still smelled faintly of food waste and caused the sensor output to increase from 136mV to 196mV near the lid of the bin. So far so good – it seems at least potentially useful for detecting spoiled food, paving the way for Internet of Smells® technology.

That Time I Purposely Let Food Spoil

To simulate spoiled milk based desserts… I actually just let some food spoil. I set aside four plastic containers of natural yogurt containing active bacterial culture. One I placed outside for 29 hours, one for 22 hours, and another for 17 hours. The final sample stayed in the fridge until 15 minutes before the experiment, so it would be approximately the same temperature as the other samples. I sampled each with the smelloscope after letting the device warm up for 2 minutes.

In the end, this wasn’t as informative as I hoped, with the measured value only increasing slightly over time, until it drops with the 29-hour sample, which was visibly drier from being left outside so long:

Environmental conditions were 28-32 degrees Celsius, and 80-90% relative humidity.

From the data I can conclude that this type of sensor may very well be capable of identifying spoiled food, but it would require quite a bit of data collection to get to the point where it’s a practical device. In other words, it was less useful than I hoped — but there’s no sense crying over spoiled milk.  I looked around my kitchen for other applications, I remembered that I had bought a durian…

Depending on your tastes, a durian is either the best fruit in the world, or about as appetizing as old gym socks. I happen to very much enjoy them (durians, not socks), but one thing is certain: They are very fragrant, to the point being banned in hotels, taxis, and public spaces in some countries.

In any case, a quick measurement of the closed and open fruit suggests I might be able to use this device to select the most fragrant fruit at the market, without opening it. Further delicious experimentation is clearly in order, but my curiosity (and appetite) are sated for now.

17 thoughts on “Internet Of Smells: Giving A Machine The Job Of Sniffing Out Spoiled Food

  1. What about a “nice” smelly cheese? Do we know enough about smell to be able to definitively classify good vs. bad smells? Smell (as beauty) is in the nose (eye) of the beholder.

  2. Great project!

    I think your best bet is to focus on the common chemical gases found in all stink. The stinks come from thiols. But Hydrogen Sulfide is common too. But the number one gas is METHANE (CH4). If you use a MQ-4 sensor which Natural Gas industry uses to detect their gas leaks. At only $5 USD it’s an experiment which should be a win-win for you.

    The level of methane build up in your frig should alert you to something is spoiling and the bacteria responsible is giving off the bad smells. It should work for eggs, milk, and cabbage. Not sure about other smells. One way to kill bad odors that are high in thiols is a spray of pure H2O2 (0.52 cents USD @ Walmart). You can spray it on your pet’s dried up stinky pee stain on the floor. It might work on skunk stink too.

    1. Pure H2O2 @Walmart for $.52? I think not! Pure Hydrogen Peroxide is a very dangerous beast. Now, 3% Hydrogen Peroxide mixed with a bit of Phosphoric acid to stabilize it can be bought cheaply most anywhere, but no way are you going to find pure hydrogen peroxide being sold that way and if you did, you wouldn’t want to go near it unless you really knew what you were doing.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.