A Flatulence Detector Using the SGP30 Sensor

Introduction: A Flatulence Detector Using the SGP30 Sensor

About: Scientist working in in-vitro diagnostics industry. Playing with all types of sensor as a spare time hobby. Aiming for simple and inexpensive tools and projects for STEM, with a bit of science and a bit of sil…

Summary: This instrucable will demonstrate how to trigger a device, here a MP3 player board, via a Reed relay attached to a RPi Pico or QT py microcontroller
, depending on an I2C sensor signal level.


In the following I would like to a simple to build flatulence detector with optical and acoustic output.

I actually had built two versions. One uses the Adafruit QT Py microcontroller, the other the new Raspberry Pi Pico. I'll try to describe both. Both versions use a SGP30 MOX eCO2/VOC sensor as input, a short CircuitPython script to analyze data and a LED, a buzzer and/or a MP3 player as output devices.

So you to place the sensor breakout somewhere close to the point of emission and upon detection of gastric winds the device will blink and sound a siren or gives some verbal comment or lets a dog bark. It's all up to you.

I developed the concept for the fart detector while working on a carbon dioxide (CO2) sensor for my son’s school class using an inexpensive eCO2/VOC sensor. I performed a bit of research and I found that many of these sensors are using other human emissions as a proxy for exhaled CO2. These are mainly hydrogen gas (H2) and some VOCs. VOC is an acronym for "volatile organic compounds" and covers gases such as methane or ethanol. Hydrogen sulfide (H2S), the major smelly component of flatulence, can presumably also detected by such detectors. Other than the chemically very inert CO2, these gases can be easily be oxidized and thereby quantified by electrochemical sensors. If I understood right, the H2/VOC detector is using a heated tin oxide coated glass plate as sensor element. The SGP30 sensor used here has multiple sensor elements which give two raw signals, a “H2” and an “ethanol” signal. An internal algorithm now calculates "CO2 equivalent" (CO2eq) values and total VOC (TVOC) values from these two raw signals. As the changes in eCO2 are much stronger than the ones seen in the raw data, eCO2 is used by the CircuitPython program to determine trigger values.

There have been several studies on the composition of farts. It seems that in humans it depends much on what you have been eating and the content of the gut microbiome. While H2 was found in the winds of all persons, methane (CH4) is not produced by all. Please be aware that humidity and temperature can influence the results determined by the SGP30 sensor. While I don't see this as necessary for this special application, but you could optimize the measurement quality by using an external humidity/temperature sensor. Or you may use the newer SGP40 sensor, which has these sensors included, instead of SPG30.

Technical Layout

The components used here are the Adafruit QT Py or the Raspberry Pi Pico microcontrollers, both running CircuitPython, an I2C SGP30 sensor breakout, a MP3 player/sound module with speaker and a reed relay. The QT Py controller has a STEMMA/QT adapter and an on-board Neopixel RGB LED. The later can be used to indicate the state of the sensor by colors.

The sensor I have used here is a Pimoroni SGP30 breakout, with jumper cables soldered to the ports and either connected to a breadboard or to the QT port of the QT Py via an adaptor cable. You might as well use the Adafruit breakouts of the SGP30 or SGP40 sensors or the SparkFun SGP30 sensor.

In its most simple form, the fart sensor may just consist of the SGP30 sensor, a QT Py and a USB power bank. The Neopixel on the QT Py will change color depending on the degree of contamination determined by the sensor, e. g. from blue to green, yellow and red.

It is more fun to couple the microcontroller with a voice playing system. Hereto I brought a cheap re-programmable MP3 sound player that comes with on-board LiPo and a speaker. Usually one would trigger the player by pressing a button on the breakout. I soldered two jumper cables to the trigger button and connected them to the two outer ports of a Massuse MES1A05 Reed relay. The two inner ports of the Reed relay are connected to the A0 and ground ports of the QT Py or GP15 and GND for the Pico. When powered, this will close the contact, and evoke an on signal on the player. Hereby you can play noises or comments from the player if the degree of contamination had reached a "critical" level. I’ll leave the plethora of options for the acustic feedback to your fantasies, but I implemented a siren, a fog horn, a barking dog and some more.

For a simpler layout you may use a buzzer as cheaper acoustic indicator. I included one in the QT Py prototype.

You now may place the sensor at the underside of a toilet seat, or into a chair cushion, or a trouser pocket … and wait. I would strongly recommend to optimize and test settings on yourself before you try to trick others.

Keep in mind that not everybody may find practical tricks like this funny and the whole thing, including the device, might go down the drain.

Ach, before I forget: You also could adapt this for a talking reminder to open class room windows if the eCO2 values are getting too high.


Adafruit QT py (Pimoroni, 7.40 €) or

Raspberry Pi Pico (5€)

Pimoroni SGP30 breakout (Pimoroni about 19 €)

(or the analogue Adafruit or SparkFun sensors, which come already with Stemma/Qwik adaptors)


a breadboard

a buzzer


Massuse MES1A05 Reed-Relay (Conrad, 2.91€)

Zerone Re-recordable USB MP3 Player (Amazon, 9.49 €)

cables, including a Stemma/Qwik - jumper cable

USB power pack

Step 1: Assembly

Raspberry Pi Pico

  • In case you want to use the Pico with a breadboard, add the provided headers
  • Install CircuitPython on the Pico as described here
  • Get the latest version of the CircutPython Library and install the SPG30 driver script on the Pico
  • Install the provided script (see next step) on your Pico as "code.py"


  • In case you want to use the QT Py with a breadboard, add the provided headers.
  • Get the Neopixel circuit python driver from the Adafruit CircuitPython driver library and install, as described in the Adafruit protocol.
  • Get the SGP30 CircuitPython driver by copying it from the Adafruit CircuitPython device driver library. Copy the file to the subdirectory on the QT py.
  • Copy the right version for your configuration (Neopixel and/or buzzer or MP3 player) of the Circuit Python script for our device to the root directory of the QT py. (in a later step, coming in near future)

