|
1 | | -# SGP43 Classifier Example |
| 1 | +# ML-Based Classifier Example |
2 | 2 |
|
3 | | -This example converts a log output from the Arduino measurement into an EDF file which can be read by Sensirion's |
4 | | -DataViewer. DataViewer is integrated |
5 | | -into [ControlCenter](https://sensirion.com/products/sensor-evaluation/control-center). |
| 3 | +This guide is about the avanced usage example of the **SEK-SGP4x** where it can be used for machine learning-based smell detection. |
| 4 | +Please refer to the associated application note **GAS_AN_SGP43**. |
6 | 5 |
|
7 | | -## Usage |
| 6 | +## Get started |
| 7 | +This example is using 2 different scripts: |
| 8 | +- An arduino sketch to run on your microcontroller |
| 9 | +- A python script to run on your computer |
8 | 10 |
|
9 | | -1. Compile the Arduino script and upload it to the microcontroller. |
10 | | -2. Check on which port the Arduino is connected to. |
11 | | -3. Follow the instructions in the following [README](./python_sgp43_classifier/README.md) on how to collect |
12 | | - measurements and run the classification script. |
| 11 | +The Arduino sketch executes the temperature ramps and return the resulting measurement using serial to the computer. |
| 12 | +From there, the python script can simply log the measurements to a file or run the example model prediction. |
13 | 13 |
|
14 | | -## Add New Temperature Profile |
| 14 | + |
| 15 | +### Wiring |
| 16 | +Connect the SGP43 sensor to your Arduino board using the guide in the [library instructions](../../README.md#connect-the-sensor) |
| 17 | + |
| 18 | +### Arduino script |
| 19 | +0. Install the Arduino IDE using the instructions from the [official website](https://www.arduino.cc/en/software/) |
| 20 | + |
| 21 | +1. Install the library and its dependencies. First, open the library manager located on left toolbar. Search for the `Sensirion I2C SGP43` library in the `Filter your search...` field and install it by clicking the `install` button. |
| 22 | +When asked about dependencies installation, click `Install all`. |
| 23 | +In case the library is not available through the library manager see the [manual installation](#manual-installation-of-the-library) section below |
| 24 | + |
| 25 | +2. Open the `exampleSGP43Classifier` sample project within the Arduino IDE: |
| 26 | + |
| 27 | + `File` ➔ `Examples` ➔ `Sensirion I2C SGP43` ➔ `exampleSGP43Classifier` |
| 28 | + |
| 29 | +3. In the top bar of the Arduino IDE, select the board matching the model you are using and the correct port. |
| 30 | + |
| 31 | +4. Click the `Upload` button in the Arduino IDE or `Sketch` ➔ `Upload` |
| 32 | + |
| 33 | +5. When the upload process has finished, open the `Serial Monitor` via the `Tools` menu to observe the measurement values. Note that the baud rate in the used tool has to be set to `115200 baud`. |
| 34 | + |
| 35 | +If you see measurement values printed to the serial monitor, then this section is complete. |
| 36 | +You are ready to collect the incoming data with the provided python script (see next section). |
| 37 | + |
| 38 | +### Python script |
| 39 | +You can now use the python script (located in the `python_sgp43_classifier` folder) to log and classify the measured values. |
| 40 | + |
| 41 | +This script converts the serial output from the Arduino measurement into an EDF file which can be read by Sensirion's DataViewer. DataViewer is included when installing [ControlCenter](https://sensirion.com/products/sensor-evaluation/control-center). |
| 42 | + |
| 43 | +In this step by step guide, we will use the provided model that can detect the present of banana or coffee beans scent. |
| 44 | + |
| 45 | +> [!IMPORTANT] |
| 46 | +> Please make sure you close the Serial Monitor before running the classifier, otherwise it will block the serial communication. |
| 47 | +
|
| 48 | + |
| 49 | + |
| 50 | +#### Requirements |
| 51 | +> [!TIP] |
| 52 | +> To run the python scripts, you need Python 3.11 or higher. |
| 53 | +
|
| 54 | + |
| 55 | +To install the dependencies you can either use `uv` (recommended) or `pip`. |
| 56 | + |
| 57 | +Using `uv` you can simply run while being located in the `python_sgp43_classifier` folder: |
| 58 | +``` |
| 59 | +uv sync |
| 60 | +``` |
| 61 | + |
| 62 | +Using `pip` you can run while being located in the `python_sgp43_classifier` folder: |
| 63 | +``` |
| 64 | +pip install -r requirements.txt |
| 65 | +``` |
| 66 | + |
| 67 | + |
| 68 | +#### Log measurements to a .edf file |
| 69 | +To log the measurements to a file (e.g. for later processing) use the command `measure`. |
| 70 | + |
| 71 | +``` |
| 72 | +python main.py measure --port <port> |
| 73 | +``` |
| 74 | +where `<port>` should be replaced by the port where the board is connected. |
| 75 | + |
| 76 | + |
| 77 | +A new EDF file will be created as soon as a new loop is detected. |
| 78 | +Each time a new loop is started, the previous one is |
| 79 | +written directly to the file. |
| 80 | + |
| 81 | +> [!TIP] |
| 82 | +> Make sure the board is plugged in before running the script. |
| 83 | +
|
| 84 | +### Live predictions using example model |
| 85 | +To get live predictions from the provided model we can use the command `measure-and-predict`. |
| 86 | + |
| 87 | +``` |
| 88 | +python main.py measure-and-predict --port <port> |
| 89 | +``` |
| 90 | +where `<port>` should be replaced by the port where the board is connected. |
| 91 | + |
| 92 | +The predicted category is printed to the console together with the output values of all three predictors. |
| 93 | +If the script cannot confidently determine a category, it outputs ???. This typically occurs immediately after powering |
| 94 | +up the sensor (during the initial blackout period) or when no category can be selected with a sufficient margin. |
| 95 | +The latter case also occurs during transitions between categories (for example, when moving the sensor from ambient air |
| 96 | +into a jar filled with coffee beans). |
| 97 | + |
| 98 | +A predictor value close to +1 indicates a strong match with the trained category, whereas a value close to -1 indicates |
| 99 | +no match. Therefore, if one of the three predictors shows a clearly positive value while the other two show negative |
| 100 | +values, it can be reliably assumed that the category associated with the positive predictor has been detected. |
| 101 | + |
| 102 | +> [!TIP] |
| 103 | +> Make sure the board is plugged in before running the script. |
| 104 | +
|
| 105 | + |
| 106 | +## Advanced topics |
| 107 | + |
| 108 | +### Add New Temperature Profile |
15 | 109 |
|
16 | 110 | 1. Copy the `default_tprofile.h` and rename it. |
17 | 111 | 2. Edit the array. |
18 | 112 | 3. In exampleSGP43Classifer.ino, change `#include "default_tprofile.h"` to include your newly created profile. |
| 113 | + |
| 114 | +### Manual installation of the library |
| 115 | +To manually install the library (without using the library manager) you can start by downloading the [latest release from github](https://github.com/Sensirion/arduino-i2c-sgp43/releases) as a `.zip` archive. |
| 116 | + |
| 117 | +Once the downloaded, it can be added in Arduino IDE using: |
| 118 | +`Sketch` ➔ `Include Library` ➔ `Include .ZIP library...` |
| 119 | + |
| 120 | +[!TIP] The manual installation will not prompt for dependency libraries. It is up to you to make sure all [dependencies](../../README.md#dependencies) are installed |
0 commit comments