SGP30 breakout

  • In case you use the Pimoroni breakout, attach (jumper) cables to the Vin, Ground, SCL and SDA ports. If you use a QT adaptor cable, take care to connect the right lines.
  • If you use the Adafruit or Sparkfun breakout, just connect them with a (long) QT/Qwik connection cable.

Reed relay switch

  • The Reed switch is a very simple and cheap electromechanical device. In contrast to an optocoupler I had tried, the 3.3V signals from the QT Py and RPi Pico (or Raspberry Pi and many other microcontrollers) is sufficient to close the Reed relay.
  • The current running through the two inner ports is generating a magnetic field that is closing an internal switch connected to the outer ports (see diagram). It's that simple, no silicon required.
  • You may place it on the breadboard or solder the cables directly to the port.


  • Place the buzzer on the breadboard and connect the (long) + port with "A2" and the (short) - port with GRN on the QT py.

Configuration of the MP3 player

  • Solder two (jumper) cables to the trigger button on the player. To use the player for this device, connect them to the outer ports of the Reed switch, either via headboard or solder directly to them.
  • Pick some MP3 files with the sound/s you like to play, e.g. from public MP3 sound libraries. You may add several to the MP3 player and switch between them by keeping the volume button pressed while playing a sound.
  • Turn on the MP3 player, connect it to your computer and copy the sound files to the gadget. Make sure to use a real microUSB cable, not a charging only cable. In case you may want to replace the Chinese sound files on the player by ones of your choice. Remember to turn off the device if not used.

Power pack

  • To power the device for in-field use, you can power them from a USB power pack, alternatively from a LiPo or battery pack. Best options may depend on the microcontroller and your special project.

Step 2: The Script (Pico Version)

The script used for the flatulence detector is to large parts based on the SGP30 CircuitPython script example by Adafruit. To drive the Reed relay, and thereby the MP3 player, I added a digital IO at GP15 of the Pico, or at the A0/D0 port of the QT py. For the buzzer, a simple IO was used at the QT Py's port A2/D2.

Using Mu as IDE, you can read and display values reported by the sensor by activating the Mu serial port and plotter. My experiments show a very rapid increase in signal strength, followed usually by a rapid decrease within a few seconds.

The MP3 player was intended to be started by pressing the trigger button. Playing a tune can be stopped by a second press on the trigger button. The trigger button signal can also be evoked by shortening the two cables I had soldered to the button, here by activating the Reed relay. For this we send a current through the line that is inducing a magnetic field in the relay, simply by sending a digital output signal from a microcontroller port, e. g. GP15 on the Pico or A0 on the QT Py. We hereto are using the digitalio library and change the respective value from "false" to "true".

To control the SPG30 sensor the Adafruit SPG30 library must be installed on the microcontroller. Then the sensor just has to be connected to ground, vcc, SCL and SDA ports. In the Pico version I used ports GP8 and GP9 for i2c connections.

The QT Py has an onboard RGB LED, that can be used as an optical output, with different colors for different levels of air quality. To use it you need to install and load the Adafruit NeoPixel library.

If you want to play the MP3 just for a specific time, you may send a second signal with a delay of several seconds.

The script may contain errors and there is some room for improvements.

If you can help please let me know.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

Example for using the SGP30 with CircuitPython and the Adafruit library
Modified for a flatulence detector by Dr H
Raspberry Pico version

import time
import board
import busio
import adafruit_sgp30
import digitalio

reed_switch = digitalio.DigitalInOut(board.GP15)
reed_switch.direction = digitalio.Direction.OUTPUT

onboard_led = digitalio.DigitalInOut(board.GP25)  # Pico onboard LED
onboard_led.direction = digitalio.Direction.OUTPUT

white_led = digitalio.DigitalInOut(board.GP13)  # optional external LED on GP13
white_led.direction = digitalio.Direction.OUTPUT

SCL = board.GP9 # Pico
SDA = board.GP8 # Pico
i2c = busio.I2C(SCL, SDA, frequency=100000)

# Create library object on our I2C port
sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c)

print("SGP30 serial #", [hex(i) for i in sgp30.serial])

sgp30.set_iaq_baseline(0x8973, 0x8AAE)

elapsed_sec = 0

while True:
    print((sgp30.eCO2,sgp30.TVOC))  # output for mu plotter
    #print("eCO2 = %d ppm \t TVOC = %d ppb" % (sgp30.eCO2, sgp30.TVOC))   # txt out
    elapsed_sec += 1
    if elapsed_sec > 10:
        elapsed_sec = 0
            "**** Baseline values: eCO2 = 0x%x, TVOC = 0x%x"
            % (sgp30.baseline_eCO2, sgp30.baseline_TVOC)
    if (sgp30.eCO2 > 800):   # sets threshold value for eCO2 according to your needs; base value: 400
        reed_switch.value = True
        onboard_led.value = True
        white_led.value = True
        reed_switch.value = False
        onboard_led.value = False
        white_led.value = False

Step 3: Some Ideas on More Serious Applications

Measuring the frequency and timing of flatulence with a cheap sensor could have some serious applications, e g., for monitoring patients with gastrointestinal disorders, or certain farm animals.
Hydrogen release assays are also used in clinical diagnostics, for example in some lactose intolerance assays.
More ideas? Let me know your suggestions.

Step 4: QT Py Script

A gap, to be filled in near future with code

Microcontroller Contest

Participated in the
Microcontroller Contest

Be the First to Share


    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • For the Home Contest

      For the Home Contest
    • Big and Small Contest

      Big and Small Contest